Creating a "sight cone" - For Each Object not working

I’m working on a top-down game, and I’m trying to create a line of sight polygon for each instance of an enemy. I followed this tutorial How To Create Advanced Enemy AI in GDevelop Free Game Engine - Tutorial - YouTube (it’s around the 21 minute mark before he starts talking about the For-Each Object), but it’s not working. A polygon is created for one enemy only - the first one placed in the scene. I can’t for the life of me figure out why.
Should I even be bothering with this, and use a raycast instead?

EDIT: In case anyone asks, I already tried it without the “Trigger Once” condition. No change. I’m starting to think I’ve run into a bug.

Here’s my code:

The trigger once in your first event is spoiling your intentions. Remove it and put the whole first event as a child event of a “Beginning of scene” or a “Trigger Once” event.

1 Like

Tried it. Still not working.

Can you be a bit more descriptive? What is it doing? Can you provide a screen shot of what you’ve done?

Also, your last 3 events can be done more efficiently as:

Better still, you may be able to avoid those events by using the Sticker extension.

1 Like

It creates a “sightcone” (actually a square) for only the first instance of the enemy placed in the level. I tried changing the creation of the sight polygon to a “Beginning of Scene” event, which had no effect.
I would send a screenshot, but I already took out that code and replaced it with raycasts, which works, for now.

I’ll have to try the “Sticker” extension - didn’t know that was a thing.

Raycasts can be processor expensive, and it’s more efficient to perform a collision first, then a raycast to see if the target is visible (i.e. isn’t hiding behind another object) if there is a collision.

EDIT: Okay, I retract that statement. NOTHING - not collisions, not raycasts, not distance to object - using the “Repeat for each instance” works at all.

I also looked at a previous top-down project I made earlier in the year which uses “distance to object” for detection, without using “For Each Object” and everything’s instantiating fine. I’m not sure why that is. I’m also not sure why the randomized movement of each enemy is working fine, as it uses the “Repeat for each instance” event.

Can you provide a screen shot of what you have?

I should also be more specific: One enemy is working. When the raycast hits the player, “alert” goes to true, which as of right now only triples its move speed. The other enemy doesn’t “see” the player at all after that point. It doesn’t matter which. Before, when I was using the polygon, it would only attach to the first enemy placed in the level, specifically.

Most likely because of the Trigger once:


1 Like

That’s what I’m thinking, but removing it causes the speed, detection, and HP to constantly increase. I’m assuming there’s a better way I can handle what happens when an enemy is in “alert” mode

Yes, there is. In the events in the image below, add the condition boolean of object critter_Melee_Base is false to the two parent events (first and third ones), and set the speed, detection and HP in the actions of the child events, where the alert variable is set to true.

And remember to get rid of that other event.

1 Like

Removing the “trigger once” event did indeed fix the problem. I also got rid of the raycast and used “distance to”, along with the “facing” variable to determine if the enemy can see the player, which works. I’ll add a raycast later to determine if the enemy can actually see the player.
Thanks for the help!

EDIT: I also changed the logic so that if the enemy sees the player when not already alerted, it’s stats - speed, sight, and HP - are doubled. Seems to have the desired effect.