Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   What is the purpose of "join"? (https://forums.graalonline.com/forums/showthread.php?t=134270524)

maximus_asinus 07-30-2017 02:24 AM

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?

cbk1994 07-30-2017 04:29 AM

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 (Post 1668517)
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.

fowlplay4 07-30-2017 04:32 AM

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.

Crono 07-30-2017 09:20 AM

damn chris out of nowhere

maximus_asinus 07-30-2017 11:19 AM

Quote:

Originally Posted by cbk1994 (Post 1741690)
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 (Post 1741691)
Post

Thanks, these were things I didn't really consider. So mostly join is used for convenience?

cbk1994 07-30-2017 05:43 PM

Quote:

Originally Posted by maximus_asinus (Post 1741693)
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 (Post 1741692)
damn chris out of nowhere

<3

maximus_asinus 07-30-2017 06:47 PM

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.

MysticalDragon 07-30-2017 09:38 PM

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.


All times are GMT +2. The time now is 07:04 AM.

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