Set message sensor subject with python

I have v2.65, and all I am trying to do is set the message sensor subject using python.
I know that all you have to do to set a message actuator subject is MessageActuator.subject=“SUBJECT”, but I tried the same code for the message sensor, and it still accepted any message subject. I have already looked at the blender/python api, but I don’t understand how to use the information it provides. If some one can give me an example of how the code is supposed to work, that would really help. Thanks.

bge.logic.sendMessage(“MessageSubjectHere”) and it is recieved just like any message.

That’s for sending a message, but what I want to do is modify my message sensor subject detection.

Maybe it is just a typing error, but according to your description you change the message actuator not the message sensor :wink:

Hey,

I am looking for the same as the topic starter. (atleast i think hes looking for the same as i do)
How do u recieve a message through python?

sending:


owner.sendMessage("EnemyDied")

But how do u recieve a message with python?
so that i can use more subjects to look for on 1 object. (instead of using like 10 message sensors on an object)

Thanks,
Cotax

@@Cotaks
I don’t think there is any sort of python interface for receiving BGE messages. But If you want to go a purely python route, it is a pretty simple thing use your own message queues, like:


#the sender does something like

receiver['messages'].append( (me,'subject','body') )

# the receiver does something like
if me['messages']:
    (sender,subject,body) = me['messages'].pop(0)


The message sensor does that for you.

There is no reason to run Python to detect events.

This is different when sending messages:

  • If you send the same message multiple time it is easier to use the Message Actuator as it comes with a nice GUI
  • If you send the same message with same subject but with different body the Message Actuator is is still handy
  • If you want to send multiple messages within one frame Python code is probably better. You can still read the required configuration from an message actuator to benefit from the GUI. Just make sure to skip activation ;).

@hobomatic, Thanks, i will look into that.

@monster, Ok so its best to stick with sensors then.
I was wondering this becouse u can send messages with python but coulnd find how to receive them.
And it looked great if it was easy to setup. Anyway ill stick to sensors then.

Thanks both of you.

Hey, I figured it out… I don’t know why it was working the way it was. but previously I had my logic block message set to receive any subject, and just reset the subject in my program (using the messagesensor.subject = ‘blahblah’ code). After doing a test, I realized that the subject did appear to be reset, it was just still accepting all subjects. I changed my logic block then, writing in a message subject that I knew did not exist, and then it worked fine. So it wasn’t me code after all.

Hello;

I figured out how to use just one message sensor to detect multiple specific messages:

1 Message Sensor - Multiple Messages receiver

By zADAN LAVEY

cont = GameLogic.getCurrentController()
obj = cont.owner

sens = cont.sensors[“sensor”]

if sens.subjects.count(“Message1”) == 1:
obj.applyRotation([0.05, 0.0, 0.0], 0)

if sens.subjects.count(“Message2”) == 1:
obj.applyRotation([0.0, 0.05, 0.0], 0)

if sens.subjects.count(“message3”) == 1:
obj.applyRotation([0.0, 0.0, .05], 0)

#for test
#print sens.bodies
#print sens.frameMessageCount
#print sens.subjects

Hope this helps.
Here is an example for B 2.49:
http://www.4shared.com/file/lO8QUl-_/Message_Sensor__1LB_Multiple_M.html
It will be easy to translate it to new python version.

zL

What if two messages with the subject “message3” are received?

Hello Monster;

That’s not a Problem, if more than one actuator is sending the same message (Subject) we just need to change a bit the script:

if sens.subjects.count(“Message3”) >= 1:
obj.applyRotation([0.05, 0.0, 0.0], 0)

With this change, no matter how many actuators send the same message (Subject), it will works.

Thanks for your question.

zL