0
Under review

Create local variable at runtime

Einar 4 months ago in Game Creator updated by Marti (Lead Developer) 3 months ago 4

Is it possible to create a local variable from code at runtime? Not setting the value, but defining the name and object type? Basically I have a combination of scripts and Game Creator components that together control basic gameplay functionality like opening doors and drawers, enter/exit vehicles, attach/detach from moving platform etc. They keep track of state etc by setting local variables on the game objects in question.

Instead of having to add the local variables game component and setting up the different named variables with type in the editor, I would like to do it at runtime. This way I do not have to go back to each single component if I have to make a change, or add some variable.


If it's not feasible then don't stress it, I'm just wondering if it can be done at runtime, and a nudge in the right direction of where to find some code to use as a base.

Unity version:
Game Creator version:

I have encountered such event in terms of quest marker creation , for every quest i need to make an arrow that points to the quest goal, of course the quest goal would change, hence what i did was create a global variable of game object type and call it whenever quest changes and inside the quest change action, initialize and assign the global variable with the quest goal game object. Single variable takes care of all the quest goals.

Under review

This is something I have in the bag of ideas for the next major iteration. On one hand, there's a lot of benefits that could be gained if local variables could be created at runtime (globals would be a bit more difficult). On the other hand, I'm not sure how these local variables could be of use. Let me explain:

When you create a Local Variable, in the Editor, you reference it from other game objects. Reference are static and don't change. What changes its their content. For example, when you create a "target" local variable, the player can use this to choose which object should he/she focus on.

So the question is more about: Does it make sense to have dynamic/runtime-creatable local variables? I'm open to hear use-cases and I think I can be easily persuaded.

Well, I would like the flexibility. In my case I often reference local variables in two ways:

1 - In an action using the Invoker or a GameObject variable as the owner of the variable, and manually entering the name of the variable

2 - In code, again calling the variable by string name


In both these cases the variable reference is found at runtime.

As for why I would like to create them at runtime as well, it's mostly about convenience, control and refactoring. If I create a behaviour script for let's say a breakable object, I would prefer to set up the variables from the Start() of the script, instead of having to add the local variable component manually, and entering every variable.Then I will only have to make changes later in the script (and in whatever triggers and actions that interact with the variables), instead of having to go over and change each and every one. Maybe I really like breakables, and have like hundreds of variations.

It can of course be mitigated by clever use of prefabs, but it's an option I think makes sense for those of us who like to complement Game Creator with code.

Thanks. I'll assess this and circle back when I've played a bit with the system. Nonetheless, making it so you can create runtime variables would be a sign of good architecture, so I'll keep this in mind.