Admob newbie question (SOLVED)

Here is my error with admob : /main/main.script:3: attempt to index global 'admob' (a nil value)
Here is the line of code that is not working : admob.init('ca-app-pub-000', true)
I have also tried like this
admob.init{ app_id = '000', test = true, listener = listener }

Obviously 000 is my id number.

Also, I have the admob folder in the root of my game.

The hint is in the fact that the module is nil. It means that the module doesn’t exist!
I’m guessing you’re using this extension?

On the asset page, it says it is only supported on iOS and Android. What platform are you trying it on?

1 Like

I am trying it on android. In the emulator it says admob not supported on this platform. When I run it on my phone, it says that error. The extension says to manually put the admob file. I followed all the instructions. I also get this error with the example.

How did you deploy the executable to the android?

I used the over wifi deploy to get the error. I also tried to export to apk then launch on phone and it launched correctly but there was no ad and no way to see if there was an error.

When using native extensions, you need to bundle for android, into an .apk.
Put that apk onto your device (e.g using adb)
You can view the logs using either adb logcat or Android Studio.

1 Like

Wow I did not know that. I will try now. Thanks for taking the time to help me. I love defold. I started 2 weeks ago and feel like a pro with such an intuitive software.

4 Likes

If you are using my extension, the code should be a bit different, please see the docs.
https://docs.spiralcodestudio.com/extension/admob/init/#example

2 Likes

Hi! Sorry for the delay. I tried the example on github of your extension and it gave me that error on the remote launch of defold. I will try to export the example as apk and send it to my phone.

Remote launch is not compatible with native extensions. Build an APK and install it.

Ok I will send one to my phone.

Remote launch is available with native extensions.
But you need to install your custom executable first.

2 Likes

Hello again! I tested the test app on my phone and the test ad worked. Now I am trying to implement it into my game but I am having some trouble. In my init function I have this code :

admob.init{
	app_id = 'ca-app-pub-685162264159115~9352519994',
	test = true, -- ALWAYS use test ads, only disable when submitting to the stores.
	listener = listener
}

Here is the code that makes the banner show up just in the script (not in init, update or anything)

local function listener(event)
	if event.phase == 'init' then
		admob.load{
			type = 'banner',
			id = 'ca-app-pub-561651641115/5277419906',
			position = 'bottom',
			size = 'smart',
			position = 'bottom'
		}
	end
end

When I launch the app, it does not show up. What else do I need to put in?

If you separated those two pieces of code apart, it won’t work since the listener variable is not visible to the admob.init() function.
You can move this part

admob.load{
			type = 'banner',
			id = 'ca-app-pub-561651641115/5277419906',
			position = 'bottom',
			size = 'smart',
			position = 'bottom'
		}

to another script, but you have to make sure, that you have called admob.init() before invoking admob.load() and that init process was successful (init phase of the event).

I moved the function code above the init code in the init function. There is still no ad. I looked at the example and there was no init. All I need is a simple banner ad.

Share all of your code. Everyone is guessing based on the little you’ve shared.

local function listener(event)
	if event.phase == 'init' then -- Admob has been initialized, now it's safe to load a banner.
		admob.load{
			type = 'banner',
			id = 'ca-app-pub-5899342264159115/5277419906',
			position = 'bottom',
			size = 'smart',
			position = 'bottom'
		}
	end
end
admob.init{
	app_id = 'ca-app-pub-5899342264159115~9352519994',
	test = true, -- ALWAYS use test ads, only disable when submitting to the stores.
	listener = listener
}
function init(self)
	collectionfactory.create("#collectionfactory")

	--admob.init('ca-app-pub-5899342264159115~9352519994', true)
end

function final(self)
	sound.stop()
end

Here is my code that is in main.scipt. Here is my collection structure.
image
Here is my file structure.
image
Here is my game.project :


You should call admob.init() from within the init() lifecycle function.

What if you add a print() to the top of your listener function? Does it get called?

1 Like

I tried but I can’t get a print when I bundle for android. I will try to make something happen when the listener gets called.