[Solved] Are instances magic? (platformer behaviour)

Hello. When my platformer character gets near to an instance of a particular sprite object it does an opacity tween, and then fades away again as the character walks away. All I use to differentiate between the instances is an object variable called id that has a different value for each instance. Most scenes have 3 or 4 instances of the object in question. It works wonderfully, with the correct object animating in and out. But… why? How does the game know which instance I want to animate based only on the condition:

image

(there are sub-events for the tweens)

Surely this condition is always true? Same for the other events - one for each instance. Are instances being picked based on the one nearest my character? I’m happy it works, but I wish I understood it! :laughing: How is GDevelop deciding what instance I’m interested in, based only on a variable check that, I think, will always be true, whether my character is near the instance or not (and the other 3 like it)?

image

1 Like

Any condition that refers to an object will narrow your object list for the event.

So in your example, since only one instance has that variable value, only that instance is picked.

The condition, on its own, doesn’t seem as if it would do anything, or will evaluate to true constantly because that instance does have that value, but the sub events are only triggered when my character gets near the instance. I think I understand… though it still seems like magic :slight_smile:

1 Like

You must have other conditions somewhere else then?

I had some hard time understanding how instance variables work
Trick is to think of any equivalent in real life

This is name tag

You have class with kids
Imagine 2 kids are called Kevin in your class

So whenever you reference kids called Kevin

You reference whole kids group but narrow it down only to ones that have name Kevin
How you know which kids have name Kevin?
You put name tags on each kid in your class and write down their names on them
That is how you make instance variables for your class
All class members have nametag (variable)
All members of class are kids (same object or same object group)
BUT on each tag is written something different (instance variable)

Imagine instance variable as just HP variable
IF you can change color of enemy whos HP var is below 50 to red
So you indicate he got damaged half way (Max hp was 100)
All enemies have HP var start with 100
But they can have different values
Instance vars are only different in a way you can pre define their values in editor
And later in game logic reference them

1 Like

Thank you. The name tag analogy is a good one.

1 Like