• Stars
    star
    144
  • Rank 248,394 (Top 6 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Monarch is a Defold screen manager with transition support and a straight forward API

Build Status Code Coverage Latest Release

Monarch

Monarch is a screen manager for the Defold game engine.

Installation

You can use Monarch in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/britzl/monarch/archive/master.zip

Or point to the ZIP file of a specific release.

Usage

Using Monarch requires that screens are created in a certain way. Once you have one or more screens created you can start navigating between the screens.

Editor Script

Right click in on a.gui file in the outline and selected the menu item, it creates a .collection and a .gui_script with the same name as the .gui file. It adds the file with some basic setup done to them, adding the selected gui script to the created gui scene and in turns adds the gui scene to the newly created collection.

Creating screens

Monarch screens are created in individual collections and either loaded through collection proxies or created through collection factories.

Collection proxies

For proxies the recommended setup is to create one game object per screen and per game object attach a collection proxy component and an instance of the screen_proxy.script provided by Monarch. The screen_proxy.script will take care of the setup of the screen. All you need to do is to make sure that the script properties on the script are correct:

  • Screen Proxy (url) - The URL to the collection proxy component containing the actual screen. Defaults to #collectionproxy.
  • Screen Id (hash) - A unique id that can be used to reference the screen when navigating your app.
  • Popup (boolean) - Check this if the screen should be treated as a popup.
  • Popup on Popup (boolean) - Check this if the screen is a popup and it can be shown on top of other popups.
  • Timestep below Popup (number) - Timestep to set on screen proxy when it is below a popup. This is useful when pausing animations and gameplay while a popup is open.
  • Screen Keeps Input Focus When Below Popup (boolean) - Check this if the screen should keep input focus when it is below a popup.
  • Others Keep Input Focus When Below Screen (boolean) - Check this if other screens should keep input focus when below this screen.
  • Transition Url (url) - DEPRECATED Optional URL to post messages to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on transitions).
  • Focus Url (url) - DEPRECATED Optional URL to post messages to when the screen gains or loses focus (see the section on screen focus).
  • Receiver Url (url) - DEPRECATED Optional URL to post messages to using monarch.post().
  • Preload (boolean) - Check this if the screen should be preloaded and kept loaded at all times. For a collection proxy it means that it will be async loaded but not enabled at all times while not visible. This can also temporarily be achieved through the monarch.preload() function.

Collection factories

For factories the recommended setup is to create one game object per screen and per game object attach a collection factory component and an instance of the screen_factory.script provided by Monarch. The screen_factory.script will take care of the setup of the screen. All you need to do is to make sure that the script properties on the script are correct:

  • Screen Factory (url) - The URL to the collection factory component containing the actual screen. Defaults to #collectionfactory.
  • Screen Id (hash) - A unique id that can be used to reference the screen when navigating your app.
  • Popup (boolean) - Check this if the screen should be treated as a popup.
  • Popup on Popup (boolean) - Check this if the screen is a popup and it can be shown on top of other popups.
  • Screen Keeps Input Focus When Below Popup (boolean) - Check this if the screen should keep input focus when it is below a popup.
  • Others Keep Input Focus When Below Screen (boolean) - Check this if other screens should keep input focus when below this screen.
  • Transition Id (hash) - DEPRECATED Optional id of the game object to send a message to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on transitions).
  • Focus Id (hash) - DEPRECATED Optional id of the game object to send a message to when the screen gains or loses focus (see the section on screen focus).
  • Preload (boolean) - Check this if the screen should be preloaded and kept loaded at all times. For a collection factory this means that its resources will be dynamically loaded at all times. This can also temporarily be achieved through the monarch.preload() function.

Note: Monarch supports dynamic collection factories (ie where the "Load Dynamically" checkbox is checked).

Nesting screens

Sometimes it might be desirable to have a screen that contains one or more sub-screens or children, for instance popups that are used only by that screen. Monarch supports nested screens only when the parent screen is created via a collection factory. If the parent screen is loaded via a collection proxy the sub/child-screens won't be able to receive any input.

Navigating between screens

The navigation in Monarch is based around a stack of screens. When a screen is shown it is pushed to the top of the stack. When going back to a previous screen the topmost screen on the stack is removed. Example:

  • Showing screen A
  • Stack is [A]
  • Showing screen B
  • Stack is [A, B] - (B is on top)
  • Going back
  • Stack is [A]

