Getting Ads in an external module to share between scenes

Hi :slight_smile: So I have successfully implemented the ads part for my app. The ads show perfectly fine. The problem is that I have 2 scenes and for the time being, I’ve coded each scene to require the ads API and it loads them from scratch each time a scene is loaded since I always remove the previous scene everytime I load a new scene. Now I want to implement the ads part from just 1 external module. Based on the ads and external module tutorials I’ve read, it should be possible to just initialize the ads once and just call it to show whenever I want and not have to reinitialize again and again like my current code is doing now. This is my code so far and it works fine.

local ads = require “ads”

local function adMobListener( event )
   if ( event.isError ) then
      storyboard.showOverlay( “selfpromo” )
   end
   return true
end

ads.init( “admob”, “XXXXXXXXXXXXXXXXXXXXXXXXX”, adMobListener )

local params = {
            x=0,
            y=0,
            isAnimated = false,
            isAutoRotation = true,
        }

ads:setCurrentProvider(“admob”)
ads.show( “banner”, params )

My idea is that I’d want each scene to just say…

local adsmodule = require “adsmodule”

adsmodule.show

Then it automatically shows the ads because the initialization has already been done.

I have made an external module called ‘adsmodule.lua’ and these are the codes under.

local M = {}

local M.ads = require “ads”
 
– functions are now local:
local function adMobListener( event )
   if ( event.isError ) then
      storyboard.showOverlay( “selfpromo” )
   end
   return true
end

local function initialize()
    M.ads.init( “admob”, “XXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, adMobListener )
end

local function show()
    local params = {
            x=0,
            y=0,
            isAnimated = false,
            isAutoRotation = true,
    }
    ads:setCurrentProvider(“admob”)
    ads.show( “banner”, params )
end

M.adMobListener = adMobListener
M.initialize = initialize
M.show = show

return M

I apologize if I did any ridiculous coding because I just got into external modules, based on the tutorials I’ve read, my mind told me to code like this and it doesn’t work. I’d appreciate it if anybody could guide me on this. Thank you very much.

I don’t see why not. Try it  :slight_smile:

Here are the templates of how I write my modules. Hope it helps you with your modular development.

[lua]


– SAMPLE OO NON-STATIC MODULE IN CORONA

@Daniel 05.07.2014


local dog = {}

local dog_mt = { __index = dog } – metatable


– CONSTRUCTOR


function dog.new(name, years)

local newDog = {

name = name or “dog”,

years = years

}

return setmetatable( newDog, dog_mt )

end


– PRIVATE FUNCTIONS


local function getDogYears( realYears )

return realYears * 7

end


– PUBLIC FUNCTIONS


function dog:getInfo()

print( "Name: " … self.name … ", Dog years: " …  getDogYears(self.years) )

end

return dog

[/lua]

[lua]


– SAMPLE OO STATIC MODULE IN CORONA

@Daniel 05.07.2014


local static = {}


– PROPERTIES


static.demo = “Demo”


– PRIVATE FUNCTIONS


local function samplePrivateFunction()

return “samplePrivateFunction”

end


– PUBLIC FUNCTIONS


function static:samplePublicFunction()

return “samplePublicFunction”

end

return static

[/lua]

Regards,

Daniel

Hello Sir Daniel, thank you very much for this help, I appreciate it very much. I’ll be trying out your suggestions now. I wasn’t able to get back to my Corona app yesterday because I was working on a C# program. .NET is my main computer language but I’m amazed by how Corona managed to get it very simplified. I was able to make a fully working app in 3 days with Corona! But of course some optimization codes still have to be implemented. I’ll get back to you as soon as I figure this out or not. THANK YOU SO MUCH.

One question though…

What’s the difference between…

  • function dog.new(name, years)
  • local newDog = {
  • name = name or “dog”,
  • years = years
  • }
  • return setmetatable( newDog, dog_mt )
  • end

and this…

 

  • function dog:getInfo()
  • print( "Name: " … self.name … ", Dog years: " …  getDogYears(self.years) )
  • end

 

Why does the first one have “.” and the second “:”. Thank you very much!

With : you define a function on a object. You can then use the self keyword to access the object inside such a function.

http://www.lua.org/pil/16.html

Daniel

Sorry for the late reply. Thank you Sir. Your suggestion works like a charm :slight_smile:

I don’t see why not. Try it  :slight_smile:

Here are the templates of how I write my modules. Hope it helps you with your modular development.

[lua]


– SAMPLE OO NON-STATIC MODULE IN CORONA

@Daniel 05.07.2014


local dog = {}

local dog_mt = { __index = dog } – metatable


– CONSTRUCTOR


function dog.new(name, years)

local newDog = {

name = name or “dog”,

years = years

}

return setmetatable( newDog, dog_mt )

end


– PRIVATE FUNCTIONS


local function getDogYears( realYears )

return realYears * 7

end


– PUBLIC FUNCTIONS


function dog:getInfo()

print( "Name: " … self.name … ", Dog years: " …  getDogYears(self.years) )

end

return dog

[/lua]

[lua]


– SAMPLE OO STATIC MODULE IN CORONA

@Daniel 05.07.2014


local static = {}


– PROPERTIES


static.demo = “Demo”


– PRIVATE FUNCTIONS


local function samplePrivateFunction()

return “samplePrivateFunction”

end


– PUBLIC FUNCTIONS


function static:samplePublicFunction()

return “samplePublicFunction”

end

return static

[/lua]

Regards,

Daniel

Hello Sir Daniel, thank you very much for this help, I appreciate it very much. I’ll be trying out your suggestions now. I wasn’t able to get back to my Corona app yesterday because I was working on a C# program. .NET is my main computer language but I’m amazed by how Corona managed to get it very simplified. I was able to make a fully working app in 3 days with Corona! But of course some optimization codes still have to be implemented. I’ll get back to you as soon as I figure this out or not. THANK YOU SO MUCH.

One question though…

What’s the difference between…

  • function dog.new(name, years)
  • local newDog = {
  • name = name or “dog”,
  • years = years
  • }
  • return setmetatable( newDog, dog_mt )
  • end

and this…

 

  • function dog:getInfo()
  • print( "Name: " … self.name … ", Dog years: " …  getDogYears(self.years) )
  • end

 

Why does the first one have “.” and the second “:”. Thank you very much!

With : you define a function on a object. You can then use the self keyword to access the object inside such a function.

http://www.lua.org/pil/16.html

Daniel

Sorry for the late reply. Thank you Sir. Your suggestion works like a charm :slight_smile: