Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Which is better?

Cyrus Odets
Registered User
Join date: 5 Oct 2005
Posts: 51
01-09-2006 11:40
Need a bit of advice here on which 'method' of doing things is better in the following situation...

Which is better? To use Global Variables and not have to pass the information in link message (Thus having it always available) or to use local variables and have to pass the information back and forth in link messages to retain access? (See example below)

Lets say you have 2 scripts that communicate via link message. In Script #1 you perform a particular function that needs to go out and have Script #2 do some stuff before Script #1 can finish what it was doing.

I'm going to make up an example here to illustrate my question:

In Script #1 you are in a function called "SetValues". This function does a search on a list to find a given list entry, and then records the index of this entry in a variable called "listIndex". You want to set that index position in the list to the value "15"...whether or not to SET this index position to "15" is determined by certain conditions, and those conditions must be resolved in Script #2. So you call Script #2 with a link message and Script #2 does its thing....and in this example, Script #2 decides that you should indeed set the position in the list indicated by "listIndex" to "15".

So now that Script #2 is finished, it calls back to Script #1 using a link message. But upon returning to Script #1...we don't return to the "SetValues" function where we left off....instead we arrive in the link event-handler of Script #1.

At this point, we need to finish setting the value at listIndex (Which was determined in "SetValues";) to the value "15" (Which was also determined in "SetValues";).

So here is the question...

If the variables "listIndex" and the value "15" were variables LOCAL to the "SetValues" function....the only way to hang onto them so that after Script #2 you can STILL set them and STILL have access to them would be to pass those values in the link message to Script #2, and then have Script #2 pass them back again to Script #1 where they will be handled in the link event.

Or....

You could make those same variables "Global" and not have to worry about passing them back and forth between scripts (Especially when Script #2 has no use for those values and just ends up passing them back to Script #1). With those values as "Global" variables they are easily available from the link event where you return to Script #1 at.

Global variables would chew up more memory sitting declared up there to be used whenever, by anyone.

Also....what I don't understand completely...is what if several things happen simulataneously in the script and those global variables must be 'shared'. In the above example...lets say that the above described operation is fired off by an avatar "Touching" the object. Upon touching that object a global variable is set to the Toucher's Avatar Key and that value then goes on to be used by other functions.

What happens if two avatars 'touch' the object in a very short period of time? If the variable is global....is it possible that a subsequent touching avatar's key will replace the key stored in the global variable WHILE it is still being processed?

If a global variable "avatarName" is set to "John Doe" as soon as John touches the object. After setting "avatarName" to "John Doe"....that variable goes on to be used in a bunch of other processing. What happens if "Jane Doe" touches the object before the script is done processing "John Doe"? Will it reset "avatarName" to "Jane Doe" before its done handling John?

I know all of the above was pretty confusing...so please forgive me for any confusion. Hopefully it was enough to get my question across :) Thanks ahead-of-time!
Fenrir Reitveld
Crazy? Don't mind if I do
Join date: 20 Apr 2005
Posts: 459
01-09-2006 13:24
As far as I can tell, SL is entirely event/queue based, and therefore all scripts act as if they are single-threaded. In your example about two people touching an object at once, what happens is SL accumulates the touch events for that time frame and then send it to that object in the form of touch_XXX events. So it is impossible that the values for the llDetectedXXX functions will change while you are in the middle of your touch() function.

You've noticed that integer parameter to touch right? That indicates how many have touched your object, within a single time slice in SL's event queue. If you loop through that number, you are guaranteed to hit all the people that touched that object -- even if they all clicked on it at once. Also this data will not change while you are processing the touch event.

As for your first question, I am not sure I understand what you are getting at. Use of global variables usually boils down to three things; Accessability, memory and speed concerns. Globals decrease code readability and uglify code logic (based on SL's state method), but sometimes are necessary if you to communicate values between different script states. They would also be necessary if you wanted to save a value that might come from a link_message() to access it later in that same event, or elsewhere in your script.

Otherwise, globals consume heap at all times -- even if you aren't using them, so they can lead to waste of memory. However, while I haven't tested this to provide any actual timing data, I am sure it's faster to re-use a global variable than it is to create/teardown that local variable in the middle of some looping event. So, using globals gain you speed if you're in the middle of a tight loop.

I assume what you are asking is if it is better to use globals or is it better to have a second script to store those variables by passing via link_messages()? If so, depends. If you are tight on script memory, storing data in another script might be your only choice. Otherwise, global variables would be more efficient. There are time penalties for sending link messages between scripts.