[Solved] Instance Variables Vs object variables

Hello all,
can please someone explain me the differences from instance variable and object variables ?

If I create an object in run time I usually use object variable but if I drag an object on edit screen I can use instance variables. In events, I can’t find Instance variable; object and instance variables are the same thing ?


1 Like

Both are different!

In the object list you have the all objects in your current scene.
Object variables are variables added by edition of your object in this list.
(right click on an object then “Edit object variables”)

On the scene you drag and drop one object, one object on scene is called instance.
An instance is one or many duplication of your original object.
If you have one object “Player” in object list, and two “Player” on scene, so you have two instances of “Player”.

Then when you click on an instance you can edit the values of the selected instance, position, width,
variables, etc…
At the bottom you see the “Instance variables” section.

If you have setup an object variable on your object the variable and the value will be shown in this section.
This variable object can be override, if you edit the value of the variable in this section, the value go to italic, this mean you have write over the object variable, and this value is now effective only on the instance selected.

If you click on a second instance, you will see the object variable, but not the value edited on the other instance :slight_smile:

1 Like

OK, that’s clear enough for me.

Generally I generate runtime objects (at the beginning of the scene) so using the object variable I assign a value exactly to the newly created object and not to everyone.

But I don’t understand one thing. How can I read the instance variable value ? Using the same command used to read of the object variable ?
Thank you

How can I read the instance variable value ? Using the same command used to read of the object variable ?


1 Like

To sum it up:
object variables apply variables to all instances of an object, while
instance variables apply variables only to the instance you’ve selected.


Hello !
do object variables apply variables to instances of other scene (if the object is global object ?).


By definition, no. An instance is a copy of an object within a scene.
If you need to propagate variables through several scenes, you need global variables.

Thank you ! I’ve done it . It’s OK !

Hello, I’ve created an global objet variable in a scene and modify it with an event, in that scene. I’d like this modification to be applied on the instances of that object in another scene.
Could you help me ?

Thanks !

I’m not sure what you mean by Global Object Variable: do you mean an object variable of a global object? As per the wiki: Variables [GDevelop wiki] there are 3 kinds of variables: global, scene, and object. An instance variable is an object variable that only applies to one particular object in a scene.

If you want to assign a global variable to an object variable of all instances of an object, you could try an event like: “For Each” condition (targetting the object) with an action “Set object.Variable(instance_variable) to GlobalVariable(set_by_events)”

There’s probably other ways to do it but maybe this will work?

Thank you !
I’ll try :grinning:

Please, help me on how to assign a value to the instance variable of a specific new runtime generated object

Please don’t bump an old post. Instead create a new thread or search the wiki, where you’ll have quickly found this link.

Thank you for your suggestion. I have seen the link before i post here, but i can’t figure how at runtime i could assign a value to the instance variable of a specific new runtime generated object.

When you create the object, add another action straight after it - click on Add action select the object from the left panel, select Value of an object variable on the middle panel, and enter the details in the right-side panel :