Global Variables by KyleFlynn
This script contains a series of functions that can be placed within other scripts to allow for global variables. These global variables allow for variables that can be used across different scripts. These scripts can all be in the same prim, or they might be in different prims of a linkset.
This script does NOT create variables that are shared across separately rezzed objects (separate linksets). That can only be done with inter-linkset channel communications.
These scripts use the root prim's description for storing these global variables. These scripts work in many different conditions: a single prim, a linkset of prims, a prim or linkset with seated avatars, and/or a linkset that is attached. However, the fact that the description is used means that these global variables do not survive re-rezzing of the object. Everything starts over when the object is pulled from inventory and re-rezzed (in-world or attached).
The root prim's description is limited to 127 bytes. Therefore, 127 characters is the maximum amount of global storage you get. These scripts also limit the storage to ASCII characters, but this includes numbers, vectors, and rotations.
There are basically three ways to use the scripts, and I'll outline all three.
1) The simplest is to use the SaveGlobalSubString & GetGlobalSubString functions. These both work very much like the llGetSubString function with a iStartIndex & iEndIndex. If we remember that we've got 127 bytes (characters) of storage, we can just use these two to save and fetch whatever data we like, keeping track of where it is in those 127 bytes.
Also, there's a ClearGlobals() function which can be used with any of the three methods. This just clears out all the global variable information.
Summary:
SaveGlobalSubString(integer iStartIndex, integer iEndIndex, string Value)
string GetGlobalSubString(integer iStartIndex, integer iEndIndex)
ClearGlobals()
2) The second way to use these scripts is to use the SaveGlobal, GetGlobal, & DeleteGlobal functions. For the SaveGlobal, you supply a variable name (VarName) and value (Value), and it stores the data. With this approach, you don't need to worry about the position within the root prim's description that the data is in. However, you must provide variable names. You should keep these variable names short so you don't overrun your 127 bytes.
Use SaveGlobal, with your VarName and your Value placed in a single-item list, to save your data.
Use GetGlobal, with the VarName, and it'll return a single-item list with the data you saved (somewhat similar to using llGetLinkPrimitiveParams). However, it's always returned as a string-type-list-item, and it's up to you to re-typecast into what you need.
The DeleteGlobal works much like GetGlobal, but it doesn't return anything, and deletes the global variable's data from the root's description.
Summary:
SaveGlobal(string VarName, list Value)
list GetGlobal(string VarName)
DeleteGlobal(string VarName)
ClearGlobals()
3) And the last way to use this is with a set of wrappers for the approach outlined in approach #2. These wrappers should be straightforward from their names. And, rather than requiring and returning a single-list-item, they require and return the type you're using. That way, you don't have to worry about making things into a list. Here are the declarations for the wrappers:
SaveGlobalInteger(string VarName, integer Value)
SaveGlobalFloat(string VarName, float Value)
SaveGlobalString(string VarName, string Value)
SaveGlobalVector(string VarName, vector Value)
SaveGlobalRot(string VarName, rotation Value)
SaveGlobalKey(string VarName, key Value)
integer GetGlobalInteger(string VarName)
float GetGlobalFloat(string VarName)
string GetGlobalString(string VarName)
vector GetGlobalVector(string VarName)
rotation GetGlobalRot(string VarName)
key GetGlobalKey(string VarName)
And don't forget ClearGlobals()
Also DeleteGlobal(string VarName) still works when using this wrapper approach.
Also, with any of these approaches, you can examine the root prim's description in the Edit Window to see what they're doing.
Happy LSL Scripting,
KyleFlynn