# Monarch doesn't trigger sequenced transitions correctly

**URL:** <https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699>\
**Category:** Questions\
**Created:** [February 6, 2025, 9:17pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699 "2025-02-06T21:17:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![M.Alkhatib](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/m.alkhatib/32/32391_2.png) [@M.Alkhatib](https://forum.defold.com/u/M.Alkhatib)\
**Post date:** [February 6, 2025, 9:17pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/1 "2025-02-06T21:17:06Z")

</div>

Hello fellow Defolders!

I’m new to Defold, and Lua in general. I came here to port a nice and complete game I’ve made using libGDX and I really like Defold so far, it’s simple, extensive, and has great compatibility and performance.

Now, I’m using many extensions to help me reduce the stress, including: Monarch, Druid, Defold-Saver, and Defold-Lang.

So far, I can say that the only thing that’s missing is really just beginner-friendly documentation. Not just for Monarch, but for most extensions.

So, my current issue is that I’m stuck with Monarch, I’m trying to re-create an effect of a fading-in initial-screen that then automatically switches (fades-out) to the next screen (e.g. the “menu”).

However, it seems like I’m handling it wrongly. The main framework I have is really simple, we have the main “controller” that includes the collection-proxies for the screen and the built-in monarch script for each parent of the proxies (e.g. `/monarch/screen_proxy.script`), and of course a simple script that handles the switching of the screens.

This is is the simple snippet that controls everything:

```lua
function on_message(self, message_id, message, sender)
    if message_id == hash("switch") then
        monarch.show(message.scene)
    end
end

```

And my main “init” function looks like this:

```lua
function init(self)
    lang.init()
    druid.set_text_function(lang.txp)
    monarch.debug()
    msg.post(".", "acquire_input_focus")
    msg.post("#", "switch", { scene = "initial" })
end

```

And so far everything’s great… the “initial” screen initializes and shows correctly:

```lua
function init(self)
    self.transition = transitions.create()
    self.transition.show_in(gui.get_node("root"), transitions.fade_in, gui.EASING_LINEAR, 0.7, 0)
    self.transition.show_in(gui.get_node("logo"), transitions.slide_in_left, gui.EASING_LINEAR, 0.3, 0)
    self.transition.show_out(gui.get_node("logo"), transitions.slide_out_right, gui.EASING_LINEAR, 0.3, 0)
    self.transition.show_out(gui.get_node("root"), transitions.fade_out, gui.EASING_LINEAR, 0.7, 0)
    monarch.on_transition("initial", self.transition)
    monarch.add_listener()
end

function final()
    monarch.remove_listener()
end

function on_message(self, message_id, message, sender)
    monarch.on_message(message_id, message, sender)
    if message_id == monarch.SCREEN_TRANSITION_IN_FINISHED then
        msg.post("main:/scene", "switch", { scene = "menu" })
    end
end

```

So, where’s the issue you might ask? Now, the issue comes whenever the “initial” screen wants to fade-out and the “menu” screen wants to fade-in… Then the transition just bugs-out.

And in case you’re wondering, here’s how I initialize my menu screen:

```lua
function init(self)
    self.transition = transitions.create(gui.get_node("root"))
    self.transition.show_in(transitions.fade_in, gui.EASING_LINEAR, 0.5, 0)
    self.transition.back_in(transitions.fade_in, gui.EASING_LINEAR, 0.5, 0)
    self.transition.show_out(transitions.fade_out, gui.EASING_LINEAR, 0.5, 0)
    self.transition.back_out(transitions.fade_out, gui.EASING_LINEAR, 0.5, 0)
    monarch.on_transition("menu", self.transition)
    monarch.add_listener()
end

... SOME OTHER CODE HERE ...

function final(self)
    monarch.remove_listener()
end

function on_message(self, message_id, message, sender)
    monarch.on_message(message_id, message, sender)
end

```

So, hopefully the professionals among the community can already see the issue (Though it might not be a code-issue but rather a structural one)… If not, maybe a minimal, reproductible example would help?

You can download it here:  
[Defold-Monarch-Issue.zip](https://forum.defold.com/uploads/short-url/2vo5xIHLyVsnUMlxC1ENJmLqUZQ.zip) (1.9 MB)

I really hope that we may find a solution together, thanks!

---

<div class="post-metadata">

**Author:** ![NaakkaDev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/naakkadev/32/34927_2.png) [@NaakkaDev](https://forum.defold.com/u/NaakkaDev)\
**Post date:** [February 7, 2025, 6:52am UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/2 "2025-02-07T06:52:23Z")

</div>

This piece of code gets triggered twice. Once after **scene\_a** transition has finished and then again after **scene\_b** transition has finished.

```lua
	if message_id == monarch.SCREEN_TRANSITION_IN_FINISHED then
		msg.post("main:/controller", "switch", { scene = "scene_b" })
	end

```

This happens because the **scene\_a** is still in the screen stack after switching to **scene\_b**. There are multiple ways to fix this, depending on your needs. One way is to replace the **scene\_a** with **scene\_b** and another way is to add a condition to the on\_message() in **scene\_a** like so:

```lua
	if message_id == monarch.SCREEN_TRANSITION_IN_FINISHED and sender.path == hash("/scene_a") then
		msg.post("main:/controller", "switch", { scene = "scene_b" })
	end

```

Btw. you can see your monarch screen stack at any time with `pprint(monarch.get_stack())`.

**—**

Now I’ll tell you how I quickly found out the issue; I call it the _print farming_ as it often gives good yields. 😁

I put `print("SCENE_A")` and `print("SCENE_B")` in each of the scenes init() functions and when running build this is what happens:

 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/0/8/085c89573e9a128c2279e31820c26063ef68dfff.png)

It becomes clear that the **scene\_b** init() function is triggering twice.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 7, 2025, 7:39am UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/3 "2025-02-07T07:39:43Z")

</div>

I wonder if maybe there is something I could do to improve this within Monarch itself? If you are able to share a minimal repro case in an issue on GitHub then I’ll promise to take a look!

---

<div class="post-metadata">

**Author:** ![M.Alkhatib](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/m.alkhatib/32/32391_2.png) [@M.Alkhatib](https://forum.defold.com/u/M.Alkhatib)\
**Post date:** [February 7, 2025, 6:29pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/4 "2025-02-07T18:29:26Z")

</div>

Ok, so I managed to somewhat fix the issue you mentioned using two solutions, but the effect is still wrong…

As you mentioned, there has to be a condition to ensure that we’re tracking the right message.

So, similarly to your solution, I did this:

```lua
function on_message(self, message_id, message, sender)
	monarch.on_message(message_id, message, sender)
	if message_id == monarch.SCREEN_TRANSITION_IN_FINISHED and hash("scene_a") == message.screen then
		msg.post("main:/controller", "switch", { scene = "scene_b" })
	end
end

```

And fair enough, after some testing, the `init()` of “scene\_b” is no longer triggered twice.

**However, the issue still persists…**

I’ve also tried various other solutions, such as using `timer.delay`, following the documentation (which didn’t work, maybe because it’s outdated?), copying code from other repositories, etc.

The open-sourced games that use Monarch seem to transition to the next screen only when an event occurs, so the player has to manually press a button or so. That’s not what I need, I need it to be automatic (so the screen transitions are seamlessly played in a sequence), like so:

`scene_a` show in → `scene_a` show out → `scene_b` show in → …

I pushed it to GitHub in case you want to see the code online:

- [GitHub - nulldoubt/Defold-Monarch-Issue-1 · GitHub](https://github.com/nulldoubt/Defold-Monarch-Issue-1)
- [Defold-Monarch-Issue-1.zip](https://github.com/nulldoubt/Defold-Monarch-Issue-1/archive/refs/heads/master.zip) (1.9 MB)

---

<div class="post-metadata">

**Author:** ![M.Alkhatib](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/m.alkhatib/32/32391_2.png) [@M.Alkhatib](https://forum.defold.com/u/M.Alkhatib)\
**Post date:** [February 7, 2025, 6:42pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/5 "2025-02-07T18:42:09Z")

</div>

So, thank god, I found a workaround!

When the effect happens immediately (`delay = 0`) then it doesn’t actually bug out, it just plays it immediately and doesn’t wait till the first effect is done (intended behavior I guess).

Adding a simple and precise delay made the issue fade away 👌!

I added the delay to the `show_in`transition of `scene_b` and it works flawlessly:

```lua
self.transition.show_in(transitions.fade_in, gui.EASING_LINEAR, 0.5, 0.5)

```

(The last parameter is the delay in seconds).

Nevertheless, great thanks to the support of this community!

---

<div class="post-metadata">

**Author:** ![M.Alkhatib](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/m.alkhatib/32/32391_2.png) [@M.Alkhatib](https://forum.defold.com/u/M.Alkhatib)\
**Post date:** [February 7, 2025, 6:44pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/6 "2025-02-07T18:44:30Z")

</div>

Maybe you could add an option to “make the animation wait before playing”? That way one can create predictably/sequenced animations without them mixing up…

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 8, 2025, 12:54pm UTC](https://forum.defold.com/t/monarch-doesnt-trigger-sequenced-transitions-correctly/79699/7 "2025-02-08T12:54:41Z")

</div>

I can take a look. Please create an issue on GitHub!
