How do I make enemies/npcs patrolling around randomly

So I have been trying to practice making AI without any good result, one of them is enemies/npcs walking around random place. I have tried to find on youtube or forum but can’t see any that close to what I tried to do.

This is what I got so far, not perfect and npcs stop felt awkward too

NPCsWalking-ezgif.com-video-to-gif-converter

Also when they move, they pop up and down, not easy to see and I use shake extension for that. if anyone have better solution, pls help with that too :P.

I also tried pathfinding but it felt weird, the variable doesn’t start to randomize at all like if it 4 it stuck at 4. I also don’t know how to make them stop on certain distant, I thought change variable will do but apparently not

Hi @Hey_ImEvil

There’s an extension actually called “Make object travel to random positions” which does just that. It uses pathfinding and it’s supe easy to use. You can use an object timer for the NPCs that resets after a random value and moves them to a new random position.

Also only use the “For each object” once and place your events inside of it. You should learn more about how it works.
https://wiki.gdevelop.io/gdevelop5/events/foreach/#how-to-add-this-event

Basic setup:

- Beginning of the scene  ===>  Start timer of NPC

- For each NPC:
  If timer of NPC > Random number  ===> Reset timer
                                        Move to new random position
1 Like

Hi @insein

Yeah I have try the extension before but without an example it kinda act weirdly. but yeah seem like it work alright base on your setup, could you help me with the stopping? I want them to stop either to stand there or do something, but I dont know how to make them stop and stand still

what I did is “if timer of NPC >= random number → random variable” and if it land on specific one, wait for random X time and do timer again but it not working (also I put OR on [the beginning of the scene} and have it with [if variable = 0] as a condition to start clock again, dont know if it good idea. Sorry for bothering I tend to over thinking to dumb solution

I’ll be glad to help. We all tend to overthink stuff when working on it for a long time!

I recreated a simplified version of your events and should work the way you want it.

2 Likes

Here’s a video about a solution from Game Endeavour - there’s no code provided, but it gives you the idea of what’s involved.

Possibly overkill for what you want. However, keep in mind that generally simple solutions here will result in simple movements.

2 Likes

It really worked like how I wanted, god you are my saver, thank you so much :pray:.

I didn’t even think about this, make them move on their own spot to trigger the “reached destination” condition. Also could I ask why you put the random variable at the spot where the timer start? I cant see the different yet, also I thought make them move on their spot without the [for each object] would confuse them?

1 Like

Oh yeah this dude, I have been watching this video over and over again and dissecting it, sucked cuz I have square brain lol, I always confuse like is the circle under enemies feet, like how that work and how to do it in gdev, is there a magical thingy I didn’t know? I was planning to watch it again for one of my project after this.

No problem!

You don’t always need a for each event. Actions on an object will be applied to all the instances by default.

Generate a random number when the timer starts, and based on the outcome, if number = 0 → move, if not then wait for the next timer reset…

1 Like

oh I mean like cuz they in a group, wouldn’t they got confused cuz we don’t specified which spot is which, like I thought A would be confused and try to move to B spot, you know?

ah I see🤔

That’s because you’re telling the NPC to move to NPC so each instance will pick and reference itself. It’s just how object picking work in GDevelop.

oh ok, learn something useful today (O-O-) thanks for taking time out to help me :pray:

1 Like