Wandering patrolling AI character using only python in upbge

How can i make a wandering patrolling AI character using only python in upbge. No actuator involve. just python code only

I don’t know the code that will do this, but let me break this down into easily Google-able components for you, so you can discover the answer yourself.

“Wandering” implies that every X seconds, the character changes direction. Traditionally, an AI like this will have hard limits on location, an invisible fence, where the AI can go no further, and changes direction regardless of timing. The easiest way to do this is to have a Center Point, and a Radius around it.

So, you need:

  • a character that moves
  • the ability to change direction after X number of frames or seconds
  • The ability to make X seconds above random
  • The ability to check if the character is inside the radius, from the center point. (Using a circle means you only have to check one direction,more like raycasting, instead of doing more complicated vector math.)
  • The ability to change direction when the character hits the radius

From this, you can also conclude that your change_direction() function should stand alone without triggers, and then you can have a change_direction_at_time() function that calls it, as well as a change_direction_at_radius() function that calls it.

That’s a good start, all of those tasks are Googleable :slight_smile: In your searches, I’d look for “Python game x”, even Pygame code will still be useful to you here.

One other suggestion- you can use a simple solution like PyTurtle to test the AI, once you’ve got a turtle “wandering” the way you want, you can translate that to your game engine code

Thanks. I didn’t think of it like this. No need for Google i can do all that myself. Thanks again :slightly_smiling_face:

1 Like

i made this pathfinding system long time ago only with python


what you need is

  • a shortest path algorithm in python (I used Dijkstra)
  • a map with empties wisely posed (your graph nodes)
  • and your own AI algorithm that will help the npc (3 jabs) to dynamically navigate through the nodes
1 Like

Thanks you😊

Hi. It’s Cal.

I always wanted to build a system like this… with real time data, where the “wandering” character would lead the user to a currently important location.

For example:

  1. Warehouse manager goes to problem area in 3D render of warehouse.
  2. Stock broker goes to proof of “good buy” in 3D representation of financials data
  3. Real Estate agent goes to “the perfect house” for someone in google map
  4. Utility company technician goes to outage (Electric, Tele, Cable, Water) in map
  5. Farmer is informed of flooded field based on Weather Service or drone data

I gotta million of 'em… :rofl:

1 Like

You need some sort of A* to create good ai, with a* there are multiple ways to go.

I got a A* based on breath first search also uses the KD_tree to find a close node, for example: you click somewhere then you can grab nodes within range ot closest node and build a path to/from it:

In this case it’s used for a tile based game, but you can rip the codes and use some nodes instead of tiles.

So now that you can create a path you simply need to make the ai behavior.

  • use path to patrol
  • use path to walk fastest way to object/player
  • along the way check if something is near or if there is an action to do before continuing the path.
  • engage the player in combat, etc.
  • return to last node used from path and continue

All these responses talking about pathfinding are ignoring the original premise of the question- OP asked for a AI that wanders. You don’t need path finding or algorithms of any kind to do this, and it’s over complicating a very simple solution. “Wandering” means the character moves in a random direction for a random amount of time, changing directions at random. No algorithms needed, it’s just randomness.

I don’t think it’s helpful to answer a question that isn’t being asked. If OP specifically asks for wandering, then answers should focus on wandering, not path-finding and navigating towards the player

I have a* in py using evaluated bpy meshes, it’s in resources here I think

We can choose a random destination node using

choice = round(len(meshOb.data.polygons) * bge.logic.getRandomFloat())

basically the annoying fly that is randomly wandering in my room right now :laughing:

Shortest path algorithms is to make your npcs behavior somehow logical. You can even hack these to make the npc escape.

But you are right, the OP was just asking for some simple behavior. But then, with some if, elif and else he can quickly set it up himself according his will … so we don’t really understand why he asks :neutral_face:

And that’s where you are wrong,

  • how would you pick a random spot
  • how much time does it needs to walk
  • what direction does it needs to walk to
  • what if it keeps walking against a wall

So it gets complicated faster then you think

  • add radar
  • add rays
  • calculate stuff based on above sensors
  • figure out the direction
  • figure out when to turn away from something
  • found player… figure out how to chase it
  • etc.

Then after wasting time to try to get something up and running, you realize that you could use some nodes. Then another post is created here, how to make path from nodes? So instead we offer the best and easiest solution, use a node based A* algorithm to create directions/paths instantly without the hassle to calculate everything with every step you take. And that path can be as random as you like just by randomly selecting a node and calculate the path. This automatically avoids corner calculations and wall banging due to the path is already laid out and always finds it target, just by feeding the script the closest node it is chasing, or a random one to just wander around.

i had the same issue when i dived into pathing, tried a lot of stuff, best option use A* it saves so much headaches.

Here is some food for your brain so you know where we come from… sorta: