PredatorPrey AI Tutorial

(goldentaiji) #1

Well that’s what I named it for short. :smiley: One of you guys needed a .blend, lol I’m not going to say who unless they choose to themselves. So instead I made a .blend and a tutorial while I was at it so some of the community could benefit as well.

Basically this tutorial will cover random placement of objects in blender, and a simple AI where the predator seeks out all the prey and “eats them”. Enjoy :wink: .

Any feedback is welcome. Too hard, too easy? Did I leave out anything that needed to be explained, etc.

Here’s the link

Note: :wink: This tutorial is not meant for guys like wiseman303, Siegel, ST150, z3r0_d, and all you other python buffs in blender GE.

Jason Lin

(ineedanewbi) #2


this is really useful, unfortuneatly i couldnt get it running in blender 2.25
i tried recreating the script, but i’ve come across some errors,

targetObj = objectsList[i]


error on the target.setObject() line, expected a string, KX_GameObject found

i tried adding in str(targetObj), no errors, but it wouldnt track to the object
i have the GameLogic.addActiveActuator(…) in there. i also want to do tests and print out the names of all the objects detected near by , but it would just print blank lines. i can only get it to print out the distance between the nearest object, but thats it.

any idea?

(goldentaiji) #3

:-? Hmm might be just the blender version error. Sorry but I made the .blend in Ultimate blender a modified compilation of Blender 2.37.

Maybe in 2.25 you can’t set the name for the object to be tracked too, which would mean no workaround for it.

Jason Lin

(Dr S) #4

HI Jason Fantastic little tute saved me from learning the hard way. You Must have read my mind.

I have already incorporated it into my bugs game (I will upload a new version to and let you know so you can have a look)

Is there an easy way to limit the number of prey that appear ie in summer heaps of food winter not so good etc I have been trying to break out of my engineering background and do some sort of biological model.

A cross between the old game creatures and a tamaguchi

Thanks again

Dr S :slight_smile:

(Siegel) #5

I see the problem it used to only ask for the objects name but now it asks for the actual object er object so to fix it you must get the objects name this is done by calling

other thing is i dont think you have the scene.getObjectList() so u will have to store the objects in a global list yourself.

so the fixed code would look someting like this

targetObj = g.obj_list[i] 

I havent tested this but it should work :smiley:
let me know :wink:

(ineedanewbi) #6

hey, thanks

yea, i almost got it all working, with track.setObject(trackObj.getName())
i am able to print out the names of all nearby objects, and check there distances,

but i still have 1 problem… when i changed the track to object, it becomes “broken” and i am unable to make the actuator work.

if i do not change the track to object, it works fine.

when i change the track to object, to the closest object, no errors appear but it does not track to that object, and when i use

print trackObj.getObject()

blender crashes and kicks me out…
do you know where i can get more documentation on blenders python? the documentation i’ve looked at only contains logic bricks, and i know theres alot more to python that just that, thanks

(goldentaiji) #7

:smiley: Here you go.

Jason Lin