Tutorial: Implementing AdMob ads in game
At the end of this article, link to the game can be seen.
I just want to share my experience implementing real ads in game.
Godot Engine 3.1.1.stable.official
Code used for showing both banner and interstitial ads:
Below the code is explanation.
Make sure you have placed this script in AutoLoad section.
extends Node var admob = null var isReal = true var isTop = true var adBannerId = "" # [Replace with your Ad Unit ID and delete this message.] var adInterstitialId = "" # [Replace with your Ad Unit ID and delete this message.] var is_next_interstitial = false func _ready(): call_deferred("_init_ads") pass func _init_ads(): if(Engine.has_singleton("AdMob")): admob = Engine.get_singleton("AdMob") var res = admob.init(isReal, get_instance_id()) admob.resize() admob.loadBanner(adBannerId, isTop) admob.showBanner() admob.loadInterstitial(adInterstitialId) game.connect("ad_interstitial_should_be", self, "_on_ad_interstitial_should_be") pass func _on_interstitial_close(): is_next_interstitial = false pass func _on_interstitial_loaded(): is_next_interstitial = true pass func _on_interstitial_not_loaded(): is_next_interstitial = false pass func _on_ad_interstitial_should_be(game): if is_next_interstitial: admob.showInterstitial() pass
Make sure you have set your ad ids to variables
Install admob module like it described in the link for admob module (README.md)
1. When script is loaded up, we postponed calling function
_init_ads function, we are getting admob module and init it with value returned from
get_instance_id ( that's because you have to connect your application to module native code, so when any action have happen in the background, appropriate function within your application get called, example:
function list can be seen on link for admob module in README.md
3. Next thing you want to resize view for banner, so it can be seen on screen, by calling
4. Next thing is loading banner and showing up and also to
5. Next thing is crucial for logic of showing interstitial ad:
Note first that we had connect this script to signal from
game script named:
we have implemented it in way that after every fifth round interstitial ad have showed up in this manner: When player end game (gameover), function in script
game_over is called. If counter says that five round have passed, we do
call_deferred("emit_signal", "ad_interstitial_should_be", self)
6. So on
_on_ad_interstitial_should_be, we check if interstitial ad have loaded up by checking variable
is_next_interstitial, and if it is, we show interstitial ad.
7. Now special functions for interstitial ad:
_on_interstitial_close - when user close interstitial ad variable
is_next_interstitial is set to false
_on_interstitial_loaded - when interstitial ad have loaded up variable
is_next_interstitial is set to true
_on_interstitial_not_loaded - vice versa
And that's it.
First you make sure that test ads are loading up, by setting variable
isReal to false
Now when we have tested game with real ads, we got some errors.
we got popular error
ERROR_CODE_NO_FILL. You can see here about that error
The ad request was successful, but no ad was returned due to lack of ad inventory.
What we have tried, we don't know if it is right thing to do, but we think it doesn't anything harm (don't take me for word):
Admob module uses 'com.google.android.gms:play-services-ads:16.0.0' sdk,
and we have read that this sdk doesn't require APPLICATION_ID
But we have just in case put in manifest file this piece of code (code copied from link above):
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-****************~**********"/>
we don't know, but now when the game is published real ads works.
APPLICATION_ID gives you AdMob portal/dashboard/service, when you logged in.
When you put this code in your AndroidManifest file, you have to recompile everything.
Link for recompiling:
Also when you debugging the admob module, like it says in the link of the module, it is helpful command to do:
adb logcat -s godot
Game on the Google Play: