Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   The variable "i" (https://forums.graalonline.com/forums/showthread.php?t=59375)

Gambet 06-10-2005 10:03 PM

The variable "i"
 
There seems to be great confusion with this variable. I spoke with CheeToS about it, and it seems not even he is certain. I suppose no one has asked stefan about it (( >_< )). So, I guess we could have an open discussion on it.


So...

Is "i" a global variable which could/does conflict with other variables?


EDIT:


In a for loop, it is said that a this. variable would make it slower, and it is rather recommended to use "i" alone. But, it is also said that "i" conflicts with other variables. So, I think it would be best to find the true answer.

Rick 06-10-2005 10:05 PM

The behavior of any variable without a prefix is undetermined. Use temp.i.

Gambet 06-10-2005 10:08 PM

Quote:

Originally Posted by Rick
The behavior of any variable without a prefix is undetermined. Use temp.i.


That is not the question at hand.


EDIT:

It is rather pertinent, but temp.i is GS2, and I'm speaking of the variable in general.

Rick 06-10-2005 10:23 PM

Quote:

Originally Posted by Gambet
In a for loop, it is said that a this. variable would make it slower, and it is rather recommended to use "i" alone.

Where did you get the silly notion that this.i is any slower than i?

Gambet 06-10-2005 10:28 PM

From a source that would wish to be kept anonymous.

Rick 06-10-2005 10:33 PM

Quote:

Originally Posted by Gambet
From a source that would wish to be kept anonymous.

Why? That is pretty stupid.

Gambet 06-10-2005 10:36 PM

Yes, it is, but I respect this persons wish to remain anonymous.

Evil_Trunks 06-10-2005 10:54 PM

I do not think using the this. prefix will cause a speed problem. Perhaps the mystery person believes using this. is a waste of memory, because it saves a loop variable to each individual NPC for no reason.

The only problem from using i is especially apparent in GS2. If you are calling a function inside another function, and they are both using the i loop variable, this will screw up the scripts. I think temp.i is better to use for this reason. I would like someone to explain the temp. prefix further though.

CheeToS2 06-10-2005 11:03 PM

I said it to him; I told him that it is supposedly slower, because I recalled somebody else telling me that. I did not know if (or why) it was true or not. Sorry for the confusion :)

Anyway, this was discussed on #gscript on IRC for a little while. Here's a log:


<CheeToS> there is some confusion about using the variable "i" in for loops
<Jacco> There is?
<fry> there always is confusion
<VeloxCruentus> i = global var. They conflict with other NPCs.
<CheeToS> some say you should use this.i to avoid conflicts
<Rick> VeloxCruentus: four years ago
<fry> yep, non this. vars don't even work in gani scripts :(
<Bennity> How did the variable i come along anyway
<Bennity> It seems to be used for temp things
<Rick> iterator
<fry> it's small
<Jacco> integer?
<Jacco> oh.
<fry> and that :D
<Bennity> ah
<CheeToS> what is your opinion, Rick?
<Bennity> I thought it stood for something
<Rick> my opinion is to use temp.i
<CheeToS> lol
<fry> same here
<CheeToS> without GS2
<Bennity> In fact, there is a thread on the Graal forums from months and months ago about the use of i
<Rick> still, temp.i
<Bennity> and what it means
<Jacco> there's a temp.i?
<Jacco> I think all single-letter variables should be declared temp.
<ben> this.i only if sleep in loop.
<ben> kai smacks you if sleep in loop.
<ben> -> no this.i
<Jacco> lol
<Jacco> I sleep in the loop.
<ben> CheeToS: How else is it going to get screwed up?
<ben> Hm. Perhaps if you call a function that also modifies i.
<ben> temp.i wins I suppose.
<fry> other scripts using i at the same time
<Rick> ben: aye
<CheeToS> velox says he has had problems with other level NPCs changing i
<ben> Rick: Is temp.i local to the stack frame?
<fry> but that's not possible so wooh

Kaimetsu 06-11-2005 01:10 AM

unprefixed variables are shared between all scripts. It is unwise to rely on them retaining their values between frames.
this. variables are specific to the script. Their values will be constant between frames, but you don't need that to be the case for simple iterators
temp. variables are specific to each execution of the script in which they are called, like local vars in C. They should be used if you need to use recursion, or if you think you might use vars with the same name concurrently.

Benm00t 06-11-2005 01:53 AM

Quote:

Originally Posted by CheeToS2
I said it to him; I told him that it is supposedly slower, because I recalled somebody else telling me that. I did not know if (or why) it was true or not. Sorry for the confusion :)

Anyway, this was discussed on #gscript on IRC for a little while. Here's a log:

*Log*

So the conversation was about "this.". Ah... I don't think I got that at the time. :rolleyes:

Admins 06-14-2005 12:56 PM

In old scripting:
On serverside variables without prefix are only valid inside the same script (npc script, weapon script, class script), on clientside they are "global".

New scripting:
All variables without prefix are global, except function parameters which are automatically made temp. variables. It is planned that there will be an option to make all variables without prefix temp. variables. That option will not be enabled by default because it could break scripts, since in the old scripting engine such variables are often used to pass parameters to functions (although this. variables are better for that if you want to pass parameters to functions inside other scripts/classes).


All times are GMT +2. The time now is 08:07 PM.

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