passing function as parameter in lua issue

Hi everyone,

I have an issue on how can I pass functions as a parameter in Lua, below is my requirements:

inside main.lua:


function refreshCoins() end

showVungleAd(refreshCoins) – this call a function inside ads.lua

Inside ads.lua


function showVungleAd(action)

  vungleAds.show( “incentivized” )  – This is a call from third party plugin to deal with vungle ads

end

local function vungleAdListener( event ) end

now my question is here, how can I call my parametrized function inside the vungleAdListener?

Following the trail… you have refreshCoins of type function, which is passed into showVungleAd as action.

This means action == refreshCoins == type is “function”.

So you can call it as action(), inserting params between the parenthesis if you need to.

Keep in mind that Lua only ever keeps one instance of a function in memory. When refreshCoins is passed into showVungleAd and picked up as the variable action, there is no copy of the function - it’s the exact same function as the original being called. (hope that made sense)

 

function showVungleAd(action)   vungleAds.show( "incentivized" )  -- This is a call from third party plugin to deal with vungle ads  action(params) end

Hi,

If you put your listener inside the showVungleAd function, you should be able to do it like this:

--ads.lua function showVungleAd(action) local function vungleAdListener(event) action() end vungleAds.show("incentivized") end

I’ve never used Vungle ads, so I’m not sure how/where the listener is called.

-dev

Normally, you would link your adListener in the init() call to the Vungle SDK.

If you want multiple paths from the adListener - to award various outcomes then add that code into the adListener event.

local ads = require( "ads" ) local function adListener( event ) if event.type == "adEnd" then --call your function here refreshCoins() end end ads.init( "vungle", "myAppId", adListener ) ... sometime later ... ads.show( "incentivized" )

Following the trail… you have refreshCoins of type function, which is passed into showVungleAd as action.

This means action == refreshCoins == type is “function”.

So you can call it as action(), inserting params between the parenthesis if you need to.

Keep in mind that Lua only ever keeps one instance of a function in memory. When refreshCoins is passed into showVungleAd and picked up as the variable action, there is no copy of the function - it’s the exact same function as the original being called. (hope that made sense)

 

function showVungleAd(action)   vungleAds.show( "incentivized" )  -- This is a call from third party plugin to deal with vungle ads  action(params) end

Hi,

If you put your listener inside the showVungleAd function, you should be able to do it like this:

--ads.lua function showVungleAd(action) local function vungleAdListener(event) action() end vungleAds.show("incentivized") end

I’ve never used Vungle ads, so I’m not sure how/where the listener is called.

-dev

Normally, you would link your adListener in the init() call to the Vungle SDK.

If you want multiple paths from the adListener - to award various outcomes then add that code into the adListener event.

local ads = require( "ads" ) local function adListener( event ) if event.type == "adEnd" then --call your function here refreshCoins() end end ads.init( "vungle", "myAppId", adListener ) ... sometime later ... ads.show( "incentivized" )