Are global variables possible in Cospaces?


#1

Hi All,
I was just adding finishing touches to my space in order to show to the teachers at my school as a demo but came across an interesting discussion with my supervisor which were about global variables (variables that can be transported from one file to another). Considering this is possible with regular java script, I was wondering if this was possible with cospaces and if so how would one go ahead to write the script. The aim of the script would be that the opening scene has a num pad in which the player can input their height. The height is then stored as a variable and transported to all scenes and the y coordinate of the height is applied to the camera providing a more realistic and individualistic vr experience for my teachers (varying in height between 1.5m and 2.0m). Any suggestions ?


#2

Hi Luc,

this can be achieved using Space.setProperty(propName, propValue) and Space.getProperty(propName) functions, where both propName are propValue are strings.

Benny made an example here.


#3

Hello Pavel,
Thank you so much for replying, and I have tried the example linked. My questions is does this also work with variables? When I set the property I used the Javascript block and wrote this:
Capture
My aim is to transport the variable from the keypad (in which you input your height) to then affect the height of all cameras in spaces. Could you please advise ?
Thank you so much


#4

Sure it works with variables as well, but on your screenshot you set property with the name Height and value FinalHeight. Instead of this you want Height property to have value stored inside FinalHeight variable and not FinalHeight literally (a string).

To achieve this remove single quotes around FinalHeight inside your Space.setProperty call. It should be like this: Space.setProperty('Height', FinalHeight)


#5

Hi Pavel,
Thank you so much, IT WOOORRKKSS. Who would have known that a pair of quotation marks could mess everything up.
If you want to experience the space here it is (though I still need to add a lot of instructions for most scenes)

For the first scene though, where the global variable applies, just enter your height (in metric units im still working on imperial). In my case I measure 1.86 meters so I would just type 186 into the keypad press confirm and you are off to the next scene and the camera will be 1.86 meters from the ground. Will be great for elementary kids in which will not feel like giants :slight_smile:

Quick Side note: This only works for the VR playground scene/ second scene for the moment but the concept works


#6

Glad that it worked, but remember these properties can only store strings. So on the second scene Space.getProperty returns “1.86” as a string, not as a number.

Although your particular script is completely fine, in general be careful with data types in your programs.

Take a look at this example:

Space.setProperty("prop", "123");
var storedValue = Space.getProperty("prop");
storedValue = storedValue + 1;
Space.log(storedValue); // this will log `1231`

storedValue here contains a string and not a number, so adding 1 to string 123 will produce another string 1231 and not number 124 as you expect.

To cast string that you’ve read from a property to a number you can use Number function:

Space.setProperty("prop", "123");
var storedValue = Number(Space.getProperty("prop"));
storedValue = storedValue + 1;
Space.log(storedValue); // this will log 124