Common Scene Variables between Scenes/Levels

How do I…

Create a set of Scene Variables that can be set on selected Scenes/Levels so that all Levels have the same set of Scene Variables to work with.

What is the expected result

I was hoping to find that I could add and extension or something to a scene and it would then make a set of variables available to anything in the scene.

For example:

Each of the playable levels in my game will have multiple tiles on it. Some levels will have only a few tiles and others will have many tiles. I want to be able to get the bounds of all the tiles in the scene dynamically when the scene begins and set variables that store the bounds of the tiles in the scene so that I can then use those boundary variables to set the zoom on the camera so that all tiles fill the view.

Is there a way to do this?

Scene variables only exist while the scene exists. Once the scene is closed, they’re gone.

Two ways to work around this.

  1. Use global variables.
  2. Write the information to storage when before the scene ends, and read it at the start of the scene.

Thanks MrMen.

Sorry if I wasn’t clear. It is not the variable values that I want to share between Scenes it is the variable themselves. For example, if I want to define that all scenes in a certain group should have a complex variable called Bounds which has children Top, Bottom, Left and Right. So, if I create a new Scene I know that it will already have Bounds.Top and Bounds.Left and so on.

It like having each scene have inherited values or said another way, implement an interface.

Right now my solution that I have one complex variable on each scene. When I create a new scene/level I need to copy the variable from one scene and add it to the new scene. I am hoping to avoid the copy step and also make it so if I want all scenes to now have a Score variable all I would have to do is edit it in one place and all the scenes would now also have a Score variable.

I am also looking into making my own complex objects (prefrabs) and thought maybe a solution was to make an object that I include in all scenes that has its own variables. Let’s call it a SceneController object. Each scene would have its own instance of the object but I would be able to add new variables or functions or behaviors to the SceneController and immediately all instances of the SceneController across all the Scenes have the new capabilities.

But maybe I a missing something.

You don’t need to declare variables in order to use them, that’s just a mere formality. Therefore, when you assign a value to a variable or try to read it, even if it hasn’t been previously declared, you are creating it at that moment. This allows you to use global variables across scenes without the need to declare them.

Right, so what you’re after is a type of variable template?

You could use a global variable for that. Define the structure in the global variable, but never add data to it. To create the scene variable, use the convert JSON to variable action, and use the global variable template JSONified as the value.

Something like:

with the event:

It’ll create the scene variable structure with default values.

1 Like