Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Passing a variable from one event to another

Landing Normandy
Proposing 4968
Join date: 28 Nov 2005
Posts: 240
09-19-2006 05:28
Here's a simple one for anyone who knows what they're doing:

How do I pass the value of a variable from one event to another? Here's an example of what I mean, not written in true LSL:

CODE
integer notecardline = 0;

default()
{

dataserver()
{
[get notecard data and save it to notecardline];
}

touch_start()
{
[do something with notecardline];
}
}


Very crude and I'm not sure if that demonstrates what I mean, but basically, when I define a variable at the start, then use it in one event, such as dataserver it will do something within that event but when I need to use it in touch_start or listen for example, the variable is empty again... how do I get it to remember and allow me to interact with it.

What I would like to do is to get dataserver to firstly tell me how many lines there are in a notecard, then using listen I'm interacting with a GUI, I need it to use the number of notecard lines to work properly. The GUI will choose which line I want and then I need to get that line out of the notecard again, presumably with dataserver.

The actual way to do this isn't in question here, I'm trying to solve that in another thread, all I need to know here is how on earth I get the variable from one event to another without it resetting to 0
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
09-19-2006 06:10
If it's declared globally like that it should persist across states, events, functions etc. So there may be something wrong with your original script.

e.g.
CODE
integer global = 0;

default
{
state_entry()
{
global = llCeil(llFrand(1000.0));
llOwnerSay("Setting global to " + (string)global);
state different;
}
}

state different
{
state_entry()
{
llOwnerSay("Changed state, global is now " + (string)global);
}
}

You should get it telling you that global is the same both times.
Landing Normandy
Proposing 4968
Join date: 28 Nov 2005
Posts: 240
09-19-2006 09:09
Ah, this is outside the DEFAULT{} that I assumed we needed to use, that makes it even more difficult for me! I assumed everything had to be within DEFAULT{}. When would I need to operate outside DEFAULT{}???

So much to learn, so little patience! ;)
Landing Normandy
Proposing 4968
Join date: 28 Nov 2005
Posts: 240
09-19-2006 09:12
Thinking about it I might already know the answer to that question! We're working with different states here yeah, rather than different events within the states? Am I really confusing myself or am I right there?

I think I can adapt my code to work like that, I'll try in a bit, thanks!
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
09-19-2006 10:50
The code that you wrote in your first post will do what you want.

CODE

integer xxx; // This is a global variable, it's always 'alive' and it always 'remembers' its value

default
{
dataserver(...)
{
integer yyy; // This is a local variable, it 'dies' once you exit the event handler

yyy = 0; // This value won't be around once the dataserver handler ends

xxx = (integer)data; // This value *will* stay around even after the event ends
}
}


So if you wanted a script that read the first line in a notecard, and than first line contained a number...

CODE

integer notecardData;

default
{
state_entry()
{
llGetNotecardLine("NotecardName", 0);
}

dataserver(key id, string data)
{
notecardData = (integer)data; // This assignment will be 'remembered', since it's a global variable
}

touch_start(integer num)
{
llOwnersay((string)notecardData); // This will have the data from the notecard in it
}
}