Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-30-2017, 02:24 AM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,743
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
What is the purpose of "join"?

My novice scripting skills are coming to the light.

I'm learning both GS2 and scripting with an NPC server for the first time and I see several references to the "join" command. I assume it is used to call code from a class NPCs? Is there a benefit to using "join" over just pasting the code directly into the NPC you're working with?

Also I see "this.join" as well. What is the difference between the two commands?
__________________
Save Classic!
Reply With Quote
  #2  
Old 07-30-2017, 04:29 AM
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
The idea behind join is that you can put common utility code in classes, and then "join" that code onto weapons and NPCs in order to re-use it. It's like you're copying-and-pasting the code from the class onto the weapon/NPC, but without actually duplicating it.

For example, let's say you wrote a function which reverses a string. You could put in a class called func_strings, and then join func_strings anywhere you want to call that function from.

I think maybe this old post about classes might help as well:

Quote:
Originally Posted by cbk1994 View Post
All a class does is extend a weapon or NPC. It doesn't stand on its own.

Class functions_bank
PHP Code:
function deposit(temp.amountToDeposit) {
  
// obviously this is just an example, don't actually use this
  
player.rupees -= temp.amountToDeposit;
  
player.bank += temp.amountToDeposit;

Weapon ATM
PHP Code:
function onCreated() {
  
this.join("functions_bank");
}

function 
onActionServerSide(temp.cmdtemp.amount) {
  if (
temp.cmd == "deposit") {
    
this.deposit(temp.amount);
  }
}

//#CLIENTSIDE
function ChatBar.onAction() {
  if (
ChatBar.text.starts("/deposit")) {
    
triggerServer("gui"this.name"deposit"player.chat.substring(9).trim());
  }

The example above shows a very simple use of classes.

When you use join(classname) on a weapon or an NPC, imagine the script of the class being copied and pasted into the weapon. You can't pass things to a class; classes extend an object. They aren't their own objects.
In general I don't think there's a difference between "join" and "this.join", but in some cases you have a reference to a player, NPC, or weapon, and want to join it to that. For example, it's valid to write something like findNPC("MyNPC").join("my_class");. Personally I always used the this as I think it's a bit more explicit.
__________________
Reply With Quote
  #3  
Old 07-30-2017, 04:32 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
1. You can change the code without changing the level.
2. You can re-use the code elsewhere much easier.
3. You see compiler/script errors right away in RC.
__________________
Quote:
Reply With Quote
  #4  
Old 07-30-2017, 09:20 AM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
damn chris out of nowhere
__________________
Reply With Quote
  #5  
Old 07-30-2017, 11:19 AM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,743
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
Quote:
Originally Posted by cbk1994 View Post
Post
I see how the class works, but I don't understand what is happening in your post so bear with me.
HTML Code:
function deposit(temp.amountToDeposit) { 
} 
I understand that "deposit" is a custom function. Is it being called by the triggerserver command? And how is "temp.amountToDeposit" assigned a value? I assume triggerserver is passing the value of the chat, similar to how tokenize worked in GS1, to the function?

Does including the variable name in the function act similarly to assigning the variable value? Like "temp.amountToDeposit = player.chat.substring(9).trim());" ?

HTML Code:
function onActionServerSide(temp.cmd, temp.amount) { 
  if (temp.cmd == "deposit") { 
    this.deposit(temp.amount); 
  } 
} 
Again, how does "temp.cmd" and "temp.amount" get assigned values? In the deposit function I can sort of understand that triggerserver calls the function. Does "onActionServerSide" also catch the values that are being sent? Maybe it would help if you posted the format of how triggerserver is to be used.

Quote:
Originally Posted by fowlplay4 View Post
Post
Thanks, these were things I didn't really consider. So mostly join is used for convenience?
__________________
Save Classic!

Last edited by maximus_asinus; 07-30-2017 at 12:20 PM..
Reply With Quote
  #6  
Old 07-30-2017, 05:43 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
Quote:
Originally Posted by maximus_asinus View Post
I understand that "deposit" is a custom function. Is it being called by the triggerserver command? And how is "temp.amountToDeposit" assigned a value? I assume triggerserver is passing the value of the chat, similar to how tokenize worked in GS1, to the function?
It's called by the serverside half of the ATM weapon:

PHP Code:
    this.deposit(temp.amount); 
Because the weapon has joined functions_bank, the deposit function is available inside the weapon without copy-pasting it. It gets called at the "this.deposit(temp.amount)" line, after the trigger is received on serverside from the client.

Quote:
Does including the variable name in the function act similarly to assigning the variable value? Like "temp.amountToDeposit = player.chat.substring(9).trim());" ?

Again, how does "temp.cmd" and "temp.amount" get assigned values? In the deposit function I can sort of understand that triggerserver calls the function. Does "onActionServerSide" also catch the values that are being sent? Maybe it would help if you posted the format of how triggerserver is to be used.
When you use a trigger from clientside to serverside like this, the arguments passed to the function here (after "gui" and the weapon name):

PHP Code:
triggerServer("gui"this.name"deposit"player.chat.substring(9).trim()); 
...end up as the arguments to onActionServerSide function on serverside. So temp.cmd ends up as "deposit", tmp.amount ends up with the value of "player.chat.substring(9).trim()".

You're right that substring is similar to tokenize, it could alternatively be written:


PHP Code:
triggerServer("gui"this.name"deposit"player.chat.tokenize()[1]); 
The difference is that tokenize breaks up the line of chat into words, whereas substring takes everything after the ninth character. Tokenize is probably actually a better choice here anyway.

So the flow in this script is [play chats clientside] => [trigger is sent from clientside to serverside] => [trigger arrives in onActionServerSide block] => [onActionServerSide block calls this.deposit].

Triggers are a quirky feature of GScript (and I probably should've picked an example that didn't use them -- sorry), there are some good explanations at http://www.graal.net/index.php/Creat.../Helpful_Posts and in particular this post: http://forums.graalonline.com/forums...84&postcount=6

Quote:
Originally Posted by Crono View Post
damn chris out of nowhere
<3
__________________
Reply With Quote
  #7  
Old 07-30-2017, 06:47 PM
maximus_asinus maximus_asinus is offline
RIP DarkCloud_PK
Join Date: Oct 2001
Location: Canada
Posts: 3,743
maximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond reputemaximus_asinus has a reputation beyond repute
Thank you for coming back and clarifying, though the confusion was mostly my fault as I misread the original script. And now I know a little more about triggers, so I'm better now for it.
__________________
Save Classic!
Reply With Quote
  #8  
Old 07-30-2017, 09:38 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is just really niceMysticalDragon is just really nice
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
One thing I think was not mention which is personally my favorite is having these "utility functions" in one centralized place. Using Chris bank example, If I wanted to add lets say taxes to my bank deposits I would only have to modify that one class oppose to 50 NPCs that are not joined to this class.
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 08:30 PM.


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