Basic/Fast introduction to BGE Scripting by andrew-101
You call a script through a python controller logic brick.
You can get that controller though
cont = GameLogic.getCurrentController()
You can get any sensors or actuators connected to the controller through
Determine if a sensor is active through
Activate/deactivate actuators though
You can get the object which has the controller on it through
own = cont.owner
You can then change the objects position through
own.position = [x,y,z]
Access properties through
You can get other objects through
Basically thats a run down of the most common/basic functions of the API. For more info on the API consult the docs:
So by stringing those together you can create scripts. Its also good to note that each sensor and actuator have their own attributes. Here is an example that will make an object look at the closest object to it. The following script assumes that an always sensor, a keyboard sensor set to trigger on SPACE (called ‘space’) and an actuator set to edit object > track to (Called ‘track to’). Connect all this to a python controller.
cont = GameLogic.getCurrentController() own = cont.owner track = cont.actuators['track to'] # track to actuator space = cont.sensors['space'] # keyboard sensor if space.positive: object_list = GameLogic.getCurrentScene() closest = object_list # set a default distance = own.getDistanceTo(closest) for object in object_list: # iterate over all the objects in a scene if object == own: # we dont the object to track to itself continue tentative_distance = own.getDistanceTo(object) if tentative_distance < distance: # this object is closer than our previous distance, so we have to change out distance and closest variables distance = tentative_distance closest = object track.object = closest # the object attribute determines which object will be tracked to cont.activate(track) # activate the track to actuator else: # space is not pressed so we wont track cont.deactivate(track)
I hope that gives you a run down of the useful functions and how to use them.
If you don’t understand some of the syntax browse over this helpful guide: