Need help to show a banner ad

so i need to put this part of code in a on init function?

You can read about the life cycle functions here:

okay so i did this and i put the part of code in a final function
now it looks like this :

local function admob_callback(self, message_id, message)
	if message_id == admob.MSG_INITIALIZATION then
		self.initialized = true
		if message.event == admob.EVENT_COMPLETE then
			print("EVENT_COMPLETE: Initialization complete")
		end
	elseif message_id == admob.MSG_BANNER then
		if message.event == admob.EVENT_LOADED then
			print("EVENT_LOADED: Banner AD loaded. Height: "..message.height.."px Width: "..message.width.."px")
			set_block_height(message.height)
		elseif message.event == admob.EVENT_OPENING then
			print("EVENT_OPENING: Banner AD is opening")
		elseif message.event == admob.EVENT_FAILED_TO_LOAD then
			print("EVENT_FAILED_TO_LOAD: Banner AD failed to load\nCode: "..message.code.."\nError: "..message.error)
		elseif message.event == admob.EVENT_CLICKED then
			print("EVENT_CLICKED: Banner AD clicked")
		elseif message.event == admob.EVENT_CLOSED then
			print("EVENT_CLOSED: Banner AD closed")
		elseif message.event == admob.EVENT_DESTROYED then
			print("EVENT_DESTROYED: Banner AD destroyed")
		elseif message.event == admob.EVENT_IMPRESSION_RECORDED then
			print("EVENT_IMPRESSION_RECORDED: Banner did record impression")
		elseif message.event == admob.EVENT_JSON_ERROR then
			print("EVENT_JSON_ERROR: Internal NE json error: "..message.error)
		end
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")

	local engine_info = sys.get_engine_info()
	self.is_debug = engine_info.is_debug

	if self.is_debug then
		-- Always test ONLY with test ads units when you test integration!
		if sys.get_sys_info().system_name == 'iPhone OS' then
			-- https://developers.google.com/admob/ios/test-ads
			self.banner_ad_unit = "ca-app-pub-3940256099942544/2934735716" -- test unit for banners
		else --Android
			-- From https://developers.google.com/admob/android/test-ads
			self.banner_ad_unit = "ca-app-pub-3940256099942544/6300978111" -- test unit for banners
		end
	else
		-- Always test ONLY with test ads units when you test integration!
		--  !!! Replace this keys with your own keys : 
		-- https://developers.google.com/admob/android/quick-start#set_up_your_app_in_your_admob_account
		if sys.get_sys_info().system_name == 'iPhone OS' then
			self.banner_ad_unit = "ca-app-pub-3940256099942544/2934735716" -- !!! replace it with iOS banner unit
		else --Android
			self.banner_ad_unit = "ca-app-pub-3940256099942544/6300978111" --  !!! replace it with Android banner unit
		end
	end

	self.show_pos = 1
	self.banner_positions = {
		"POS_BOTTOM_CENTER",
		"POS_BOTTOM_LEFT",
		"POS_BOTTOM_RIGHT",
		"POS_NONE",
		"POS_TOP_LEFT",
		"POS_TOP_CENTER",
		"POS_TOP_RIGHT",
		"POS_CENTER"
	}

	-- `admob` works only on iOS and Android, make sure the extension is available
	if admob then
		-- !!! Set callback before initialization
		admob.set_callback(admob_callback)
		-- !!! Read documentation about privacy settings and use the following method if you need to apply it
		-- https://developers.google.com/admob/ios/ccpa
		-- https://developers.google.com/admob/android/ccpa
		admob.set_privacy_settings(true)
	end
end

function on_input(self, action_id, action)
	if not admob then
		return
	end

	local prev_type = self.ad_type
	admob.initialize()
end

function final(self)
self.ad_type = dirtylarry:radio("ad_type_banner", action_id, action, admob.MSG_BANNER, self.ad_type)
admob.load_banner(self.banner_ad_unit)
print("admob.show_banner( admob."..self.banner_positions[self.show_pos]..")")
admob.show_banner(admob[self.banner_positions[self.show_pos]])
self.show_pos = self.show_pos + 1
if self.show_pos > #self.banner_positions then
	self.show_pos = 1
end
end

but it doesn’t work, also Mathias the code i used is from the example project and i think i remove the parts that i don’t need so i don’t understand why it dosen’t work

You are calling admob.initialize() from the on_input() function. I’m pretty sure the example didn’t do this. You want to initialize once. This is what the init() lifecycle function is for.

You are calling load_banner() and show_banner() from final(). Again, this is not what the example project did. The final() function is called when the script is deleted.

Please carefully read the documentation we link to. And please be mindful of changes you make to an example and try to consider the implications of the changes you make.

4 Likes