Last active
March 15, 2017 06:05
-
-
Save ericroy/541a8285f767d29254f3ef5c67f8701c to your computer and use it in GitHub Desktop.
Usage example for saga.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local saga = require('lib.saga') | |
local function switch_scene(self, next_scene) | |
-- Unload the current scene (if there is one) | |
if self.scene then | |
local proxy_id = '#' .. self.scene | |
print('Unloading proxy: ' .. proxy_id) | |
msg.post(proxy_id, 'disable') | |
msg.post(proxy_id, 'final') | |
msg.post(proxy_id, 'unload') | |
local message_id, message, sender = saga.take(hash('proxy_unloaded')) | |
print('Proxy unloaded: ' .. proxy_id) | |
end | |
-- Load the next scene | |
local proxy_id = '#' .. next_scene | |
print('Loading proxy: ' .. proxy_id) | |
msg.post(proxy_id, 'load') | |
local message_id, message, sender = saga.take(hash('proxy_loaded')) | |
print('Proxy loaded: ' .. proxy_id) | |
msg.post(sender, 'init') | |
msg.post(sender, 'enable') | |
self.scene = next_scene | |
end | |
function init(self) | |
self.scene = null | |
self.saga = null | |
-- Load the first scene | |
msg.post('.', 'goto_scene', {name = 'main_menu'}) | |
end | |
function on_message(self, message_id, message, sender) | |
if self.saga then | |
self.saga = saga.continue(self.saga, message_id, message, sender) | |
else | |
if message_id == hash('goto_scene') then | |
self.saga = saga.create(switch_scene, self, message.name) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment