![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | ninogamedev |
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:
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)
-
When script is loaded up, we postponed calling function
_init_ads
throughcall_deferred
, because
What are the semantics of call_deferred? - Archive - Godot Forum -
In
_init_ads
function, we are getting admob module and init it with value returned fromget_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 -
Next thing you want to resize view for banner, so it can be seen on screen, by calling
admob.resize()
-
Next thing is loading banner and showing up and also to
loadInterstitial
-
Next thing is crucial for logic of showing interstitial ad:
Note first that we had connect this script to signal fromgame
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 scriptgame
calledgame_over
is called. If counter says that five round have passed, we docall_deferred("emit_signal", "ad_interstitial_should_be", self)
-
So on
_on_ad_interstitial_should_be
, we check if interstitial ad have loaded up by checking variableis_next_interstitial
, and if it is, we show interstitial ad. -
Now special functions for interstitial ad:
_on_interstitial_close
- when user close interstitial ad variableis_next_interstitial
is set to false
_on_interstitial_loaded
- when interstitial ad have loaded up variableis_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
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: 开始使用 | Android | Google for Developers
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:
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:
Hi, my name is Carlos. I’m new with this programming language. I have some doubts about the implementation of admod. I could communicate with you.
I wait your answer
carlosgame | 2020-03-27 13:05