How to get adListener() to run a function from another .lua file?

Ok, first off, I know this is relating to Appodeal, but I feel as though it is more of a external modules and function calling question than an appodeal specific question, thus I placed it here.  If you feel it should be moved please do so.  

So, after alot of research and some trial and error, I have found that it is stated to be best that the appodeal be initially required in the main, as well as the init().  Because the appodeal.init(adListener, {appKey = “…”}) mentions the adListener function and because of scoping I need to add the adListener function within the main.lua as well.  

My current main.lua is as follows: 

--------------------------------------------------------------------------------- -- -- main.lua -- --------------------------------------------------------------------------------- -- require the composer library local composer = require "composer" -- require the appodeal library, ad the listender, and init local appodeal = require( "plugin.appodeal" ) local function adListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) elseif ( event.phase == "loaded" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "displayed" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "clicked" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "closed" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "hidden" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "failed" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "playbackBegan" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "playbackEnded" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "dataReceived" ) then print(tostring(event.phase)..": "..tostring(event.type)) elseif ( event.phase == "playbackEnded") and (event.type == "rewardedVideo") then print("Video Reward Playback Ended") rewardThem() end end appodeal.init( adListener, { appKey= "111111111111111111111111111111" } ) appodeal.load( "banner" ) appodeal.load( "rewardedVideo" ) -- hide the status bar display.setStatusBar( display.HiddenStatusBar ) -- Reserve channel 1 for background music audio.reserveChannels( 1 ) -- Seed the random number generator math.randomseed( os.time() ) -- load logo scene before going to menu composer.gotoScene( "logo", {effect = "fade", time = 3000} )

Now I am trying to figure out how to get the adListener to… “listen” once I continue on to other scenes, such as my gameOnePlayer.lua.  Also, once it listens, and “hears” that a reward video ad is complete I want it to run the rewardThem() function which will actually be in my gameOnePlayer.lua. 

I read through the globals tutorial as well as the external modules tutorial and still need a little guidance.  So, multi questions coming up. 

First, if I create the adListener(event) function in the main.lua, does it continue through the life of the app or do I need to somehow reference it in each scene? 

Second, how do I get the rewardThem()  function which is actually located in the gameOnePlayer.lua to work?  My first thought was that I needed to require("gameOnePlayer) as well in the main, and create a local variable for gameOnePlayer.rewardThem() function.  Problem is, I am confusing my self when attempting to do this.   Should I use the below code within the gameOnePlayer.lua or just call it a local function like normal? 

function scene:rewardThem() --Give reward code here end

Third, and hopefully lastly, if I load something in the main.lua like a sound or in this case 

appodeal.load("rewardedVideo")

does it stay loaded even when progressing through other scenes and can I call it with the same function as normal or do I have to reference the main.lua some how?

I didn’t really follow your entire thread, but from what I gather you want to be able to do something in lua file B when something is fired in lua file A.  This is what I do for both cuadros (at the end of a game when you still have swaps) and bletchley (to give you 2 more tries to figure out the code). The code below is from cuadros and I am using Appnext for rewarded video ads.

  1. Create a dispatcher in lua file A:

    M.eventDispatcher = system.newEventDispatcher()

  2. In the reward callback fire the dispatcher from file A:

    if event.event == “videoEnded” and event.adKey == appNextRewardedAppKey then M.eventDispatcher:dispatchEvent( { name=“rewarded”, completed=true } ) end

  3. Then in file B listen to the event:

    utils.eventDispatcher:addEventListener(“rewarded”, swapSquares )

  4. And then do something with call:

    swapSquares = function(event) if event.completed == true then --do something here else --do something else here end end

First, yes, your adListener lives for the run of your app.

What I would do is follow what you started, in your game scene:

function scene:rewardThem() ... end

In your adListener you can do:

local gameScene = composer.getScene("gameOnePlayer") gameScene:rewardThem()

Rob

As always Rob thanks for the help. BTW I’m loving this… Making app/games. I released my first app/game like a week and half ago with little expectations and have made 60$ with only like 14 downloads so far. My next game i am working on is higher expectations and I am really working to get the graphics, sound, and smoothness right. I’ll post it later once done, I have the jist created just have to finish making all the levels and add in the in-app purchases.

I didn’t really follow your entire thread, but from what I gather you want to be able to do something in lua file B when something is fired in lua file A.  This is what I do for both cuadros (at the end of a game when you still have swaps) and bletchley (to give you 2 more tries to figure out the code). The code below is from cuadros and I am using Appnext for rewarded video ads.

  1. Create a dispatcher in lua file A:

    M.eventDispatcher = system.newEventDispatcher()

  2. In the reward callback fire the dispatcher from file A:

    if event.event == “videoEnded” and event.adKey == appNextRewardedAppKey then M.eventDispatcher:dispatchEvent( { name=“rewarded”, completed=true } ) end

  3. Then in file B listen to the event:

    utils.eventDispatcher:addEventListener(“rewarded”, swapSquares )

  4. And then do something with call:

    swapSquares = function(event) if event.completed == true then --do something here else --do something else here end end

First, yes, your adListener lives for the run of your app.

What I would do is follow what you started, in your game scene:

function scene:rewardThem() ... end

In your adListener you can do:

local gameScene = composer.getScene("gameOnePlayer") gameScene:rewardThem()

Rob

As always Rob thanks for the help. BTW I’m loving this… Making app/games. I released my first app/game like a week and half ago with little expectations and have made 60$ with only like 14 downloads so far. My next game i am working on is higher expectations and I am really working to get the graphics, sound, and smoothness right. I’ll post it later once done, I have the jist created just have to finish making all the levels and add in the in-app purchases.