Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Hit a weird bug(?) with variable names

Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
09-11-2005 12:15
I had a string variable called "name" in my script. It compiled fine, and didn't change color like it was a reserved keyword. But while running, the variable's value would get reset to "" on its own. I checked this by putting llSay's around every place I was seting the value. And also, changing the variable to agentName fixed the problem.

Does anyone know what might have happened? If this is a documented issue, that would be good to know. If anyone else can duplicate it, that would be useful too. Basically, the script went something like this. A listen picked up the name, then looked around for it in a sensor. A pretty simple targeting/following system.

CODE

string name;

default
{
...

listen(...)
{
name = message;
llSay(0, "Name is " + name); // This prints correctly

// llSensor for agents
}

...

sensor(...)
{
llSay(0, "Name is " + name); // Here it prints an empty string

for (i = 0; i < num; i ++)
{
// Look for name in the llDetectedNames - that's what I wanted to do,
// but it didn't work
}
}

...

}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-11-2005 13:33
By any chance is your header for the listen event the default?
CODE

listen(integer channel, string name, key id, string message)


The problem is when you set "name = message;" it sets message into the local variable and not the global.


DISCLAIMER: Bad coding practices discussed below, do not try this at home.

For my scripts i use leters for the event variable names, that way i never have conflicts with globals (unless i'm being lazy and used singles for globals too, which from time to time i will x_x ).

CODE

listen(integer a, string b, key c, string d)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
09-11-2005 13:54
I do the same as Strife, and always prefix globals with g.
As in, gName. That way you will never have scope problems again.
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
09-11-2005 16:49
Argh! Unbelievable!!!

*hangs his head and slinks away in shame*

Thanks :)
a lost user
Join date: ?
Posts: ?
09-12-2005 01:34
And Moses came down with the Ten Coding Commandments.

1) Thout shall use notation before variables, that namespace conflicts shall not be naughty in thine eyes.