Simple Python Actuator commands please

I am learning python and have been using www.tutorialsforblender3d.com a lot.

I however cannot find or figure out a way to add and remove objects threw scripting, I cannot find the commands to type in or when I do find one only one so far It spawns it but it has no physics XD.

I am using 2.49. Does anyone know any of these simple commans such as adding objects, ending objects, and any other commans that your think are pretty critical to know O.O ?

Removing an object is straight forward. Every object has an .endObject() method that kills it without needing an edit object actuator. If the script is attached to the object you’re trying to kill use this:


cont = GameLogic.getCurrentController()
own - cont.owner
 
# kill the object attached to this script
own.endObject()

If the object you’re ending is not attached to the script you can fetch it through the scene.objects list and then kill it, like so:


# Get objects in the scene
objects = GameLogic.getCurrentScene().objects
 
# Get the object you're after, remember to include OB before the object name!
toDie = objects['OB<i>objectNameToKill</i>']
toDie.endObject() #kill it!

To added an object you need to have an add object actuator attached to the scripts controller and then there’s a couple of ways for python to trigger it.


cont = GameLogic.getCurrentController()
own - cont.owner
 
addObj = own.actuators['<i>actuatorName</i>']
 
# It can be triggered like any other actuator
cont.activate(addObj)
 
# Or
addObj.instantAddObject()
 
# You could then get the last object created and do things with it
newObj = addObj.objectLastCreated
newObj['someProperty'] = 1
newObj.worldPosition = [0,10,0]
newObj.endObject()

I always use .instantAddObject. I think there might be a difference in when they actually add the object between the two ways, but I’ve never had a problem with .instantAddObject. With regards to the no physics check that the object being added is dynamic/rigid body.

Hope this helps!

for the instant add object does it just add the object on the add object actuator or if you put in OBname in the () will it add the named object ?

It just adds the object named in the actuator. It is possible to change the object added with python - as well as all the other settings.


cont = GameLogic.getCurrentController()
own - cont.owner
 
addObj = own.actuators['actuatorName']

#Change the added object, note OB is not required, just the object name
addObj.object = 'NewObjectToAdd'
addObj.instantAddObject() # add it!

addObj.object = 'AnotherObject'

# change the time the object lives for
addObj.time = 1200

# Change the spawned objects velocity
addObj.linearVelocity = [0, 10, 0]

# Change the angular velocity
addObj.angularVelocity = [3, 0, 0]

addObject.instantAddObject()

I tend to just attached one add object actuator then just use python to change the settings. Remember that the object you’re trying to add must be on a hidden layer.

Hope this helps!