Showing a new screen

You show a screen in one of two ways:

  1. Post a show message to the screen script (either screen_proxy.script or screen_factory.script)
  2. Call monarch.show() (see below)

Showing a screen will push it to the top of the stack and trigger an optional transition. The previous screen will be hidden (with an optional transition) unless the screen to be shown is a popup.

NOTE: You must ensure that the init() function of the screen script (either screen_proxy.script or screen_factory.script) has run. The init() function is responsible for registering the screen and it's not possible to show it until this has happened. A good practice is to delay the first call by posting a message to a controller script or similar before calling monarch.show() the first time:

function init(self)
	msg.post("#", "show_first_screen")
end

function on_message(self, message_id, message, sender)
	monarch.show(hash("first_screen"))
end

Preventing duplicates in the stack

You can pass an optional clear flag when showing a screen (either as a key value pair in the options table when calling monarch.show() or in the message). If the clear flag is set Monarch will search the stack for the screen in question. If the screen already exists in the stack and the clear flag is set Monarch will remove all screens between the current top and the screen in question. Example:

  • Stack is [A, B, C, D] - (D is on top)
  • A call to monarch.show(B, { clear = true }) is made
  • Stack is [A, B]

As opposed to if the clear flag was not set:

  • Stack is [A, B, C, D] - (D is on top)
  • A call to monarch.show(B, { clear = false }) is made
  • Stack is [A, B, C, D, B] - (B is on top)

Showing a screen without adding it to the stack

Monarch can also show a screen without adding it to the stack. This can be used to for instance load a collection containing a background that you want to have visible at all times. You show and hide such a screen like this:

-- show the background without adding it to the stack
monarch.show(hash("background"), { no_stack = true })

-- hide the background
monarch.hide(hash("background"))

Going back to a previous screen

You navigate back in the screen hierarchy in one of two ways:

  1. Post a back message to the screen script (either screen_proxy.script or screen_factory.script)
  2. Call monarch.back() (see below)

Input focus

Monarch will acquire and release input focus on the game objects containing the proxies to the screens and ensure that only the top-most screen will ever have input focus. The screen settings above provide a Screen Keeps Input Focus When Below Popup and Others Keep Input Focus When Below Screen setting to override this behavior so that a screen can continue to have focus. This is useful when you have for instance a tabbed popup where the tabs are in a root screen and the content of the individual tabs are separate screens. In this case you want the tabs to have input as well as the tab content.

Popups

A screen that is flagged as a popup (see list of screen properties above) will be treated slightly differently when it comes to navigation.

Popup on normal screen

If a popup is shown on top of a non-popup the current top screen will not be unloaded and instead remain visible in the background:

  • Stack is [A, B]
  • A call to monarch.show(C) is made and C is a popup
  • Stack is [A, B, C] and B will still be visible

Popup on popup

If a popup is at the top of the stack and another popup is shown the behavior will depend on if the new popup has the Popup on Popup flag set or not. If the Popup on Popup flag is set the underlying popup will remain visible.

  • Stack is [A, B, C] and C is a popup
  • A call to monarch.show(D) is made and D is a popup with the popup on popup flag set
  • Stack is [A, B, C, D]

If the Popup on Popup flag is not set then the underlying popup will be closed, just as when showing a normal screen on top of a popup (see above).

  • Stack is [A, B, C] and C is a popup
  • A call to monarch.show(D) is made and D is a popup without the popup on popup flag set
  • Stack is [A, B, D]

Screen on popup

If a screen is shown on top of one or more popups they will all be removed from the stack:

  • Stack is [A, B, C, D] and C and D are popups
  • A call to monarch.show(E) is made and E is not a popup
  • Stack is [A, B, E]

Transitions

You can add optional transitions when navigating between screens. This is described in detail here.

Screen focus gain/loss

Monarch will send focus gain and focus loss messages if a focus change listener has been set using monarch.on_focus_change(screen_id, fn)

DEPRECATED: Monarch will send focus gain and focus loss messages if a Focus Url (proxy) or Focus Id (collectionfactory) was provided when the screen was created.

The focus gained message will contain the id of the previous screen and the focus loss message will contain the id of the next screen. Example:

	local monarch = require "monarch.monarch"

	function init(self)
		monarch.on_focus_changed("foobar", function(message_id, message, sender)
			if message_id == monarch.FOCUS.GAINED then
				print("Focus gained, previous screen: ", message.id)
			elseif message_id == monarch.FOCUS.LOST then
				print("Focus lost, next screen: ", message.id)
			end
		end)
	end

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

