Interaction between instances of the same object

Greetings!
Just wanted to ask a quick question: How would I create an event(s) that would make one instance of the object move toward another instance of the same object?
Not really sure how selecting and picking those instances would go through an event.
Thanks in advance!

Well, we would need a variable on an object that tells it to move to another. What I would do is make a number called “group”, which controls which objects move towards each other. Then, have a boolean called “isMoving”. if this is true, then move towards object with the same group.

1 Like

Sure, seems logical. But when you put a condition that selects an object/instance, it usually picks all instances in that event. How would you tell the event system that you want the instance 1 go to instance 2 if the condition to pick an object will pick all of them in the event?

Welcome back. It depends on how they’re moving and how you’re deciding which one to go after which one. Is this 2D,3D, platform or top-down? Are they movong through a behavior or through forces? The more info the better the responses.

You can’t access the information from multiple instances at the same time like you can if they were different objects. It would involve either using a second object to detect or target or you can pick 1 of the instances and put it’s x, y location into variables and then pick the other object and move it towards that position.

Again, it all depends on the situation. There are so many ways to do things in gdevelop.

You could assign ids to all of the moving objects so that it could you could add a condition to check if the id is not its own

1 Like

If i have objects with IDs from 0 to 100
Then how i check if object ID is not his own

If i would have from 0 to 3 i would simply add condition to check if object ID is 0 or 1 or 2 or 3 but with 100 i would not make condition for each possible ID

So how you dynamically check if object ID is not its own?

1 Like

It’s a top-down game and basic force events are being used. The solution you provided in the second paragraph seems really complex to me, not really sure how I would manage that. Also, I would have a lot of instances of that same object in the scene, but I asked for the solution of an example with 2 instances thinking it could apply to a scenario with more instances.
Basically, my main goal is to make every instance of that object (there would be 10 in the scene e.g.) start moving towards the nearest instance of that object using basic forces.

If you had 0 - 3 instances each with its own ID, I don’t think you could simply create events for that manually. Is your idea doing something like this?

Because if it is, you can probably see the problem here. You can’t pick one instance in that example event without the other being picked as well. The condition will apply to all.

What do the objects represent? What happens when then reach each other? Are the objects spread out? Are there teams or is it just a free for all? Would multiple objects lock onto the same closest object?

All of that matters. If one object targets another would the other target it? Or do they continuely run around chasing the closest object?

If you don’t have a lot of objects then you could stick a second object to each one. Then you wouldn’t need to save the location. If there are a lot, you could use variables.

Object2 would be stuck to each object1 with the sticker behavior.

Pick object1
Pick object2
Apply force to object1 towards object2

The question is how do they get picked. Continuesly?

You could use a for each object

For each obj1
----Pick nearest obj2 from obj1 x, obj1 y
----Apply force on obj1 towards obj2

I played around with this concept.
It uses a scene variable called counter.

At the beginning
—for each obj1
-------create obj2 at obj1 x, obj1 y
--------set ID of obj1 to counter
--------set ID of obj2 to counter
--------counter +1

For each obj1
----ID of obj2 ≠ ID of obj1
----Pick nearest obj2 from obj1 x, obj1 y
----Apply force on obj1 towards obj2

The ID was needed to ignore the obj2 attached to obj1.

1 Like

I was not expecting picking exact object
BUT automate it so it is dynamically picking one
My idea was to go with ID of enemy is less than ID of enemy

1 Like

Alright, reading this it really seems not hard at all, makes me wonder how I even got stuck by this issue :dotted_line_face:
Thank you a lot, really helped me out, and to others as well!

1 Like