Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Globally scoped variable not updating - ideas?

Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 01:44
Hi all.

I have a odd one which may well be something i can't see - but after extensive testing (lots of OwnerSays and swapping code around) the issue persists.

I declare a global variable in this script at the top along with some variables to read a notecard (taken from the wiki - thanks!)

CODE

// Read out a complete notecard from the object's inventory.
// name of a notecard in the object's inventory
integer gLine = 0; // current line number
key gQueryID; // id used to identify dataserver queries


// **** this is the variable ****
integer numadverts;

list adverts = [];
integer stride = 4;

//more code, state entry, touchs etc.....


I then do the notecard read process - which in itself works just fine, and echo's to screen as I would expect, stores in a list as I would expect etc etc.

However - the "numadverts" variable will not increment in the global scope - despite reporting via OwnerSay that its incrementing correctly whilst in the dataserver event.

Heres what I use for the dataserver event.

CODE


//we come to this after a GetNoteCardLine() event...

dataserver(key query_id, string data) {
if (query_id == gQueryID) {
if (data != EOF)
{ // not at the end of the notecard
//place the line into the list

//this line looks for comments
if(llGetSubString(data,0,1) != "//")
{
adverts += llParseString2List(data,[";"],[]);

//now increment the number of adverts
numadverts ++;
}
++gLine; // increase line count
gQueryID = llGetNotecardLine("_config", gLine);
}

}


What is truly weird is that (I think) gline increases its count... or perhaps it doesnt! Is the scope of an incremented global variable in an event local only?

I need the "numadverts" variable elsewhere to affect a strided list so its quite important...

I am sure I have changed global variables from within events before - in fact I know I have, so is there something about the dataserver event thats different? OR am I being a complete numpty?

I appreciate any suggestions.

Cheers

Erwin
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
03-08-2007 02:10
Global variables should update ok no mattter where.
Check you havent inadvertantly duplicated the variable name locally.
Or that you dont reset it

if(numadverts = 0)

instead of

if(numadverts == 0)
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 03:31
From: Newgate Ludd
Global variables should update ok no mattter where.
Check you havent inadvertantly duplicated the variable name locally.
Or that you dont reset it

if(numadverts = 0)

instead of

if(numadverts == 0)


Hi Newgate

Thanks for the reply, the variable numadverts wasnt declared or used anywhere else except in a LLOwnerSay..

I suspect though - after removing most of the script to drill down that the issue is down to timing...

If I put the OwnerSay into a touch event - and wait a couple secs then the variable appears filled, however if I put the OwnerSay on state_entry (after the notecard pull) it fails with "0"... I even tried putting a short sleep in the script just after the NotecardLine pull - but I guess that stops the entire script including the dataserver event!

According to the wiki theres no way to find if a dataserver event is completed apart from running a timeout test... so I am guessing that needing to change the initail parameters of my script based on a NotecardPull is going to be only possible if I make a function that only works if the numadverts variable is actually > 0..

Ho hum..

Erwin
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-08-2007 03:43
From: Erwin Goldblatt
Hi Newgate

Thanks for the reply, the variable numadverts wasnt declared or used anywhere else except in a LLOwnerSay..

I suspect though - after removing most of the script to drill down that the issue is down to timing...

If I put the OwnerSay into a touch event - and wait a couple secs then the variable appears filled, however if I put the OwnerSay on state_entry (after the notecard pull) it fails with "0"... I even tried putting a short sleep in the script just after the NotecardLine pull - but I guess that stops the entire script including the dataserver event!

According to the wiki theres no way to find if a dataserver event is completed apart from running a timeout test... so I am guessing that needing to change the initail parameters of my script based on a NotecardPull is going to be only possible if I make a function that only works if the numadverts variable is actually > 0..

Ho hum..

Erwin


What I do is do the notecard reading in the default state, then when it's completed I move to a "running" state, which is where I do all of my actual work. That way, I know that everything inside the running state can assume that the notecard has been read successfully.

Any work that needs to wait until after the notecard has been read can be done in the state_entry event of the running state.

This also has the advantage of allowing me to change where I get the configuration from - as long as it ends up parsed into the same lists and setting the same count variables, I can read the config via http, via my object description etc and not have to touch my main code.
_____________________
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 03:49
From: Stephen Zenith
What I do is do the notecard reading in the default state, then when it's completed I move to a "running" state, which is where I do all of my actual work. That way, I know that everything inside the running state can assume that the notecard has been read successfully.
QUOTE]

Hi there.

I sort of just realised that if I tested for EOF and updated a variable which was tested in a loop then that might be the answer - which I guess is what you do - test for a changed variable (ie: the end of the dataserver event) and then proceed.

Is there any danger in doing this? Ie: with the possibility of an endlessly running loop... would I be safe to put in a timeout on the loop for instance?

Yes?

Cheers

