Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Variable Scopes? (https://forums.graalonline.com/forums/showthread.php?t=61780)

jake13jake 10-17-2005 07:20 AM

Variable Scopes?
 
Okay, I am a little bit confused about variable scopes, especially in gs2.

I know:
this
player
npcs[]
players[]
client/r
server/r
var = getPlayer or findPlayer or something and then that makes a new prefix.
etc.

are there other variable prefixes? I mean, I just don't know.
and also, what about non-prefixed vars? Are they clientside global temp? Confirm this or correct me.

Okay, the other thing that is much confusing me. How in the world does passing variables within an NPC to another function work? I mean, do param named variables stay local to their function...? It's just so confusing.

Skyld 10-17-2005 12:59 PM

NPC Code:
var = findPlayer(account);


"var" becomes the prefix to player "account", i.e. var.chat

Also, about passing parameters to functions.

NPC Code:
function onCreated()
{
echo(findNPC("foo").myFunction("zombies", "ninjas"));
}


NPC Code:
public function myFunction(var1, var2)
{
return("The first variable is equal to '" @ var1 @ "' and my second is equal to '" @ var2 @ "'.");
/*
In effect:

var1 = "zombies";
var2 = "ninjas";
*/
}


In the first NPC, the expected parameters are variables "var1" and "var2", so when the function is given parameters, "var1" takes the first parameter and "var2" takes the second.

The variables passed to the public function will remain local to that function in the other NPC (as in, you cannot access the parameters given to that public function in the script calling the public function)

jake13jake 10-17-2005 05:34 PM

In the first NPC, the expected parameters are variables "var1" and "var2", so when the function is given parameters, "var1" takes the first parameter and "var2" takes the second.

The variables passed to the public function will remain local to that function in the other NPC (as in, you cannot access the parameters given to that public function in the script calling the public function)[/QUOTE]

So functions passing variables should be kept in different NPCs?

Skyld 10-17-2005 06:42 PM

Quote:

Originally Posted by jake13jake
So functions passing variables should be kept in different NPCs?

What do you mean?

jake13jake 10-17-2005 07:49 PM

NPC Code:

//ex.
function onCreated() {
dx = 2;
dy = 5;
setTimer(5);
}

function onTimeout() {
distance = inaccurateDistance(9,20);
}

function inaccurateDistance(dx,dy) {
dx-=2
dy-=2
return (dx^2+dy^2) ^0.5;
}



Would the values of dx and dy set in onCreated change in that script because they are set by receiving parameters? Would they change within the function inaccurateDistance?

What would be really nice to hear is that the variable scope of function params remained local to the function.

ApothiX 10-17-2005 09:49 PM

Quote:

Originally Posted by Skyld
The variables passed to the public function will remain local to that function in the other NPC (as in, you cannot access the parameters given to that public function in the script calling the public function)

I think he already answered that question?

jake13jake 10-17-2005 09:53 PM

Quote:

Originally Posted by ApothiX
I think he already answered that question?

well, actually, does the function he speaks of come out of a different NPC or is it within the same NPC as when it is called, It's just his wording confuses me.

Lance 10-18-2005 01:55 AM

Quote:

Originally Posted by jake13jake
What would be really nice to hear is that the variable scope of function params remained local to the function.

That's what he said...?


All times are GMT +2. The time now is 03:25 PM.

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