I am currently working on a game and I would like to implement a patrol system for a city guard, I know next to nothing about python so currently I only work with logic bricks (if that matters). It is not really important but I would like to learn how.
Thanks for any help
P.S. if you need clarification just ask all my stuff is crazy
Way points are good. The simple method I used recently was to get the nearest way points and choose randomly which one to go to. Then add that waypoint to a list of visited points.
Next time when choosing the nearest waypoint dont choose any that are on the list. You can clear the visited list either when it gets too long or when there are no near waypoints to choose from.
let me know if you need help implementing anything,
cont = bge.logic.getCurrentController()
own = cont.owner
## store list of navpoints and steering target
if 'NavTarget' not in own:
for child in own.children:
if 'SteerTarget' in child:
if 'Nav' in child:
for NavTargets in own['NavList']:
## if you are at your target pick another
#if you are done with the last target pick the first
## delete the pound symbol before these two lines to print debug info
##for NavTarget in own['NavList']:
##print('Name is '+NavTarget.name+" and number is "+str(NavTarget['Nav']))
Well Monster, you are always telling people to choose variable names which are meaningful. Surely navtarget is better than “nt” or “jiggypuff” or anything like that. I think BPR’s names are pretty well chosen. In a large game there might be a lot of "target"s or “points” and certainly a lot of lists, so calling them by a meaningful name is a good idea isn’t it?
I did a section on patrolling AI as part of my stealth tutorial. Maybe that can help, I’ve explained how the python works, so hopefully it’s relatively jargon free. In it I used ordered waypoints so that AI follows a fixed route.
The “jargon” of the script BluePrintRandom posted is the fact that there are few variables used, regardless of their names.
I started to rewrite the script, but without the Blend I quickly realised that it wasn’t going to be useful, but for reference:
own = cont.owner
# Get previously setup data
waypoints = own["waypoints"]
index = own["waypoint_index"]
steering_target = own["steering_target"]
# Setup data as this is the first frame
waypoints = own["waypoints"] = [o for o in own.scene.objects if "waypoint" in o]
steering_target = own["steering_target"] = next(o for o in own.children if "steering_target" in o)
index = own["waypoint_index"] = 0
# Skip all waypoints that we're close too
min_waypoint_distance = 1.0
waypoint = waypoints[index]
if own.getDistanceTo(waypoint) > min_waypoint_distance:
index += 1
own['index'] = index
steering_target.worldPosition = waypoint.worldPosition
Things like the recursive children could have been handled more intuitively, rather than just iterating over 3 deep.
There’s a few ways I do enemy AI patrolling a beat or whatever. Animate the enemy to walk a beat, but it’s predictable. Use a Motion & a Radar with an angle of say 55 and a distance of 3 looking for the property avoid. when the radar is positive it turns your enemy 90 degrees. Then create a “fence” (if you will) with the property avoid (see Scene 2 on the blend) Still predictable
I know you said you didn’t use Python, I’m not a Python coder either, but I use a Track_to_closest object script with a motion actuator. I found it in resources, I think Cotax wrote it. I modified it slightly.
Then spawn nodes for the enemy to track to…
The enemy will move towards the node, when the enemy collides with the node it disappears using end object logic brick. I loop a Timer property < interval to re-spawn the nodes, (when the timer reaches a certain number it resets to 0) There are other ways of doing it. (see the blend)
Then it’s a matter of moving empties around in your environment, and tweaking the Timer properties
I use states on my enemy patrol.
state 1 - Walking the beat
state 2 - Looking
state 3 - switch
state 4 - Chase
I’ll use a Random sensor with a seed of 2 > say 100 with an “and” & “nand” controller. switching between state 1 & 2. So the enemy will walk the beat, but at random times, stop and look around. With the nodes spawning and ending, it can create a bit of unpredictability as the enemy turns toward a new node.
I’ll use a ray on the enemy, or a group of rays, to look for the player. If the ray is positive, the enemy switches to state 4. Then uses the navmesh to chase the player. Or, I’ll have the player spawn nodes at every corner, so the enemy can chase the player around buildings an such without a navmesh.
See the attached Blend. it isn’t;t perfect, I just threw it together quickly.
I couldn’t get the navmesh to work so I deleted it.
But you should get the idea.
Use WSAD to move the player.
The Radar set up is on Scene 2