I am recieving three values in a guiscript (via message)
however, i cant store these values in local variables for later use.
how can i store the message attributes ( message.myname, message.mybuildlevel) for later access in
on_input function?
Now it only show nil if i try to access the self.myname
function on_message(self, message_id, message, sender)
--safe attributes
self.mybuildtype=message.mybuildtype
self.myname=message.myname
self.mybuildlevel=message.mybuildlevel
--show menu buttons
msg.post(".", "enable")
local n1 = gui.get_node("general")
gui.set_position(n1, vmath.vector3(message.tx, message.ty, 0))
end
function on_input(self, action_id, action)
if action_id == hash("click") and gui.pick_node(gui.get_node("bt0-menuspot1"), action.x, action.y) and
action.pressed then
print("myn:" .. self.myname)
print("mybt:" .. self.mybuildtype)
end
end
The code looks good from what I can tell. Can you please print in on_message and see that you are actually receiving a message?
Could it be that the on_message function is receiving multiple messages and that some of them do not have the mybuildtype, myname etc? If that is the case then you need to check message_id and only if it’s the right type of message set self.mybuildtype etc
ok. britzl was right. there were other messages incoming without the attributes, thus the original values from the first call were replaced by nil. now for other people to see what i am passing around:
a) a game object sends a message to the gui with some parameters
local function do_click(self, url, message)
message.myname=self.myname
message.mybuildtype=self.mybuildtype
message.mybuildlevel=self.mybuildlevel
msg.post("/menu#gui", "show", message)
end
b) the gui object recieves the message and stores it
function on_message(self, message_id, message, sender)
if message_id == hash("show") then
self.mybuildtype=message.mybuildtype
self.myname=message.myname
self.mybuildlevel=message.mybuildlevel
self.sender=sender
-
msg.post(".", "enable")
local n1 = gui.get_node("general")
gui.set_position(n1, vmath.vector3(message.tx, message.ty, 0))
end
end
c) later when a menu button of the gui is pressed i can increase one of the stored values
and call back the original sender (game object).
function on_input(self, action_id, action)
if action_id == hash("click") and gui.pick_node(gui.get_node("bt0-menuspot1"), action.x, action.y) and action.pressed then
self.mybuildlevel=self.mybuildlevel+1
msg.post(self.sender,"do_build", { level=self.mybuildlevel })
end
end
d) the game object reacts by storing the new variable and changing its animation
function on_message(self, message_id, message, sender)
if message_id == hash("do_build") then
self.mybuildlevel=message.level
msg.post("#sprite", "play_animation", {id = hash("animation2")})
end
end