Callbacks

Both the monarch.show() and monarch.back() functions take an optional callback function that will be invoked when the transition_show_in (or the transition_back_in in the case of a monarch.back() call) transition is completed. The transition is considered completed when a transition_done message has been received (see section on transitions above).

Monarch API

The full Monarch API is documented here.

More Repositories

1

publicexamples

Examples for the Defold engine
230
star
2

gooey

Defold GUI system
Lua
138
star
3

defold-orthographic

Orthographic camera functionality for the Defold game engine
Lua
130
star
4

defold-input

Simplify input related operations such as gesture detection, input mapping and clicking/dragging game objects
Lua
105
star
5

ludobits

Utilities for game development using the Defold engine
Lua
88
star
6

defold-richtext

Defold-RichText is a system to create styled text based on an HTML inspired markup language
Lua
64
star
7

defnet

Defold networking examples
Lua
56
star
8

platypus

Defold platformer engine
Lua
44
star
9

extension-imgui

Dear ImGUI extension for Defold
C++
42
star
10

defcon

A developer console for games written using the Defold game engine
Lua
38
star
11

aws-sdk-lua

Auto generated AWS SDK for Lua
Lua
38
star
12

defold-metrics

Calculate and display performance metrics in Defold games
Lua
30
star
13

lumiere

A collection of post processing effects for Defold
Lua
30
star
14

emthree

Match 3 engine for Defold
Lua
29
star
15

deftest

Unit testing in Defold
Lua
27
star
16

defold-screenshot

Screenshot extension for the Defold game engine
C
27
star
17

steamworks-defold

Steamworks support for Defold using the Defold extension system
C++
27
star
18

defold-websocket

DEPRECATED - Use https://github.com/defold/extension-websocket instead!
Lua
25
star
19

boom

Boom is a game framework built on top of Defold
Lua
22
star
20

defold-sharing

Defold native extension to share data from a Defold application using native dialogs
C++
21
star
21

defpro

Defold Profiler interaction using Lua
Lua
20
star
22

template-lowres

Defold template project for lowres/pixel-art games.
GLSL
19
star
23

playfabexamples

PlayFab API examples for Defold
Lua
18
star
24

defold-png

Defold native extension to load and save PNG images
C
17
star
25

blip

Blip is a library to procedurally generate and play sound effects for games
C++
17
star
26

defads

Defold Ads Library Project - DEPRECATED!
Lua
15
star
27

defold-googleanalytics

Google Analytics implementation for the Defold game engine
Lua
15
star
28

luacov-cobertura

LuaCov to Cobertura report generator
Lua
13
star
29

roguearchive

Archive of old roguelike games in general and versions of Rogue in particular
HTML
12
star
30

throwacrow

Defold example of a slingshot style physics game
Go
11
star
31

defold-clipboard

Defold native extension to access the clipboard
C++
11
star
32

desert

(de)sert is a Lua table (de)serialiser for the Defold game engine
Lua
11
star
33

defold-bunnymark

Defold bunnymark test
Lua
10
star
34

defold-lfs

LuaFileSystem port to a Defold native extension
C++
10
star
35

manifestation

Defold App Manifest generator
HTML
9
star
36

defold-adaptive_icons

Adaptive Icons example for Android
9
star
37

defold-luasocket

The LuaSocket *.lua files, modified to play nicely with Defold. Also contains the mime.core files as a native extension.
Lua
9
star
38

stately

Simple Finite State Machine
Lua
9
star
39

pico8

Run Pico-8 Tweetjam carts in Defold
Lua
9
star
40

gbrausers

A game inspired by Luftrausers, made for GB Jam #5
Go
8
star
41

chipmunk2d-defold

Defold native extension for the Chipmunk 2D physics engine
C
8
star
42

linkandswitch

Defold linker and switcher game example
Lua
8
star
43

warbattles-rts

RTS sample project for the Defold game engine
Go
8
star
44

extension-filedrop

This extension provides a unified, simple to use interface to handle drag and drop of files onto a Defold application
JavaScript
8
star
45

pong

Defold implementation of the classic game Pong
Go
7
star
46

defold-training_intermediate

Go
7
star
47

androidutils

Small pieces of reusable Android stuff
Java
6
star
48

