Sprite.play_flipbook appears to be interpreted by script

Hi!
I have the following code triggered by a message from a third party.

The line “sprite.play_flipbook(“face#mouth”, “mouth1”)” triggers the message function- in fact, when I don’t grey out the second “sprite.play_flipbook(“face#mouth”, “mouth2”)”, the message function loops indefinitely.

Can any one say why this is?

Thanks!

function on_message(self, message_id, message, sender)
	print("psych x recd")
	if self.xed == false then 
		self.xed = true
		go.set("mushrooms", "position.z", 0)
		go.set("liney", "position.z", 0)
		go.set("liney1", "position.z", 0)
		sprite.play_flipbook("face#mouth", "mouth1")
	else
		--sprite.play_flipbook("face#mouth", "mouth2")
		go.set("mushrooms", "position.z", -100000)
		go.set("liney", "position.z", -100000)
		go.set("liney1", "position.z", -100000)
	end
end

That sounds strange.
What is the message id(s) that you receive in this loop?

{ --[[0x121d6ec70]]
  id = hash: [mouth1],
  current_tile = 1
}

I have solved this with a

if message[1] then

but I still get that other message from the line (in the post I made above) from the following line:


sprite.play_flipbook("face#mouth", "mouth1")

Greying out that line with double dash means the message isn’t set to the script.

I perhaps misinterpreted your comment as “the program hangs”, but I guess it doesn’t hang?

What is the actual url/name of the sprite that you want to send to?
(remember that sprite.play_flipbook() is just an alias for the message play_animation)

Also, since I don’t think the issue/solution is very obvious, it might be good if you shared a repro case that someone could test.

I somehow don’t get what you are doing .
For example, you say the line triggers the message function. How so?
Why do you put the blank code in the message function, without listening to a specific event? Your code basically reacts to every event that comes through.
I don’t know if it helps, but i would do it like so, without knowing the rest of your code :

local function set_mouth(mouth, zPos)
  sprite.play_flipbook("face#mouth", mouth)
  go.set("mushrooms", "position.z", zPos)
  go.set("liney", "position.z", zPos)
  go.set("liney1", "position.z", zPos)
end

local function on_msg_set_mouth(msg)
  if not self.xed then
    self.xed = true
    set_mouth("mouth1", 0)
  else
    set_mouth("mouth2", -100000)
end

function on_message(self, message_id, message, sender)
  if message_id == hash("mouth1") then
    on_msg_set_mouth(message)
  end
end

Look at my collection structure:

I send a message (from another collection) to “/everything#psychedelic” which has the following function:

function on_message(self, message_id, message, sender)
	print("psych x recd")
	sprite.play_flipbook("face#mouth", "mouth1")
end

The debug monitor prints “psych x recd” over and over and over again.
Removing the line “sprite.play_flipbook(“face#mouth”, “mouth1”)” solves the problem.

The sprite does correctly change. I have quickly added a boolean to check what the message_id is so the problem is an easy fix.

The message that prints when “sprite.play_flipbook(“face#mouth”, mouth)” is received by the script is

{ --[[0x120dce880]]
id = hash: [mouth1],
current_tile = 1
}

Hope the post above is OK for repro. I honestly don’t understand what is happening but usually this sort of thing is my fault :sweat_smile:

This is not surprising. You are calling sprite.play_flipbook() without a callback function. And since you don’t specify a callback function your script will receive an "animation_done"message each time the animation competes. And each time you receive the “animation_done” message your start a new animation. And this goes on forever and ever :slight_smile:

You really should check the message_id and not run the code on any random incoming message.

1 Like

Got it! Wasn’t aware of the callback function. THANKS

Like @britzl said, you should verify the message ids in your message function.