one is in my scene menu.lua, its an incentivized publicity that you need to watch til the end to get some coins
the other is in game.lua its an interstitial, it display the publicity after every X minutes.
I put my ads.init() and adEventListerner in main.lua so it is accessible in all scenes.
The issue is, from adEventListerner in main.lua, how can I access variables or objects in other scenes?
I want for example that when you are in the menu.lua, I do my ads.show() and after I want to show a prompt to display the coins you receive after viewing the publicity. While in the game.lua after I do the ads.show and watch the publicity, I don’t want to do nothing after.
So here I am in main.lua trying to acces objects and variables from menu.lua… is there any way I can do this?
Is there a way I can have a unique callback function by scene? one in menu.lua and one in game.lua?
Or maybe there is a better way to handle ads in multiple scene? what do you think?
You can create a wrapper around the ads library. Make the wrapper global or store it in composer with SetVariable.
Then make a method in your wrapper that you call every time you want to show an ad and that also allows you to pass a callback. This way you can control what happens after the ad anytime.
Another solution, make the animation to show the coins respond to an event attached to a global object or even Runtime. Whenever you want to show the coins gained just fire the appropriate event (remember you can attach arbitrary data to your custom events).
I created a “controller” function in each module and from my main.lua I call one of those controller. for example. In my game.lua I have a function like this:
-- game.lua function gameController(action) if action == 'resume' then -- resume game end end
and I have the same in menu.lua
--- menu.lua function menuController(action) if action == 'reward' then end end
those 2 functions need to be global so they can be accessed from my main.lua.
So after all I do is something like this:
--main.lua function adInitListener(event) if event.type == "adEnd" then if composer.getSceneName("current") == "game" then gameController("resume") elseif composer.getSceneName("current") == "menu" then menuController("reward") end end end ads.init( "vungle", "1234567890", adInitListener)
two methods suggest themselves: either define a method on both of your scenes (fe scene:adCallback()) then have your ad code ask composer for the current scene, and if that method is present then call it. or have both scenes register/unregister event listeners for custom events, then have your ad code emit those events. either way you either only have one current scene, or with proper management only one listener, so your ad code doesn’t actually need to know “who” is the current scene, just that it found one willing to respond appropriately. (there are plenty of other ways you might set up an anonymous “message bus”, but those two seem to “fit” easiest with what Corona already gives you)
[EDIT: nevermind, somehow missed the last post, you’re already doing a modified version of the first method, just using some if statements to navigate it, which is just as good]
i don’t miss it - very few scripting languages have all that, and you can set up a “base” object that supports inheritence with a single line of code (well, maybe two, if you want it to be readable :D). i use the callback method for handling back keys, delegating from a single runtime system event listener, then each scene can do as appropriate (ignoring, going back a scene, hiding if an overlay, unpausing if a pauser, etc). works well, and though could have done it with listeners, it saves a lot of add/remove listener code this way. :)
Instead of having “coins” be a local variable in your game scene, add it to a table that’s required into all modules. You can even attach functions to this table that are globally runable too.
You can create a wrapper around the ads library. Make the wrapper global or store it in composer with SetVariable.
Then make a method in your wrapper that you call every time you want to show an ad and that also allows you to pass a callback. This way you can control what happens after the ad anytime.
Another solution, make the animation to show the coins respond to an event attached to a global object or even Runtime. Whenever you want to show the coins gained just fire the appropriate event (remember you can attach arbitrary data to your custom events).
I created a “controller” function in each module and from my main.lua I call one of those controller. for example. In my game.lua I have a function like this:
-- game.lua function gameController(action) if action == 'resume' then -- resume game end end
and I have the same in menu.lua
--- menu.lua function menuController(action) if action == 'reward' then end end
those 2 functions need to be global so they can be accessed from my main.lua.
So after all I do is something like this:
--main.lua function adInitListener(event) if event.type == "adEnd" then if composer.getSceneName("current") == "game" then gameController("resume") elseif composer.getSceneName("current") == "menu" then menuController("reward") end end end ads.init( "vungle", "1234567890", adInitListener)
two methods suggest themselves: either define a method on both of your scenes (fe scene:adCallback()) then have your ad code ask composer for the current scene, and if that method is present then call it. or have both scenes register/unregister event listeners for custom events, then have your ad code emit those events. either way you either only have one current scene, or with proper management only one listener, so your ad code doesn’t actually need to know “who” is the current scene, just that it found one willing to respond appropriately. (there are plenty of other ways you might set up an anonymous “message bus”, but those two seem to “fit” easiest with what Corona already gives you)
[EDIT: nevermind, somehow missed the last post, you’re already doing a modified version of the first method, just using some if statements to navigate it, which is just as good]
i don’t miss it - very few scripting languages have all that, and you can set up a “base” object that supports inheritence with a single line of code (well, maybe two, if you want it to be readable :D). i use the callback method for handling back keys, delegating from a single runtime system event listener, then each scene can do as appropriate (ignoring, going back a scene, hiding if an overlay, unpausing if a pauser, etc). works well, and though could have done it with listeners, it saves a lot of add/remove listener code this way. :)
Instead of having “coins” be a local variable in your game scene, add it to a table that’s required into all modules. You can even attach functions to this table that are globally runable too.