oneroom

Ludum Dare #37
Go
6
star
49

prettyprint

Lua print replacement that will print the contents of tables
Lua
6
star
50

lua-websocket_example

Lua and Defold websocket example based on lua-websocket and LuaSocket.
Lua
6
star
51

luatoxml

Converts a Lua value (table) into properly formatted XML
Lua
6
star
52

towerz

Defold version of Tipsy Towers
Go
5
star
53

numberz

Defold version of the zNumbers game
Go
5
star
54

defold-pcg

Procedural Content Generation algorithms and examples for Defold
Lua
5
star
55

lowrezadventure

Game for LOWREZJAM 2018 using the Defold game engine
Go
5
star
56

easel

Easel is used to switch render script content at run-time
Lua
5
star
57

defold-gyro

Gyroscope extension for the Defold game engine
C++
5
star
58

replicant

Example of simple multiplayer game mechanics.
Lua
5
star
59

defold-midi

MIDI for Defold
C
5
star
60

remoteplatformer

Lua
4
star
61

tweetfighter

You are Donald Trump.
Lua
4
star
62

lowrezinvaders

Lowrez Space Invanders clone for the Defold game engine
Lua
4
star
63

cards

Playing with card game mechanics in Defold
4
star
64

dialogflow-interactive-canvas-defold

JavaScript
4
star
65

facesnap

Camera and face recognition with AR overlay using Defold native extensions
C++
4
star
66

template-camera-and-input

Template project showing how to set up a camera and handle mouse input
Lua
4
star
67

defold-london

Exercise projects for the Defold London training January 2017
4
star
68

liverunner

Defold Live Update example in the form of a runner game
Go
4
star
69

defold-imp

Defold native extension to apply filters to an image buffer
C++
3
star
70

crashtool

Show information from a Defold crash file
JavaScript
3
star
71

gooey-kenney

Theme for Gooey - Kenney
Lua
3
star
72

screenshooter

Simplify the process of creating screenshots for app store listings
Go
3
star
73

gooey-dirtylarry

Theme for Gooey - Dirtylarry
Lua
3
star
74

broadsock

Super simple socket server with broadcast functionality
Lua
3
star
75

gooey-rpg

Theme for Gooey - RPG
Lua
3
star
76

stackem

Example of a block stacking game
Go
3
star
77

udgj-boss

Entry for UDGJ #1 jam
Lua
3
star
78

cgajam-2017

Zling - Defold game for CGA Jam 2017
Go
3
star
79

template-britzl

Defold template project with some britzl created extensions added
3
star
80

breakout

Breakout clone for the Defold game engine
Go
3
star
81

racer

Defold racing game
Go
3
star
82

shapeshift

Ludum Dare 35 competition entry made using the Defold game engine
Go
3
star
83

example-zombies-follow

Lua
3
star
84

wordball

ProcJam 2017
HTML
3
star
85

combochamp

A game created for the A Game By Its Cover 2019 game jam
Go
3
star
86

minimegaparty

Multiplayer one-button mini games inspired by the Wario Ware series of mini games. Created using the Defold game engine.
3
star
87

extension-paths

Get various Defold and system related paths
C++
2
star
88

oceancommotion-linker

Linker sample game made using the Defold engine
Go
2
star
89

bitmapfontcreator

Create bitmap fonts either at run-time or compile-time
Java
2
star
90

edelweiss

A game for A Game By Its Cover 2018
Go
2
star
91

famicase-rps

O / > - A Famicase 2017 game
Lua
2
star
92

luckyfishing

Submission for A Game By Its Cover 2016
Go
2
star
93

cucumber-defold

Cucumber for the Defold game engine
Lua
2
star
94

defold-builder

Defold build script to wrap bob.jar
Shell
2
star
95

mike_dangers

Defold implementation of the Mike Dangers game
Go
2
star
96

httpserver-python

Extension of SimpleHTTPServer for chunked transfer encoding and compressed content encoding
Python
2
star
97

ld40

Ludum Dare 40 Jam Entry. Theme: The more you have the worse it gets. -> The more humans, the worse it gets.
Go
2
star
98

defold-ldt

Defold LDT is an Execution Environment for LDT with support for the Defold game engine APIs
Python
2
star
99

gyrobomber

Gyro and AR based game using Defold native extensions
C
2
star
100

coverallsemma

Emma to Coveralls report converter
Java
2
star