Python Code For Switching Actuators

I copied the python code from this tutorial in order to implement it in my game:

The code runs without complaining, but it doesn’t initiate the actuator. Here;s the code:

import bge
import random

def patrol():

    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    actu = cont.actuators['Steering']
    waypoints = (scene.objects["Waypoint.1"], scene.objects["Waypoint.2"], 
        scene.objects["Waypoint.3"], scene.objects["Waypoint.4"])
    newWaypoint = random.choice(waypoints)
    
    print(newWaypoint) #for debugging
    cont.activate(actu)

patrol()

I noticed that it makes no reference to newWaypoint when activating the actuator, which doesn’t seem right. The code is from Blender 2.72 and I’m using 2.79.

I’m still struggling to understand the structure of python in Blender and how to make calls to the various classes or whatever they’re called, so I don’t know how to look this up or what to look for. I assume that it’s just a simple operation needed to pass newWaypoint to the actuator.

Any help would be appreciated.

You’re correct - that script boils down to:
activate the actuator called “Steering”

All of that waypoint stuff is never used.

Presumably you want to take the random new waypoint and assign it to the actuator?
If so, there will be a property on the actuator to set the target. Something like
actu.target = newWaypoint
You’ll want to do that just before you activate the actuator.

You can print out a dir(actu) to find out what the available properties/functions are. Have a look for one that looks like it could be the target.

The documentation can be quite helpful in finding what you need as well. I searched “bge steering actuator” and got:
https://docs.blender.org/manual/en/2.79/game_engine/logic/actuators/types/steering.html
Which tells me “target” is most likely the thing that you want to set, and it requires an object (which is consistent with your newWaypoint variable).

By the way, it’s important that you only run that script once when you want to set the destination. If you run it every frame then it’ll change destinations every frame.

This could be the problem. I merely put it on an Always sensor, while the original set-up in the video was more complex and called it after receiving a message.

I’ll play around with it later on after I look at that documentation. I have it working nicely without python right now using some relatively simple logic, which I don’t think would be any simpler using that python script.

Thanks for the help.

An always sensor is fine (and normal), just be mindful of the pulsing mode on it.
image
If the button in the picture is off (as currently shown), then it’ll run once. If it’s on, then it’ll run every frame (or as often as the “skip” is set to).

I’m beginning to understand that. Those operators along that part of the sensors stumped me for a while. Your explanation of how things are called each frame helped me get it.