Set Instance Variable at runtime

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

You first create te object then assign the variable, make sure is different, add a counter or use the uuid extension

1 Like

Super fast solution. Many thanks.

Please spent some time to explain why gdevelop change the instance variable value ONLY of the newly created instance and not of ALL the instances of the object?
For example, in the above event, if i use as condition a mouse click on a instance of the same object then the action for the change applied also to that instance

In order to update the object variable of all instances of an object you need to iterate with a Foreach look for that in actions then do
Foreach MyObject
MyObject.Variable = Bla
This way all your instances are going to have MyObject.Variable = Bla

1 Like

Not really.

You can have an unconditional event (that’s not a subevent) and just have the action to set the object variable. That will act on all the objects.

image


Or you can add the condition to pick all objects and set the variable in the action.

image

1 Like

Nice, but I didn’t say that with a foreach is the only way.

let’s say we have a road with cars one after the other
… is it possible to check ahead of a car instance if exists another same car instance ? so we move it on, regardless if behind it is also a car instance (intially collision ahead and behind and then the ahead car moves so we want to move the middle car also…)

It is possible, but not with 2 car objects. If you try to do something like car in front car stop car and move other car, GDevelop won’t know which car object you are referencing for each action.

I would look at adding an extra object, like a small square. Place an instance of this object in front of very car, and link it to each car it is in front of.

Then remove all of these little squares that don’t collide with a car. You will be left with the little squares that are in collision with a car object.

You can now go through each of the little square objects, and perform whatever action on the car the car linked to it.

… i have an object variable (OBJID) and at object creation at run time i set different values for each instance. Then at en event i use for condition (FOR EACH) and Variable check to identify the preffered instance… is that the correct way to identify an object instance at run time?

Hi you can check if your logic is correct just start in Debug mode and check if every car has the OBID assigned and make sure everyone is different.
For the 3 involved collision maybe inside your foreach you need to set some temp variables to identify which of the cars are involved, and use Pick nearest

thank you for the hints! …

…please give me also your help on how i overcome the browser sound not playing at start for an HTML5 exported project.

The game needs a user interaction before the sound will start. From the wiki :

Before a sound/music can be played, you have to first ensure that the player interacted with your game (a simple click/touch on the screen is ok to unlock audio). This is a limitation of browsers and cannot be worked around. In your final game, it’s recommended to have on your first scene a “Press any key or touch the screen to continue” message.