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

Bringing back an older thread here.

Fairly new to gdevelop but for the life of me I cannot get “make object travel to random positions” to work.

I have the pathfinding behavior on my npc already.
What exactly is making the object move with this extension?

Looking in the event list for the extension, movement is set to 0. Even if changed, I still cant get any movement from my npc’s.

Any insight would be appreciated.

Here is what I currently have.

As simple as this

1 Like

Ow my above post should go to you

Look into that example

1 Like

Hey zompire welcome!

The “at the beginning of scene” makes all its sub-events run only once when the scene is loaded. So you probably need to put the for each event as a standalone event.

2 Likes

That’s random movement, not make object move to a random position. They’re different things.

Ah, thats good info to know. Thank you.

After some time messing with it I was able to get it to mostly work with just one simple action like this.

After hours of playing around with it I could never get the timer to work correctly. I was trying to get the enemy to move to a random location, stop for a couple seconds then repeat the move action. I was able to get it to work for the first enemy but none of the newly spawned enemies have the effect, even with “for each instance”. I can deal without the timer if need be however.

Now I come to a new issue however, suggestions on how to contain all of the spawned enemies within a certain area?

With the current random move action, it doesnt take them long to get pretty far away from their spawn point.

Thank you for the info, I may use those ideas later on for other projects.

Use this event.

It’s the same as my previous suggestion but slightly adjusted to work for every newly created enemy.