How to handle multiple instances of same object

Sorry, I tried to find something in the tutorials and the forums, but I wasn’t able to find a solution. I would be happy if someone could point me in the right direction.

My problem: I have an enemy sprite that is moving depending on the relative position from itself to the player. I subtract the x position of the enemy from the x position of the player, same with y positions and then compare the absolute values to decide if the enemy moves in x or in y. Movement for player and enemy is restricted in up, down, left and right. This is how it looks:


My custom actions look like this:

(One for each direction with corresponding angles and animations.)

This works exactly as intended, as long as there is only one enemy. If I add a second enemy I run in two problems:

  1. The actual sprite for the second enemy isn’t following the second controller. (skeletonController is just a hidden sprite that gets rotated for movement without rotating the actual shown sprite. I use this because the direction the enemy is facing will be needed later.)
  2. Only the first skeletonController that I created makes the decision where to move and the second controller just moves in the same direction, no matter what it’s position to the player is.

I know there are Object Variables, but I don’t know how I should use them here. How do I compare the position of every instance to the position of the player and call different actions on every instance depending on the outcome of this comparison? And how do I make one instance of a sprite follow a specific other instance of another sprite?

Try this:

1 Like

I tried this:

But now the first instance moves like it should and the second instance never changes it’s direction.

They are sharing the same scene variables and scene timers. You need to convert these to object variables and object timers (of skeletonController).

But how? I found out how to create an object variable, but how do I calculate the distance with it?

1 Like

Did you find a solution? Or a explanation?
Greetings :slightly_smiling_face:

I am not that advanced in Gdevelop, but thats what I have been trying to figure out.
I am in the same situation, where enemy behavior would work as they should as per the events described.
But it only works in the context of those instances that are involved initially. The code wont work for new instances. This is where Extensions and Functions come.

I know the solution is somehow to pack the entire algorithm (events sequence) as an extension or function. And then use that extension as a behavior on whichever enemy objects we want.

Just like any behavior or action the extension would have to be made such a way that it accepts parameters that vary.

Hello, I would like to bump this topic, in case any one has a solution.


Lets say Object1’s instance1 interacts with Object2’s instance1 using Event Group1
and similarly Object1’s instance2 interacts with Object2’s instance2 using Event Group1 as well.
but this ideally never happens because Object1’s instance2 will always prefer Objects2’s instance1 and will usually ignore Object2’2 instance2.

How can we achieve this correctly where let’s say there is a situation in a scene that has multiple enemies of same type but they need to perform different tasks (such as having different patroling routes, states etc).

One of the solutions will be to have each enemy as a separate object (and separate objects with which they are interacting) then have separate events for them. But this is not an efficient solution as it will result in lots of redundancy.

1 Like

I think you may need to create Groups for both Objects, then do you checks on Group and iterate with a foreach
1 -Foreach objects of Group 1
2 --Foreach objects of Group2
3 -----if Group1.obj_variable == Group2.obj_variable
4 -------- Do something…

1 Like

Totally makes sense.
I will test this out, based on my current familiarity level of Gdevelop, I have high hopes that this approach should work.

Just instances cannot be fundamentally added to a group.
Only objects as whole can be assigned to a group.

Any other ideas…how instances can be treated separately?

The answer may be right under my nose :sweat_smile:

1 Like

is there way to make instances with the “create an object” -function? so u dont have to create the objects before the game is running.

Add an Object and set Objects variable to the object then when you use “Create an Object” assign the Object varibles as you Like see this image is for the enemies of my game,
The Object is named enemy_1, enemy_2, … but note the hp, attackpoints are different for each one in this case i create the enemies when player collide to a zone then check if enemy variable is 20 then enemy_1.varibale(inst_hp) = new value
Enemy 1

Enemy 2 same Object variable different values

Event where the enemy is created

I just add a dumb check if obj_var_hp of enemy_1 = 20 then set obj_var_hp of enemy_1 to 120

Hope this helps.

1 Like

Thanks for answering and for your time:)!
I’m sorry, I don’t understand it. :frowning: I read a lot of times…

I made an example, but I don’t know how can i make the same with "the create object " or “Create an object from its name”- fuction.

I’m sorry, maybe you can explain it with the small example?


When you creates an instance of NewObject into the Scene just after
create NewObject
assign its varibales

Look here first creates an object of enemy type then assign obj_var_direction and obj_var_atk_points

In a For Each loop, you can calculate the distance between any sprite you want. Even there is a condition for it, then you can store the results in objects variables.

But you can create behaviors even in a much easier way. An example:

This simple events:

Create this behavior:


The example also calculates the distance between the nearest enemies and store the result in objects variables for each enemy, but it don’t even uses that data to move the enemies.

1 Like

@UlisesFreitas Thanks a lot. I got it!

@erdo Hey:) I’m sorry i can not see the connection to Object Instances. Maybe i’m just tired ^^
I’m also working with calculation Distances, but on other parts! :slight_smile:

@TheGeneral Well, no mood to be rude, I was replying to the creator of this thread, not you…

… but anyways, the “connection to object instances” you’re asking for is implied in the way that my example manages object instances using a repeat for each instance loop at the very top (the first thing you read if you take a look at the picture) of the events that manages object instances. Also, the animated picture showing many instances of an object reacting differently at runtime can be interpreted as the manifestation of a “connection to object instances” handling (just saying).

So, I’m not sure what an object instance mean to you (if you think an “object instance” is the same thing as “mayonaise” or “platypus” is not my fault), but there are object instances
emanating from my answer as if they were fly larvae emanating from a beautiful dung.

Next time I do an object instance handling example I will tattoo “OBJECT INSTANCE” on my forehead and install a large arrow of neon light pointing at my head :kissing_heart:

Jokes apart, this was the message I was replying:

But how? I found out how to create an object variable, but how do I calculate the distance with it?
1 Like

As far as i understand the OP is about creating event code for each instance one the same object.
There is an instance list of all the same object, and an event to select all instances, but there seems to be no way to reference a specific instance.
I have an enemy object that I have assigned, and then placed two instances of that object on the screen. If I want event code to interact with the second instance and not effect the first how do I do that??
This is the root issue. I found an extension to return location values for each instance, but still if I want to change the direction, animation, or even object health of a set instance of the enemy there seems to be no way.
I know there has to be an index of all instances in the back end coding to run more then one instance of any object.
So there should be a way to reference any aspect of All object parameters for a single instance of that object.
IE: change the number of the animation of enemy.instance00: set to 2
or change the number of the animation of enemy.instance23: set to 2

This is an extreme limitation to power of this engine if we can’t directly change parameters of each instance using events.

I would really love to see an actual response to the actual issue! This would open up the options of what we can do!! Otherwise I might as well go back to using normal text based coding in an old school IDE…