Post message with parameter not working (SOLVED)

I simply want to pass the integer value “score” from one script to another.
I get the following error: attempt to perform arithmetic on field ‘score’ (a nil value)

coin.script

function init(self)
self.collected = false
self.score = 5
end

function on_message(self, message_id, message, sender)
     if self.collected == false and message_id == hash("collision_response") then
        self.collected = true
        local offset = vmath.vector3(0, 20, 0)
        go.animate(go.get_id(), "position", go.PLAYBACK_ONCE_FORWARD, vmath.vector3(0, 900, 0),   go.EASING_INOUTSINE, 0.5, 0, update_score)
    elseif message_id == hash("start_animation") then
       pos = go.get_position()
       go.animate(go.get_id(), "position.y", go.PLAYBACK_LOOP_PINGPONG, pos.y + 24, go.EASING_INOUTSINE, 0.75, message.delay)
   end
end

function update_score()
    msg.post("level/score#gui","add_score", { score = 5 })
end

score.gui_script

-- how fast the score counts up per second
local score_inc_speed = 100

function init(self)
-- the target score is the current score in the game
self.target_score = 0
-- the current score being counted up towards the target score
self.current_score = 0
-- the score as displayed in the hud
self.displayed_score = 0
-- keep a reference to the node displaying the score for later use below
self.score_node = gui.get_node("score")
end

local function delete_star(self, star)
-- star has finished animation, delete it
gui.delete_node(star)
end

local function fade_out_star(self, star)
-- fade out the star before deletion
gui.animate(star, gui.PROP_COLOR, vmath.vector4(1, 1, 1, 0), gui.EASING_INOUT, 0.2, 0.0, delete_star)
end

local function spawn_stars(self, amount)
-- position of the score node, to be used for placing the stars
local p = gui.get_position(self.score_node)
-- distance from the position where the star is spawned
local start_distance = 0
-- distance where the star stops
local end_distance = 240
-- angle distance between each star in the star circle
local angle_step = 2 * math.pi / amount
-- randomize start angle
local angle = angle_step * math.random()
for i=1,amount do
    -- increment the angle by the step to get an even distribution of stars
    angle = angle + angle_step
    -- direction of the star movement
    local dir = vmath.vector3(math.cos(angle), math.sin(angle), 0)
    -- start/end positions of the star
    local start_p = p + dir * start_distance
    local end_p = p + dir * end_distance
    -- create the star node
    local star = gui.new_box_node(vmath.vector3(start_p.x, start_p.y, 0), vmath.vector3(30, 30, 0))
    -- set its texture
    gui.set_texture(star, "star")
    -- set to transparent
    gui.set_color(star, vmath.vector4(1, 1, 1, 0))
    -- fade in
    gui.animate(star, gui.PROP_COLOR, vmath.vector4(1, 1, 1, 1), gui.EASING_OUT, 0.2, 0.0, fade_out_star)
    -- animate position
    gui.animate(star, gui.PROP_POSITION, end_p, gui.EASING_NONE, 0.55)
end
end

function update(self, dt)
-- check if the score needs to be updated
if self.current_score < self.target_score then
    -- increment the score for this timestep to grow towards the target score
    self.current_score = self.current_score + score_inc_speed * dt
    -- clamp the score so it doesn't grow past the target score
    self.current_score = math.min(self.current_score, self.target_score)
    -- floor the score so it can be displayed without decimals
    local floored_score = math.floor(self.current_score)
    -- check if the displayed score should be updated
    if self.displayed_score ~= floored_score then
        -- update displayed score
        self.displayed_score = floored_score
        -- update the text of the score node
        gui.set_text(self.score_node, "score "..self.displayed_score)
        -- set the scale of the score node to be slightly bigger than normal
        local s = 1.3
        gui.set_scale(self.score_node, vmath.vector3(s, s, s))
        -- then animate the scale back to the original value
        s = 1.0
        gui.animate(self.score_node, gui.PROP_SCALE, vmath.vector3(s, s, s), gui.EASING_OUT, 0.2)
        -- spawn stars
        --spawn_stars(self, 4)
    end
end
end

function on_message(self, message_id, message, sender)
if message_id == hash("add_score") then
self.target_score = self.target_score + message.score
end

end

Is self.target_score set to an initial value of 0… or is this the first time your accessing it?

It’s the only thing I can see that could be the problem. if you have any other details or could provide the full code for the script, I might could help further.

Also, you can use the print method…

print(message.score)

This should help you debug and know for sure if the message.score value is passed.

Please see my edited question. I provided my full code.
The print of message.score is nil.

And you don’t have any other code somewhere that could be sending the message?

I only ask because this functionality is used all over the place and I don’t see anything wrong with your code.

3 Likes

This was the problem :confused:
I previously copied my code and did not change it.

Thank you very much for your help!

1 Like

No problem! Glad you found it :slight_smile:

1 Like