How to get a result back from storyboard.showOverlay()?

Hi,

Is there a way to get a result back from a call to storyboard.showOverlay()? I know you can pass parameters INTO the overlay with this function, but how do I get a result OUT of it once the overlay is closed?

For example:

The game scene could have a button that opens an overlay that asks the user a question and has the user answer either yes or no with two buttons respectively. How would I get the user’s input from this question without using a horrible global variable or some generic storage variable attached to the storyboard object?

Game scene:

-- the following event is dispatched once the overlay is in place function scene:overlayBegan( event ) print( "Showing overlay: " .. event.sceneName ) end -- the following event is dispatched once overlay is removed function scene:overlayEnded( event ) print( "Overlay removed: " .. event.sceneName ) -- Anyway to access data here passed back from the overlay scene????? end

Overlay scene:

local function yesButton\_onReleaseListener(event) -- Any way to pass data back to the calling scene to be used in scene:overlayEnded???????? storyboard.hideOverlay("slideUp", 250) return true -- indicates successful touch end

i think the way it’s intended to be used is to pass a params table in the options during showOverlay() from the underlying scene.  assuming that table (or at least one of its properties) references a table that is still in scope when you later return from hiding the overlay, then you can access the modified values, fe:

local returnvalues = {} -- at module scope   -- then elsewhere: storyboard.showOverlay("overlay", { params=returnvalues })   -- then in overlay: local returnvalues = nil -- at module scope, does nothing as is, only serves to "document" intent to use   function scene:create(event)   returnvalues = event.params -- grab it, save it   -- later local function onCancelPress(event)   returnvalues.whatDidTheyPress = "cancel"   storyboard.hideOverlay()   -- back in underlying scene function scene:overlayEnded(event)   if (returnvalues.whatDidTheyPress=="cancel") then...

that’s quick pseudo-code, just to convey the idea, don’t expect it to run as written.

all that works, but is a bit clunky.  if you’re up for it, legacy storyboard is open-sourced, and it’s trivially easy to add a “params” option to hideOverlay and attach it onto the event created for overlayEnded (exactly mimicing how showOverlay works).  that way you don’t need ANY “permanent” storage for your return values.

fwiw, hth

There are a few ways to pass back parameters from an overlay. 

  1. Send in a table as one of the parameters (options -> params) into overlay when it is created. Table is pass-by-reference in Lua.

  2. Create a global variable and use that to update. Easy to use, but global variables is something that needs extra care.

  3. If you are using COMPOSER (instead of storyboard). Use the event.parent, to call a function in the parent (use this to send back data too).

I use #1 above like this :

local dataTable = {} function scene:overlayEnded( event ) print( "Overlay removed: " .. event.sceneName ) print( "Data From Overlay : " , dataTable.dataFromOverlay ) end local options = { effect = "fade", time = 400, params = { dataToCapture = dataTable, } } storyboard.showOverlay( "overlay\_scene", options ) -- In overlay scene, grab the param and update it. function scene:createScene( event ) local group = self.view local dataToCapture -- Grab the variables from the parent scene if event.params ~= nil then if event.params.dataToCapture ~= nil then dataToCapture = event.params.dataToCapture end end -- Do your work here. -- Update the variable dataToCapture.dataFromOverlay = XX -- Add a field to the table. DO NOT replace the table! end

i think the way it’s intended to be used is to pass a params table in the options during showOverlay() from the underlying scene.  assuming that table (or at least one of its properties) references a table that is still in scope when you later return from hiding the overlay, then you can access the modified values, fe:

local returnvalues = {} -- at module scope   -- then elsewhere: storyboard.showOverlay("overlay", { params=returnvalues })   -- then in overlay: local returnvalues = nil -- at module scope, does nothing as is, only serves to "document" intent to use   function scene:create(event)   returnvalues = event.params -- grab it, save it   -- later local function onCancelPress(event)   returnvalues.whatDidTheyPress = "cancel"   storyboard.hideOverlay()   -- back in underlying scene function scene:overlayEnded(event)   if (returnvalues.whatDidTheyPress=="cancel") then...

that’s quick pseudo-code, just to convey the idea, don’t expect it to run as written.

all that works, but is a bit clunky.  if you’re up for it, legacy storyboard is open-sourced, and it’s trivially easy to add a “params” option to hideOverlay and attach it onto the event created for overlayEnded (exactly mimicing how showOverlay works).  that way you don’t need ANY “permanent” storage for your return values.

fwiw, hth

There are a few ways to pass back parameters from an overlay. 

  1. Send in a table as one of the parameters (options -> params) into overlay when it is created. Table is pass-by-reference in Lua.

  2. Create a global variable and use that to update. Easy to use, but global variables is something that needs extra care.

  3. If you are using COMPOSER (instead of storyboard). Use the event.parent, to call a function in the parent (use this to send back data too).

I use #1 above like this :

local dataTable = {} function scene:overlayEnded( event ) print( "Overlay removed: " .. event.sceneName ) print( "Data From Overlay : " , dataTable.dataFromOverlay ) end local options = { effect = "fade", time = 400, params = { dataToCapture = dataTable, } } storyboard.showOverlay( "overlay\_scene", options ) -- In overlay scene, grab the param and update it. function scene:createScene( event ) local group = self.view local dataToCapture -- Grab the variables from the parent scene if event.params ~= nil then if event.params.dataToCapture ~= nil then dataToCapture = event.params.dataToCapture end end -- Do your work here. -- Update the variable dataToCapture.dataFromOverlay = XX -- Add a field to the table. DO NOT replace the table! end