Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Questions about classes (https://forums.graalonline.com/forums/showthread.php?t=81048)

The_Kez 08-04-2008 09:06 AM

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?

xXziroXx 08-04-2008 02:08 PM

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.

Skyld 08-04-2008 02:24 PM

Quote:

Originally Posted by xXziroXx (Post 1411271)
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.

xXziroXx 08-04-2008 06:00 PM

Quote:

Originally Posted by Skyld (Post 1411272)
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.

Inverness 08-04-2008 06:04 PM

I personally avoid joining classes to the player. Its simpler to join stuff to weapons and use <WeaponName>.<functionName>() or so.

cbk1994 08-04-2008 06:50 PM

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.

[email protected] 08-04-2008 06:58 PM

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
}


Inverness 08-04-2008 07:16 PM

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
}

The_Kez 08-04-2008 08:21 PM

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.




All times are GMT +2. The time now is 01:33 PM.

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