Does msg.post redirect return values from on_message? (SOLVED)

I’m setting my camera’s position through msg.post. In the same way I want to use the messaging api to retrieve the current position but as it seems it’s not working. Does msg.post redirect return values from on_message?

local pos =  msg.post("camera#cameraController", "get_camera_position")

camera#cameraController

    function on_message(self, message_id, message, sender)
    -- Add message-handling code here
    -- Camera view and projection arrives here. Store them.
 
    if message_id == hash("set_camera_position") then
        go.set_position(message.pos)
    end
    
    if message_id == hash("get_camera_position") then
        return go.get_position()
    end
    
end

No it does not send back values by itself, you have to handle that yourself.
If you want to do with message functions you will have to send one back

function init(self)    
    msg.post("camera#cameraController", "get_camera_position")
    self.camera_position = 0
end

function on_message(self, message_id, message, sender)
    if message_id == hash("get_camera_position") then
        self.camera_position = message.camera_position
    end
end

camera#cameraController

function on_message(self, message_id, message, sender)
   -- Add message-handling code here
   -- Camera view and projection arrives here. Store them.
    if message_id == hash("set_camera_position") then
        go.set_position(message.pos)
    end
    if message_id == hash("get_camera_position") then
        msg.post(sender, message_id, {camera_position = go.get_position("camera_object")})
    end
end

Code is untested

3 Likes

Thanks for that snippet, I got the idea.

In this case I would recommend that you use go.get_position("camera#cameraController") to get the position without the need for message passing.

Or is there some particular reason that you want to use message passing instead?

I’ll use go.get_position("camera#cameraController"), it fits my needs :slight_smile: There was no particular reason why I wanted to use the message api, I’m just new to Defold and wasn’t aware of that. Thanks!

1 Like