using python in the gameEngine

Earlier in this thread there was the problem of sending multiple messages to the same actuator. I’ve gotten around this as follows:

Make an single empty named “Generic Message” with lots of message actuators, more than the number of messages you’ll ever create during the execution of one python contoller. (And make certain that this empty registers itself in the GameLogic.Objects dictionary as per the original post in this thread.)

http://www.merrill-samuelson.com/permanent_display/duh.png

Then I import the following code in my python controllers:



MessageActuatorCounter=0
def GenericMessage(To, Subject, Body):
    global MessageActuatorCounter   #Number of the actuator we're going to use
    done=0;
    while not done:
        try:
            aXu= "act"+   "%d" % MessageActuatorCounter; # Actuator's name
#            print aXu;
            act=GameLogic.Objects["Generic Message"].getActuator(aXu);
        except:
            MessageActuatorCounter=0;    # Loop around to the first one again.
        else: done=1
    act.setToPropName(To);     # Message Receivers must have this property
    act.setSubject(Subject);   # Message has this subject. 
    act.setBody(Body);
    GameLogic.addActiveActuator(act,1)    # do it.
    #use next actuator next time
    MessageActuatorCounter=MessageActuatorCounter+1;

The advantage is that now I can just use the command

GenericMessage(To, Subject, Body)

to send messages from Python to anybody anytime anywhere.

Note, there are no errors generated! If you use up alotment of messages before your contoller exits, they loop around and you overwrite your earlier messages. EEK. Bad. If anyone comes up with an easy fix for this, let me know.

Presumably one can do this with all sorts of actuators, at least the types that are easily definable using python. Perhaps a class that handles the incrementing and subclasses of that class define the actual values to be set in the actuator…

-Frank

I’m glad to see that this post has been resurrected. Jasper has an excellent method for python scripting.

The link above appears dead – does anyone have a copy?

how do you check if the object is adding itself?

To see if the object (an object with the particular name) is already registered this way, you can query the dictionary like this:


GameLogic.Object.has_key(name)

Hopefully helped.

Oh, or did you mean how to check if they are being added at all? Then you should check where you put the code that does the registering…

~Toni

hi all…

i’m trying to follow the way you describe it, but i have the problem, that the property-sensor seems to be triggered BEFORE the always sensor …
so GameLogic.Objects is empty for me. i tried to use 2 property sensors, but it seems, that there’s an index which object gets initialised first … so this didnt help.

any suggestions?

c sascha

in 2.34, a new Scene module was added that gives you access to the objects in the scene.

hopefully that helps. if not, please bug the game engine coders about it (i’m looking at it too).

~Toni

ok - the Blender module was at some point not available in the game engine at all, but I’m happy to hear it works now.

it seems that the Scene module, that’s on the game engine side, can do that too – see http://www.blender.org/modules/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_Scene.KX_Scene-class.html

hi toni…

it seems i mixed something up. the object from Blender.Object.Get("") is a blender not a GameLogic object (different class hirarchy i think).
sorry, allready deleted my post

c sascha

ok no prob.

that is basically result of the unfortunate fact that the game engine is not as well integrated to the rest of Blender as it could be (there is this other old engine, Enji, where you can use the normal Blender API but it’s otherwise not as featureful as Ketsji (the current engine))

~Toni

i tried it, but objectList is empty, speak [,]. somehow you have to register a object with a scene (maybe link it).
any ideas? the scene would be handy if it worx.

c sascha

i think it currently gives only the objects on visible layers (that’s why i haven’t been able to use it yet)