AI Random Events

So I’m making a stray dog for my game. I’ve modeled it, rigged it, and animated it. Now, of course, I’m onto the development part. Where I program the German Shepard to remain idle, walk, run, and attack with python.

I’ve only got the idle segment complete. (Now I’m sure there’s other looking for answers for the same problem, so I’ll make this as broad as I can). Now, I’m wondering: how do I make the dog just do random things? Like scratch it’s head and randomly walk around in python?

if your using a list you can use
(get random number for size of state list

import random

def state1():
    #state stuff

def state2():

states = [ state1, state2, state3, state4] 
def main():
    selction = random.randint(0, len(states)-1)
    command = states[selection]

I am not utilizing lists. I’m using a boolean method, which would work efficiently in my case.

So is there any way I can add a random sequence during the idle state?

Like the dog sniffing something on the floor or scratching it’s ear during the idle state?

I so far have this:

from bge import logic, events

scene = logic.getCurrentScene()
owner = logic.getCurrentController().owner

armature = scene.objects['GermanShepardRig']
tongue = scene.objects["GermanShepardTongue"]

if owner["Idle"] == True:
    armature.playAction("DogIdle", 0, 29, 0, 0, 5)
    tongue.playAction("DogTongue", 0, 5, 0, 0, 5, 1)

check out own.state in the api

bitmask are a little tricky but basically you will need a state that always runs, that launches sub states for x time and then does something else

I assume it’s using the states in the logic bricks?

yes but states can run unique py per state*

if this

if that

if this

if that

is much slower than calling a snippet or module or functions

Well is there another alternative?

always-----(1 state)

in each state

in each state you need a goal (anim is done playing or ??)

when reached choose another state

this is faster than each state being all cramped in 1 py script unless you use a list of commands and choose one
(like my first post)

def state():
#stuff in state

in main choose state function somehow and call it.

I’ll go with the top one you listed. Since I like to keep the scrips consistent.

I was considering making a property called “Random”. It generates a random number from 0-10, since there are 10 misc idle animations. Then whatever integers it is, it plays that animation.


1 is dog sniffing ground. 2 is dog scratching its head.

It generates 1, so then it plays the sniffing ground animation.

Could such method exist?

you can use a random int after each animation is played to get the next animation/behavior…hell you could even wieight them to give some a higher chance than others.

from random import randint

behavior = randint(0,3)

So how could I specifically accomplish this?

you could use Markov chains for this

here is a link to a wiki page if you don’t know what that is

I could use this, but I’m not sure how to input it into Python.

just put it into a dict.

states = {}

states["sniffing"] = ["scratch","wagging","look around"]
states["scratch"] = ["sniffing","wagging","look around"]
states["wagging"] = ["scratch","sniffing","look around"]
states["look around"] = ["sniffing","wagging","scratch"]

and some code to traverse the chain

here is a link to a blend i made that uses a markov chain to generate random text
you are welcome to grab anything from it if useful

just use what I provided above…then a ‘switch case’…but geared to python…

if behavior == 0:#stand I assume
    play animation function()#generic would have to write a function to control the animations...same for the following
elif behavior == 1:
    play animation function()
elif behavior == 2:
    play animation function()
    play animation function()
This is very generic and not really may be better to just change a property like add 'anim_state' as an int and then set up the animation bricks to play based on the property sensor...this would be fine for something simple, but can get a little crazy with complex logic.

here is a crude example, feel free to look at it…I’m not certain of your experience level I hope it is not too simple or even too complex…it is really basic stuff though.simple_states.blend (451 KB)

You do not need python for that.

The random actuator can get you a random number in a property when activated.

You can setup the different states as states of the build-in state machine.

You can use the property with the random value to decide what state to switch to (or how long to wait till you do such a decision).

For example, in idle (or waiting) state you create a random number that represents the seconds when to decide the next action. So you start a timeout starting with that random number.

When the timer exceeds you calculate another random number (in a property). Then decide what to do. For example: 0 - wait again, 1 - walk to a random place, 2 - search for food …

You can even use this method at other states with different decisions: When waling and detecting an enemy the decide randomly to attack, hide or run away.

And yes, the exact same thing can be implemented via Python.

I hope it helps a bit.

you are right monster, but my opinion is…it is simpler easier to read in python…just my opinion…but giving him more options is always good.

you are right monster, but my opinion is…it is simpler easier to read in python…just my opinion…but giving him more options is always good.

I’m a she, by the way. Just kidding.

Humor aside…

Couldn’t I just use Monster’s idea to generate a random number then use Justin Barrett’s method to execute the actions?

Alright, I got it working by using Monster’s and Justin Barrett’s methods!

I appreciate your guy’s support, and I couldn’t have done it without you guys!

Bis Später!