Message Python Help

Hi all, I’ve been having some trouble figuring how messages work, this is my code:


from bge import logic, events
#a player from another scene
def message():
    cont = logic.getCurrentController()
    key = logic.keyboard.events
    space = key[events.SPACEKEY]
    message = cont.actuators['Message']
    if space:
        message.subject = "Jump!"
        cont.activate(message)

#the player
def jump():
    cont = logic.getCurrentController()
    message = cont.sensors['Message']
    motion = cont.actuators['Motion']
    if message.subject == "Jump!":
        print("HI")
    cont.deactivate(motion)

    

Or is this not how messages work? Please help. It’s just been a wall after another.
Here’s a blend file.test2.blend (462 KB)

Well, I am not sure, but I will help, I use logic, and am just getting into python,

to pass stuff from one scene to another, your message can not be a message, it has to be a global I believe,

And how do you do that?

Isn’t this for changing global dict.? I need to know how to do messages with python.

#a player from another scene

I thought you were referring to another scene, not in the same .blend
messages are easy enough,

sendMessage(subject,body="",to="")
Sends a message.
[TABLE=“class: docutils field-list”]
[TR=“class: field”]
[TH=“class: field-name, align: right”]Parameters:[/TH]

  • subject (string) – The subject of the message
  • body (string) – The body of the message (optional)
  • to (string) – The name of the object to send the message to (optional)

[/TR]
[/TABLE]
This is easy enough right?

the other stuff is for editing a message actuator, which is not super useful as you are using python,

Okay. I just read the game loop. Uh. I still have some questions though. How do you code messages? Maybe an example of a script of how it should be done? I read it over and over again and didn’t quite get the answer. But still, thank you monster.


#the player 
def jump():     
    cont = logic.getCurrentController()     
    message = cont.sensors['Message']     
    motion = cont.actuators['Motion']
    subjs = message.subjects
    if message.positive:         
        if "Jump" in subjs[0]:             
            print("HI")         
            cont.deactivate(motion)

I don’t know why but I do this and it work for me

three ways of sending messages:

A) Use the GUI to configure the Message Actuator
B) Use Python to to configure the Message Actuator (as you already do)
send it by activating the actuator (can be done by any connected controller)

C) Use the python API to send messages (look at the Blender API see bge.logic)

two ways of receiving:
A) Receive filtered messages. Which means you set a subject this sensor is listening to. Only messages with the set subject will be evaluated by this sensor.
B) Receive all messages. This means you need to filter via Python … see below.

@bankbaa:
please use variable names that you can speak out ;).

Yes, it is a good idea to check the status of the sensor.
The subjects of the received messages are in the “subjects” attribute (not to be confused with the “subject” attribute). Keep in mind attribute “subjects” references a list.

yeah indeed subjs it should be subjects or subjectlist
and I have to learn more to be like you Prof.Monster:D
I’m very bad at explain:p.