Using GlobalState
GlobalStates are very easy to use in Cubern. The main purpose is to allow 2 or more number of scripts to easily access, modify and call functions from each other. Unfortunately, you cannot reference a script and execute functions from it like:
ClientScripts["ScriptName"].SomeFunction() -- wont work
ClientScripts["ScriptName"].Variable -- wont work
ClientScripts["ScriptName"].Variable = true -- wont work
so, in order to allow that we made GlobalState.
Seeking and Pushing a value of another script
GlobalState is a global key which means it can be used with Server and Client sided scripts.
Pushing
Pushing or updating a value, you are required to first declare the variable as global with a simple function. For this example lets create 2 scripts, Script1 and Script2. Great! Now lets declare a global variable in Script1:
-- This is Script1
GlobalState:Push("Dog", 3)
Here we declared a Dog variable which has the value of 3! We can store any type inside the GlobalState variables like Instances! Now we have finally created a variable which can be accessed by any script! We can update its value by re-declaring it! The engine will search for a existing value with that variable name and will automatically overwrite its value!
-- This is Script1
GlobalState:Push("Dog", 4)
Now Dog has a value of 4!
Seeking
Seeking or reading a value you registered in GlobalState is also very easy! It can be easily accessed through just a single function. As we declared a Dog variable, we will now read its value!
-- This is Script2
printl(GlobalState:Seek("Dog"))
This will print 4 because we first made the variable of Dog with value 3 and then updated it to value 4!
Seeking Functions
Just like seeking a value, you can also seek and execute any function even those which do not require to be registered through a function. Lets see a example! We will be using the 2 scripts: Script1 and Script2!
In Script1 we will be declaring a function:
-- Script1
function Foo()
printl("Hewwo!");
end
And then in Script2 we can do:
-- Script2
GlobalStage:SeekFunction("Script1", "Foo")
Now, when we playtest: on execution of Script2, it will tell Script1 to execute its function called “Foo”!