How to view Python code for logic bricks?

Is it possible to view the raw code behind a current logic brick and its settings?

For example, I have a group of Edit Object/Add Object Actuators and I would like to see the Python code for them, so I can replace it with a Python Controller with a more random outcome (such as random objects, random linV and time, etc. all based on arrays or limits I set).

Thanks for your advice.

No it’s not… the logic bricks are not translated to python before running they are just ran as they are.

It’s not possible to add objects with python you need an actuator.

But you can change all those settings on the actuator with python before you run it to achieve the same effect.

Ok, thanks for the reply. Do you have a link or search terms to help me find the specific code to change actuator values like that?

Take a look at the following, it might have errors since I didn’t test it:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

#--An edit object actuator named act is attached to the controller...
addObj = cont.getActuator("act")

#--The edit object actuator has a few methods...

#To set the object to be added...

#To instantly add the object without activating the actuator...

#To fire the actuator...
GameLogic.addActiveActuator(addObj, 1)

#--Now, to create random numbers...

#First the random module...
import random

#Now, get a random number, say from 1 to 5...
roll = random.randint(1, 5)

#Make a if/elif statement to randomly add an object...
if roll == 1:
elif roll == 2:
elif roll == 3:
elif roll == 4:
elif roll == 5:

Thanks very much. That’s a big help. I’ll see if I can get it working and at least now I know some the functions to search for.

You can change all of the editable fields of sensors and actuators by using their function. For example you can change the linear velocity that an object is created with for the add object actuator using

add = cont.getActuator("add")

You can find out what all these functions are by using the dir() function:

print dir(add)

And if you don’t know what arguments the function takes, just look to see if it has a get counter part, so if you were using .setLinearVeloicty you could type:

 print add.getLinearVelocity()

Check the console and see that it is [0.0,0.0,0.0], normally when you see that in blender it means x,y,z.

Thanks very much for the help andrew-101 and Moguri.

I also found this useful list of methods:

Class KX_SCA_EndObjectActuator

My script is working and now I have a better idea of how to do start trying other things.

This is what I ended up with. It seems to be working. I’m not sure if I need the addActiveActuator() line at all or if I could just get by with only using instantAddObject(). I figured I would leave it in, so I remember the command for later use.

This generates 3 to 5 pieces of random debris when an object is hit. I also use a standard End Object logic brick along with this.

import GameLogic
import random
rollpieces = random.randint(3,5)
for i in range(0,rollpieces):
	rolldebris = random.randint(0, 3)
	debrislist = ["Debris", "Debris.001", "Debris.002", "Debris.003"]
	rollvelx = random.randint(-3,3)
	rollvely = random.randint(-3,3)
	rollvelz = random.randint(-3,3)
	if i < (rollpieces-1):
GameLogic.addActiveActuator(randomdebris, 1)

Correct me if I’m wrong… but that should add between 4-6 pieces of debris…

the iterations between say 0 - 3 is four… 0-5 is 6

not sure if that is even important to you… but if you specifically need it in the 3-5 pieces range then start the for loop like this:

for i in range(1,rollpieces):

not nit picking… just pointing it out - as it’s easier for someone else to spot it… and thought you might have missed it.

If you talking about range(0,3) then it will only run 3 times; 0, 1 and 2, the 3 does not get run, its the same as saying from 0 up to and not including 3.

aaahhhh… fairl play - my bad!

cheers for the info!


Yeah, I use that site a lot. It is a great reference. And you’re right, you don’t need the addActiveActuator(). Also, you don’t need to import GameLogic, it is already automatically imported.

I’m not sure what you’re using the “if i < (rollpieces-1):” for. It seems like this would restrict the script to only spawn 1-2 pieces.

And in case you wanted to know, the KX_GameObject class has an endObject() method.

Thanks for the helpful feedback. I was thinking I might be able to End Object as well, that would give me one less actuator and simplify things.

I did use another similar script for a slightly different case without addActiveActuator() and it worked fine.

The “if < (rollpieces-1):” was used so the loop would generate the full number of sets of random parameter values (3 - 5), but only use instantAddObject() to add (2-4), so the addActiveActuator() could always add the last one.

I wasn’t sure of what the actual difference was. For my current use there seems to be none, so I erased the “if” line and the last line in the other variations of the script.

Anyway, it was good for me to try out addActiveActuator() in case there is another actuator class that doesn’t use instant.