Help A.i. tracking to nearest enemy

I am currently working on a game where I need villagers to track to the nearest enemy, and start beating on them. The problem is every villager tracks to the same enemy, usually the first enemy created.

I have tried:
When ENEMY is 100 pixels away from VILLAGER…
VILLAGER tracks to ENEMY

I think the problem might be in telling them to move to the enemy. Every villager is choosing the same enemy.
I’m lost at how else I might do this.

If any of you guys have gotten something like this to work, or just got suggestions, I’d appreciate the help.

Thanks for reading!,

Maybe a “For each object” loop can do the trick:

For each Villager, do: Conditions: Select the closest Enemy (for example with the nearest condition: Pick nearest Enemy to Villager.X(); Villager.Y()) Actions: Add a force to move Villager to Enemy
Hope this works :slight_smile:

It sounds like you might have only one villager object and then have dragged copies of it onto the scene. I think what will happen then is that they will all be treated as identical, so when you find the closest enemy to one of them, that enemy will also be selected for the others. It would probably be the same story if you have different villager objects that you have grouped into a “villager” group.

Lizard’s suggestion of a “For each” loop sounds like the solution (as usual :smiley: ), as then you are forcing GDevelop to do the “what is the closest enemy” calculation for every instance of the villager object, not just once.

Okay…I get that. I’ll try it and see what happens.

But still…gDevelop is written in c++, right? And it’s an object oriented language.

The whole power of OOP languages is every instance of an object is unique, and can act and modify itself independent of any other instances. I just can’t understand why this isn’t what’s happening here :confused:

Wait a minute…I can’t figure out how to do loops in gDevelop! :angry: That’s a pretty basic programming function. I can’t even find an event to code my own loop. Any one got ideas?

I found it! In the events tab, click the"add" drop down menu, and select
“for each object”

It also has a “c++ code” event

Thanks everyone… Now I’ll try this

It works perfectly!!

I used:
For VILLAGER in group villagers:

Find nearest enemy
(Just search “nearest” if you can’t find it)
Move VILLAGER towards enemy

Thank you so much lizard-13 :smiley:

@BearKnuckles
Great! :smiley:

@MattLB
Yes, you’re right, C++ and JavaScript are OOP, but think on it:
If you have, I don’t know, 100 enemies (instances), and you need to update them, you’ll surely use an array/list/vector/wathever-you-language-use containing the instances. Now you iterate over the list and update each instance, GD does it so far.

Now you want to subtract health of each enemy colliding with a bullet, there are a lot of bullets in a list, and a lot of enemies in another list, what do you do in a OOP? Iterate over the bullet list, and for each bullet, iterate over the enemy list, this way you check the collision for each bullet for each enemy, and subtract health.
GD does something similar, and returns two new lists: a list of bullet colliding with an enemy, and a list of enemies colliding with a bullet. You’ve asked for bullets and enemies colliding, so your actions will be launched over these two new lists, and the subtract health actions will affect the enemies in the new list only, the same effect.

But I think I know what do you mean, and Victor/4ian are the right people to talk about it (because I am not sure if this is even possible), but what do you think about an object that acts as a variable? You add this object to the scene (is invisible), its only function is to “save” an instance inside it (a pointer), and the actions launched over the variable object are redirected to the saved instance. This way you avoid to have to “mark” a specfic instance with an ID and search for
this instancein every frame, maybe you could even react on enemy-enemy collisions, it’s very hard / impossible to do it right now (check same objects collision/distance/etc).