E
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-08-2007 04:01
From: Erwin Goldblatt
From: Stephen Zenith
What I do is do the notecard reading in the default state, then when it's completed I move to a "running" state, which is where I do all of my actual work. That way, I know that everything inside the running state can assume that the notecard has been read successfully.
QUOTE]

Hi there.

I sort of just realised that if I tested for EOF and updated a variable which was tested in a loop then that might be the answer - which I guess is what you do - test for a changed variable (ie: the end of the dataserver event) and then proceed.

Is there any danger in doing this? Ie: with the possibility of an endlessly running loop... would I be safe to put in a timeout on the loop for instance?

Yes?

Cheers

E


Essentially it's the same thing, you're just using a variable to maintain the state whereas I'm using the language features.

A timeout on the dataserver events doesn't sound like a bad idea in general - do a llSetTimerEvent before getting the first notecard line, then if the timer event triggers maybe log a message and stop trying to read the notecard.

The beauty of using a different state is that it means you don't have to continually check the variable - it will automatically move into the main code if and only if you have finished reading the notecard. But I know some people don't like using different states.
_____________________
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 04:24
Absolutely no problem using different states.. scenes in Flash spring to mind :)

I didnt however realise that the dataserver event would hold up the state transition - which if it does is a "wow" feature for what I am thinking!

So what you are suggesting (if I have it right) is that the transition to another state after a call to the dataserver event will ONLY take place after the dataserver event is completed?

This sounds much more efficient than my looping idea.

Erwin
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-08-2007 04:31
From: Erwin Goldblatt
Absolutely no problem using different states.. scenes in Flash spring to mind :)

I didnt however realise that the dataserver event would hold up the state transition - which if it does is a "wow" feature for what I am thinking!

So what you are suggesting (if I have it right) is that the transition to another state after a call to the dataserver event will ONLY take place after the dataserver event is completed?

This sounds much more efficient than my looping idea.

Erwin


Yes, state transitions are totally under the control of the script. You can just do a "state running;" (or whatever) and the script moves into the new state, calling the state_exit event of the old state and then the state_entry event of the new state on the way. So you can make it so that your script only changes into this state once you know you've successfully read the notecard.
_____________________
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 04:33
I just did a bit of digging on the wiki;

***** PASTE *****

When the interpreter reaches a state change statement, the event containing the state change is immediately ended. If the current state has a state_exit event, this is executed before the state change takes place. Note: previous versions may have allowed the event containing a state change to complete before the state change takes place, but this is no longer the case.

When a state changes, all pending events are cleared, and all events that require setup (via a function) are defaulted (disabled).

****** PASTE *****

Ooooer, isnt that what was suggested in the post above?

Kick me if I misunderstand your comment please!

Erwin
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-08-2007 04:55
From: Erwin Goldblatt
I just did a bit of digging on the wiki;

***** PASTE *****

When the interpreter reaches a state change statement, the event containing the state change is immediately ended. If the current state has a state_exit event, this is executed before the state change takes place. Note: previous versions may have allowed the event containing a state change to complete before the state change takes place, but this is no longer the case.

When a state changes, all pending events are cleared, and all events that require setup (via a function) are defaulted (disabled).

****** PASTE *****

Ooooer, isnt that what was suggested in the post above?

Kick me if I misunderstand your comment please!

Erwin


Yes, that pretty much covers what I said - the only bit I missed was that when you do a state change, the rest of the event & any functions you're in are going to get ignored.
_____________________
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 05:01
**LIGHT BULB!**

Got it... suddenly light dawns... test for EOF and then change state... gotcha gotcha .

Thats SO simple and obvious, thanks so much! Gets around loads of issues.

Erwin
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-08-2007 05:09
From: Erwin Goldblatt
**LIGHT BULB!**

Got it... suddenly light dawns... test for EOF and then change state... gotcha gotcha .

Thats SO simple and obvious, thanks so much! Gets around loads of issues.

Erwin


Ah, yes - sorry, I thought about posting a bit of example code but didn't get round to it!

Depending on what you're actually going to be doing, you could always verify that you have a valid config as opposed to a non-empty config. So, for example if certain things in the config were prerequisite and you haven't seen them before hitting the EOF, don't go to the running state (but still give them a way to re-read the config).

Speaking of rereading the config, it should be as simple as going back to the default state, possible clearing your variables first. I usually do it if the inventory of the object has changed, which would happen if the config notecard were altered.
_____________________
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-08-2007 12:43
Hey thanks for all the assistance, I went off and coded up the script with the switch state in mind and its real good now.

Doing as suggested I put a "found EOF" test in there with a timer event that checked if we were still trying after 10 seconds and if so then reset the script and had another go.

Of course if EOF was found then we toodled off to a new state and all that preceded was forgotten about.

Wonderful - neat and easy to debug.

Thanks!!!

Erwin