Detailed proposals for the python API: have any?

And I’m talking only well written proposals including code mockups and a reason for it to be a new BGE API feature, don’t use a one liner and be done with it, here’s an example.


We all know the sendMessage function

sendMessage(subject, body, to)

We can send messages, but objects need bricks to check for them, in other words, a function would be nice and handy.

Why would this function be useful, in very complex games there may be a lot of messages being sent and recieved throughout the game, this proposed function would eliminate the need for message bricks, and therefore reduce logic brick mess.

Ways to do this could be this. NOTE: Slot argument would not be needed if the 4th possibility for a python function to recieve the message uses a string where you type in the name itself.

 obj.sendMessage(subject, body, to, slot) 

See the slot argument, this would be neccesary incase a recieving object gets more than one message at the same time, you could leave this empty, but then a recieving object will only get one message per frame.

Ways to recieve it are as follows

It could be part of an if statement that checks if the object is receiving the message and returns true if it is

 if obj.recieveMessage(slot) == True: 

Perhaps a property would be better (value equals slot number of message recieved that frame)? Drawback is that there’s no facilitation for more than one message per frame.

 if obj.messages == 1: 

Or you can do it like the meshes property (no. in brackets equal slot number of message)

 if obj.messages[5] == True: 

Or you may not need a slot argument for the sendMessage() function at all, just make the recieve command use a string that is the name of the message (I really like this last option as it doesn’t change the sendMessage() function and its arguments at all (remember that the message name has to be typed exactly and is case-sensitive))

 if obj.recieveMessage("elevator") == True: 

For all these possible recieve functions do note the string version will not work if you send the message with an object name in the TO field that’s not the recieving object even if the message title is the same.

List your proposals for the API here, remember they must be detailed and include code mockups.

The message sensor is already capable to deal with multiple messages received in one frame. You get a list from sensor.bodies and sensor.subjects.

I can’t see any advantage not to use the message sensor. You just need one message sensor to deal with all messages the object receives. Also the sensor triggers the script.
How do you want to activate the scipt when an message arrives, if you don’t have an appropriate sensor?

That is what I miss in the Python API regarding messages:

  • the possibility to set the “To:” part of the message actuator. (Instance Variable “to” = KX_GameObject or None maybe?)
  • The sendMessage function should support KX_GameObjects beside strings for the “to” parameter to get around the problems of non-unique object names (from addObject, linked Groups etc.).

Yes this is a two line proposal without code mockup.

Usually when I have large central scripts I’ve written when pythonizing a lot of things in some of my old projects one of the sensors the script is hooked up to is an always sensor with pulse-mode on and use the positive property so some parts run only when a sensor(s) is positive or not positive and some other parts run every frame. With the function I proposed one who uses such a central script won’t need message sensors if they want the object to recieve messages and have an always sensor if parts of the script run every frame.

Just to note in case you’re against large centralized scripts, they’re less effort to wade through if you use comments frequently.

How about that?

import GameTypes

for obj in GameLogic.getCurrentScene().objects[:]:
    for sensor in obj.sensors:
        if type(sensor)==GameTypes.KX_NetworkMessageSensor \
        and sensor.positive:

You just need one message sensor connected to any controller on the objects you want to receive messages.