We’re introducing Meta accounts in August 2022: a new way to log into VR devices that doesn’t require a Facebook account. Learn more

VARIABLE PERSISTENCE

When a script transfers ownership, its variables reset to their initial value. However, sometimes a variable needs to persist between ownership transfers. This is typical when an object is handed from one player to another and a variable needs to be the same for both players. As an example, the amount of charges remaining in a laser tag device should stay the same when a player hands the device to another player. To ensure that the variables keep their value when the ownership changes, a default script needs to keep track of those changes. When the world start event runs, send an event named ‘get’ to an object variable. That object would be running the persistence script to store the variables. When the ‘get’ event is received, send a ‘set’ event back to the object with the variables that need to be persistent. The initial value of the variables can be set in the variables tab of the persistence script. When the ‘set’ event is received, set the value of the variables to the parameters that were passed. Any changes made to the variables while the object was grabbed or attached need to be saved in the persistence object when the object is released or unattached. In the ‘when object is released’ event, send a ‘save’ event to the persistence object with the variables as parameters. When the ‘save’ event is received in the persistence object, set its variables to the parameters. Any event in the local script that changes the variables while the object is not grabbed also needs to send the ‘save’ event with the variables.

PREREQUISITE READING

WHEN WORLD IS STARTED precedes this tutorial


Seen above is the script used for the Laser Tag Device.
Seen above is the script for the Chargers Manager.

Next Up