Help with Messaging - keeps looping message (SOLVED)

So I’m relatively new to Defold and have a feeling this is a noobish mistake - I’m trying to send a message to another script - which is working - however the message is being sent infinitely, and as such the temporary “print” that I put in there is constantly sending.
In other words, I’m sending a limited number of messages however it is being received infinitely.

    elseif action_id == hash("test") then
		msg.post("spawnerRight#spawnNPCS", "leftMove")
    end

and on the spawnNPCS script

    if message_id == hash("leftMove") then
	    print("5")

However all I’m getting is an infinite loop of “5” in the console despite only sending a single message.
image
image
I’m sure this is just an issue of me not understanding the way messages work within Defold, but I can’t find any help on this issue anywhere, and it seems to be quite an easy to make mistake haha.
Thanks in advance!

1 Like

The action gets sent all the while the key is pressed. You need to add “and action.pressed” to the condition, like this:

 elseif action_id == hash("test") and action.pressed then
	msg.post("spawnerRight#spawnNPCS", "leftMove")
 end

This way the action only gets sent when the key is first pressed and doesn’t get repeated until you release it.

3 Likes

Haha, I knew it would be a ridiculously simple issue :smiley: Thanks for your help.

2 Likes

Otherwise I wouldn’t have been able to help =P

So I did as you recommended, however it’s still sending an infinite number of prints.

elseif action_id == hash("test") and action.pressed then
	msg.post("spawnerRight#spawnNPCS", "leftMove")
end

I’m still getting this though
image
It also isn’t just hitting a large number and stopping, it’s doing something I can’t describe with images which is constantly cycling between 900 and 999 constantly.
Any help would be nice lol.

1 Like

You need to share more code. It’s really hard to help when you’re unable to see the whole script.

1 Like

In what function is the following code located?

if message_id == hash("leftMove") then
	    print("5")

(It should be located in your on_message function. :slight_smile: )

Is there somewhere else in your code that you send “leftMove”?

Here is a super minimal example using the code you shared, and it behave as expected.

As @britzl pointed out, we need some more details to understand what could be wrong. :slight_smile:

So after opening Defold again to look it over it’s now working when I’m running it - I don’t know why it took me restarting defold for it to work and why rebuilding it didn’t do it, but atleast it’s working.
Thanks @britzl for the advice, will do so next time I have an issue :smiley: I don’t really use forums of any kind but thought I’d give it a try as I was desperate for a solution, so didnt really know what to do lol.
Thanks for being patient with me guys

2 Likes