- Edited
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.
Tested on: Godot Engine 3.1.1.stable.official
Used library: https://github.com/kloder-games/godot-admob
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
Explanation:
Make sure you have set your ad ids to variables adBannerId
and adInterstitialId
.
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
through call_deferred
, because
https://godotengine.org/qa/7336/what-are-the-semantics-of-call_deferred
2. In _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:
_on_interstitial_close
, _on_interstitial_not_loaded
....
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 admob.resize()
4. Next thing is loading banner and showing up and also to loadInterstitial
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: ad_interstitial_should_be
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
called 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
https://github.com/kloder-games/godot-admob#troubleshooting
It says
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 Link: https://developers.google.com/admob/android/quick-start
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.
Your 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: http://docs.godotengine.org/en/latest/development/compiling/compiling_for_android.html#compiling-export-templates
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: https://play.google.com/store/apps/details?id=com.desi.game