Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-04-2008, 09:06 AM
The_Kez The_Kez is offline
N-Pulse Asst. Manager
The_Kez's Avatar
Join Date: Dec 2007
Posts: 106
The_Kez is on a distinguished road
Send a message via MSN to The_Kez
Questions about classes

My understanding about classes is that when you join a class to an object, the 'this' object in the class becomes the object you joined the class to. So if you joined a class to a player, obviously 'this' would be this player who triggered the class function to be called.
I'm wondering if the 'this' rule of classes applies to all other objects aside from the player. So that if you join a class to a level NPC, 'this' would be the NPC the class is joined to.
One would wonder how I've gone this long without clarifying this basic idea about classes, but here I am :P

My more specific question stems from the same general question about classes. I want to join a class to my GUIs to reduce the clutter in some of my system weapons. I want to add functions to my GUIs (for example Inventory.toggle()) but I'm having a hard time getting my functions to call.
My idea to do this would be:

PHP Code:
//#CLIENTSIDE
function DrawWindow() {
new 
GuiWindowCtrl"TestWindow" ) {
  
this.join"testwindowfunctions" );
  
//window attributes
}
}
function 
onKeyPressedcd ky ) {
  if ( 
ky == "Q" TestWindow.Toggle();
  if ( !
TestWindow.Toggle() ) DrawWindow();

Which would trigger:

PHP Code:
//#CLIENTSIDE
public function Toggle() {
  echo( 
"Object: " this );
  if ( 
this.visible this.destroy();
  else return 
false;

I have a script similar to this running in an inventory window, but I can't seem to call the class.

Don't know if this is a very practical way of doing things. Mainly I'm just looking for some clarifications about this stuff. And if anyone has any better ideas on how organize the GUI scripts that is always welcome too. =)

UPDATE:
The above script probably wouldn't work because the function is called before the class ever has a chance to be joined to the object. How can I join the class to the object to do something like that if the class is supposed to be triggering the window to be created in the first place?

Last edited by The_Kez; 08-04-2008 at 09:59 AM.. Reason: Update
Reply With Quote
  #2  
Old 08-04-2008, 02:08 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
I always do something like this whenever I know I'll use a class for both NPC's and players:

PHP Code:
function myFunction()
{
  if (
objecttype() == "TServerPlayer"temp.obj "player";
  else if (
objecttype() == "TServerNPC"temp.obj "this";

  (@
obj).variable 666;
  (@
obj).triggerAnotherCoolFunction();

Etcetera etcetera.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #3  
Old 08-04-2008, 02:24 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by xXziroXx View Post
I always do something like this whenever I know I'll use a class for both NPC's and players:

PHP Code:
function myFunction()
{
  if (
objecttype() == "TServerPlayer"temp.obj "player";
  else if (
objecttype() == "TServerNPC"temp.obj "this";

  (@
obj).variable 666;
  (@
obj).triggerAnotherCoolFunction();

Etcetera etcetera.
Not necessary. If this.objecttype() is returning TServerPlayer, then "this." is already pointing to the player. Therefore, the whole "temp.obj" thing is redundant and you could just use "this." to refer to whatever you joined the class to, regardless of what it is.
Reply With Quote
  #4  
Old 08-04-2008, 06:00 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Skyld View Post
Not necessary. If this.objecttype() is returning TServerPlayer, then "this." is already pointing to the player. Therefore, the whole "temp.obj" thing is redundant and you could just use "this." to refer to whatever you joined the class to, regardless of what it is.
I think I had problems with it a while ago, which is what spurred me into doing like that. Quite sure the problem was with this.objecttype() returning one thing, while plain objecttype() referred to the player with the joined class. Wish I remembered more.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #5  
Old 08-04-2008, 06:04 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
I personally avoid joining classes to the player. Its simpler to join stuff to weapons and use <WeaponName>.<functionName>() or so.
__________________
Reply With Quote
  #6  
Old 08-04-2008, 06:50 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
This is what we do on Vesporia:

PHP Code:
join"functions_gui" );
//#CLIENTSIDE
function onCreated()
{
  
showMenu();
}
function 
showMenu()
{
  
profile "vspWindow";
  
  
width 300;
  
height 300;
  
  
center();
  
setVars();
  
  
text "hehe window";

The functions basically look like this:

PHP Code:
//#CLIENTSIDE
function center()
{
  
// how to center

Think of a class as a set of functions added onto the script.

If I have a class that does this:
PHP Code:
function foo()
{
   return 
true;

and a script that does this:

PHP Code:
join"myclass" );
function 
onCreated()
{
  echo( 
foo() );

Then you can think of it like this:

PHP Code:
function onCreated()
{
  echo( 
foo() );
}
function 
foo()
{
   return 
true;


You can use this. to work with the object whether it is joined to a player or an NPC.

PHP Code:
//#CLIENTSIDE
function onCreated()
{
  
this.chat "hi";

That will work whether joining to an NPC or a player.
__________________
Reply With Quote
  #7  
Old 08-04-2008, 06:58 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
hmm, use public functions i suppose and just name the weapon?
Here's an example with an inventory kind of system

Weapon Name 1
HTML Code:
function onCreated() {
  Swords = this;
}

public function onEquip() {
  //Do stuff
}
From another NPC
HTML Code:
function onEquip() {
  //Some-how find out it's a sword type of weapon you're equipping, generally by reading a string
  Swords.trigger("Equip", "");
  //Or you could do 
  Swords.onEquip();
  //However, if you didn't use that method, you'd use this 
  findWeapon("Weapon Name 1").onEquip(); //Or the trigger etc
}
Reply With Quote
  #8  
Old 08-04-2008, 07:16 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Profiles are objects not strings. The only reason strings work is because Stefan is too nice about fixing peoples scripting errors for them.

new GuiWindowCtrl("TheWindow") {
this.profile = GuiWindowProfile;
// blah
}
__________________
Reply With Quote
  #9  
Old 08-04-2008, 08:21 PM
The_Kez The_Kez is offline
N-Pulse Asst. Manager
The_Kez's Avatar
Join Date: Dec 2007
Posts: 106
The_Kez is on a distinguished road
Send a message via MSN to The_Kez
Okay well this answers my question about classes Thanks.

As far as joining the functions to the GUI object, the idea I'm getting from you guys for equipping items (since I have a system for custom weapons, I trigger the server for custom weapons to be equipped/unequipped) or similar is to say:

-System/WepName1

PHP Code:
function onEquipobj ) { //catchevent for "Equip" button onAction
  
obj.Run();

To trigger...

PHP Code:
//#CLIENTSIDE
  
public function Run() {
   if ( 
this == "Inv_Button1" ) {
     
//trigger the server of -System/WepName1 to equip the player's item
     //like normal
   
}
   elseif ( 
this == "SomethingElse" ) {
     
//run the function appropriate for when this button is pressed
   
}
   
//etc.

Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.