Make a button do something on another scene?

Is there a way I can make a button on an overlay of a scene remove an object from the scene?

local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) composer.hideOverlay("ifc.drop", options ) if event.phase == "ended" and eggplant.alpha ~= 0 then eggplant.alpha = 0 physics.removeBody(eggplant) end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local removeeggplant = widget.newButton( { width = 80, height = 40, defaultFile = "images/level.png", overFile = "images/level.png", label = "", onEvent = handleButtonEvent } ) -- Center the button removeeggplant.x = 200 removeeggplant.y = 20 -- Change the button's label text removeeggplant:setLabel( "Remove" ) sceneGroup:insert(removeeggplant) 

So I have this code above that has a button on an overlay called “drop” that when pushed hides the overlay and removes the eggplant, but because the eggplant is not in the overlay but in the scene it doesn’t work, is there a way I can make the button remove the eggplant from the scene from the overlay?

give the parent scene a “helper” function to do what you want (so that eggplant is in scope), then in the overlay’s show event you can capture its parent and call the parent’s helper when needed by the button

Hi Dave, could you elaborate further, this is very new to me

attach the button to global values and call these values in the other scene <<the simplest way :slight_smile:

roughly, here’s structure only, details left for you to fill in…:

-- in parent scene define helper function function scene:helperFunction() -- remove the eggplant end -- in the overlay scene create a place to store reference to parent local parentScene = nil -- in overlay scene's show event grab parent from event for later use by button function scene:show(event) parentScene = event.parent -- in button's handler call parent's helper function local function handleButtonEvent(event) parentScene:helperFunction()

give the parent scene a “helper” function to do what you want (so that eggplant is in scope), then in the overlay’s show event you can capture its parent and call the parent’s helper when needed by the button

Hi Dave, could you elaborate further, this is very new to me

attach the button to global values and call these values in the other scene <<the simplest way :slight_smile:

roughly, here’s structure only, details left for you to fill in…:

-- in parent scene define helper function function scene:helperFunction() -- remove the eggplant end -- in the overlay scene create a place to store reference to parent local parentScene = nil -- in overlay scene's show event grab parent from event for later use by button function scene:show(event) parentScene = event.parent -- in button's handler call parent's helper function local function handleButtonEvent(event) parentScene:helperFunction()