• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Interstitial ads with Google AdMob

I

iQd

Guest
Hi,

I am making a free version with ads of our new game, and although I have read all the documentation I could find regarding the usage of the ad system for making interstitial ads, one thing still is not clear for me.

In the beginning of the game I run the code for initializing the ads on the creation code of the first room.
Next, I load up an ad.

When the user clicks on a level selection button, the game goes to the ad room where the interstitial ad is to be shown.

Here is the thing that is not clear for me.
I have now set the room to go forward using a timer after 10 seconds.
If the user clicks the close button on the ad way before that, how can I determine that this has happened in order to move forward right after?

I would want to avoid a situation where the user clicks close on the ad and sits in the empty room for 8 seconds.
When exiting the room I load the next ad.

Thank you!
 

Still57

Member
I don't understand why you would want to wait 10 seconds to continue. Just the load the ad when the room starts. The ad will show on top of everything else anyway
 
I

iQd

Guest
Ok so a dedicated room is actually not even needed?
The way I understood the documentation is that there is supposed to be a room for the ad and you need to give time for the ad to be viewed before moving out from that room.

So based on what you said the ad is really just a layer on any currently visible content and I can just call it visible when entering the game level, and after the user closes it the game is visible normally?
This would make sense and make it much simpler.
Thank you Still57.
 

Still57

Member
You are correct the ad shows on top of everything and you don't need a dedicated room. The usual and right way to do it is to load it when you start the game or the level and then show it when the player dies or the level ends.

You should use this dummy ad id for testing purposes:

ca-app-pub-3940256099942544/1033173712

Also keep in mind that when you want to check if the ad has loaded for the first time when you are exporting for Android the following piece of code may not work:

Code:
if GoogleMobileAds_InterstitialStatus() == "Ready"
{
GoogleMobileAds_ShowInterstitial();
}
This is the first piece of code they show you in the guide for checking if an interstitial ad is loaded.

You should instead use the piece of code they tell you to use to check if the ad has loaded and is ready in a Social Asynchronous event:

Code:
var loaded = ds_map_find_value(async_load, "loaded");
 
Last edited:
I

iQd

Guest
Thank you for the information.
I have now basically just one object for doing this.

In the first room's creation code I have the initialization and a couple of global variables for determining the state of the ad mode.

Then I have an ad object which has Create, Step and Social events, having the following code.

Create:
Code:
// Load the ad.
if (global.AdMode_active == 1 && os_type == os_android)
    {
        // Load interstitial ad.
        GoogleMobileAds_LoadInterstitial();
    }
Step:
Code:
// If the ad is loaded, display the ad and start loading the next one.
if (global.AdMode_active == 1 && global.AdMode_loaded == true)
    {
        global.AdMode_loaded = false;
        GoogleMobileAds_ShowInterstitial();
    }
Social:
Code:
///Get ad info
var ident = string(ds_map_find_value(async_load, "type" ));
var loaded = ds_map_find_value(async_load, "loaded"); 

if (ident == "interstitial_load")
{
    if (loaded) { global.AdMode_loaded = true; }
}
The functionality goes like so, that the Create loads the ad, Social sets the global.AdMode_loaded flag to True and finally Step performs the showing of the ad, while resetting the loaded flag.

Would I be ok with this implementation method?
 
I

iQd

Guest
I could maybe drop the Step event and move the ad display function to Social, why do I need a separate event for that if Social is going to fire anyway when the ad is loaded.
 

Still57

Member
Yes you should be fine but you should test it. If you only use interstitial ads change your Social Asynchronous code to this:

Code:
///Get ad info
var loaded = ds_map_find_value(async_load, "loaded");
if (loaded) { global.AdMode_loaded = true; }
You don't need to check the type of the ad
 
I

iQd

Guest
Ok, I've simplified the code to two events.
Create is as above, and Social is only:

Code:
///Get ad load status and display if loaded.
var loaded = ds_map_find_value(async_load, "loaded");  
if (loaded && global.AdMode_active == 1 && os_type == os_android) {GoogleMobileAds_ShowInterstitial();}
You are right about the type of the ad, I use only interstitial in this case so why would I need to check the type.
This came from some example earlier I think.
 

Still57

Member
I could maybe drop the Step event and move the ad display function to Social, why do I need a separate event for that if Social is going to fire anyway when the ad is loaded.
Because you may want to wait until the game or the level has ended or the player has died. It is up to you. If you want the ad to show immediately after it is loaded then you can do that as well
 
  • Like
Reactions: kia

Still57

Member
Do this:

Code:
var loaded = ds_map_find_value(async_load, "loaded");
if (loaded) {GoogleMobileAds_ShowInterstitial();}
You don't need the other variables because the code in this event will only fire when something related to your ad happens. So it doesn't run all the time like the step event.

You can even do:

Code:
if (ds_map_find_value(async_load, "loaded")) {GoogleMobileAds_ShowInterstitial();}
Make sure to test it though
 
I

iQd

Guest
Ok, the other variables on that part could be extra too, I'll remove them as well.

How about the global game settings?
I need to enable Internet permission, but do I have to have 'Enable Google Services' enabled for the ads to work?
Also, Advertising => Android => Use Google Mobile Ads needs to be active too.

I do not have IAP's and any achievement updates, the game itself needs no Internet connection.

I have now only the ad ID on the init string. Do I need to enter the app ID anywhere?
 

Still57

Member
Ok, the other variables on that part could be extra too, I'll remove them as well.

How about the global game settings?
I need to enable Internet permission, but do I have to have 'Enable Google Services' enabled for the ads to work?
I do not have IAP's and any achievement updates, the game itself needs no Internet connection.
You don't need to enable Google Play Services. Enable "INTERNET" and "ACCESS_NETWORK_STATE" and check the admob checkbox in the advertising tab. The app id isn't necessary. You will see if it works only if you TEST
 
I

iQd

Guest
Thank you. Just wanted to make sure that I understood completely the operation of the ad functionality.
Otherwise I'd just lose time testing with wrong code and wrong settings.
I would have missed the 'Access_Network_State' completely, although it is logical that it is needed.

Thank you very much for your kind assistance, you cleared all what was unclear to me.
 
What are you talking about?
Access_Network_State is NOT needed for AdMob ads. In fact, I would suggest you avoid unless you really have to. Why? Because when someone is about to download your app they are greeted by an ominous message implying the game might be trying to scan your contacts and take control of your device in other ways. I had pains with this one a few months ago, getting lots of negative feedback for this one and people refusing to install the app because of it. That's why I never use it, except for in one project, where it is actually needed (php-server stuff is involved.) For all my other apps, I NEVER use Access_Network_State, and ads are working just fine.
 
I

iQd

Guest
Ok, thank you for the heads-up on that one. I'll go ahead and disable the Access_Network_State then if it is not required for the ads to work.
 
Yeah, do that. Also, skip the Social Asynchronous stuff, it's just redundant.

What I've done is this:

1. Create an object for ads. Make sure it's set to "persistent."
2. Add the object in the very first room of the game.
3. In create-event of the object, create the ads.
4. Have an alarm-even that checks itself. Have the code show an instertitial ad if there is one to show.
5. Have a room-end event that calls the alarm
6. Have a room start-event where you load interstitial ads and also checks if regular ads should be shown or not.

This is a great method that has never failed me.
 
I

iQd

Guest
Thank you obsidiannovels, I implemented the method you suggested.
 
I

iQd

Guest
When I start the game, I get the following error right after the ad object starts:
ERROR in action number 1 of Alarm event for alarm 1 for obj_ad:
Data structure with index does not exist.
at gms_Object_obj_ad_Alarm_1

I have the following code in this object.
Create:
Code:
// Load the first ad.
if (global.AdMode_active == 1 && os_type == os_android)
    {
        // Load interstitial ad.
        GoogleMobileAds_LoadInterstitial();
    }
Alarm0:
Code:
/// Load a new ad.
if (global.AdMode_active == 1 && os_type == os_android)
    {
        // Load interstitial ad.
        GoogleMobileAds_LoadInterstitial();
    }
Alarm1:
Code:
///Get ad load status and display if loaded.
var loaded = ds_map_find_value(async_load, "loaded");

if (loaded && global.AdMode_showok = 1)
    {
        GoogleMobileAds_ShowInterstitial();
    }
At the stage where the code is running, only the Create event should run, I am not calling any Alarms yet.
I set the global variables 'global.AdMode' and 'global.AdMode_showok' in the same room before.

The 'showok' variable is set whenever the user enters any room.
This prevents from displaying the ad right after game start, the ad gets displayed only after exiting a game room and returning to level selection.

Am I messing up something here?
 
Last edited by a moderator:
I

iQd

Guest
I am trying to determine is the ad loaded and can it be shown.
I understood from all blogs and documentation that I can do it with the ds_map_find_value, and I have seen no alternative method for doing this.
 

Still57

Member
obsidiannovels is right about the "Access_Network_State". You don't need it. Other than that stick to what I said, he's just getting you confused. The method I told you to use works perfectly. You don't need to use alarms to do this.

What I understood was that you want to show an interstitial ad when a room starts right?

Do this:

Create 2 rooms: "rStart" and "rGame"
Put this piece of code in the creation code of "rStart":

Code:
GoogleMobileAds_Init("ca-app-pub-3940256099942544/1033173712");
room_goto(rGame);
Now create an object put this code in the create event:

Code:
GoogleMobileAds_LoadInterstitial();
and put this code in the Social Asynchronous event:

Code:
var loaded = ds_map_find_value(async_load, "loaded");
if (loaded) {GoogleMobileAds_ShowInterstitial();}
put this object in the "rGame" room.

Go to your Global Game Settings check "INTERNET" in the permissions tab. Check "Google Mobile Ads" in advertising and make sure you imported the AdMob extension.

That's it. It's that simple. Now run it on your Android or iOS phone and you'll see it works. You'll modify the code the way you want afterwards, according to how or when you want to show the ad.

Plus if you want proof that this works: Here

The ad will load every 3 games after you die
 
Last edited:
I

iQd

Guest
Hi Still57,

Thank you, I think I know now what the problem has been.

I have been trying to load up the ad so that there is time between the load and display, allowing time for the load to happen during the level play.
I placed the load command to the beginning of the game room, and display command at the end of the game room.
The problem here is that if I set the showinterstitial command anywhere else than the Social Asynchronous event, it just does not function at all (no error, no ad).

I've had it previously set so that the ad would be shown at the end of the level when the user exits to level selection room.
However after a lot of testing, it just fails to work when I move it elsewhere.
Moving the code from Social to any other type of event will end up in error regarding the ds_map not having content.

I got it working now by keeping the display command in the Social event, however with these two happening right after each other, the loading of the ad gives too much time for the user to start doing something, then after download it gives the appearance of displaying at a random or otherwise inconvenient situation, for example right after the user has already started to play.

Due to this, I cannot control when after loading the ad will be actually shown.
I must be doing something wrong, it cannot be like this
I have however tried many combinations for the showinterstitial command, but without luck as it just really does nothing after that, even when the load command was already issued.
 

Still57

Member
Try this: Change the Social Asynchronous event to

Code:
if(ds_map_find_value(async_load, "loaded")){loaded = true;}
In the Create event do

Code:
loaded = false;
GoogleMobileAds_LoadInterstitial();
and in the Step event put

Code:
if (loaded) {
GoogleMobileAds_ShowInterstitial();
loaded = false;
}
and use the code in the Step event when the game ends
 
R

RATInteractive

Guest
Trying to ad the adverts today but the log is saying the SKD is outdated and connection refused? anyone with the same issue
 
Last edited by a moderator:
G

Guest User

Guest
How do I fix this?

Event: Create

///Initialise

if (os_type == os_ios)
{
GoogleMobileAds_Init("My ad ID here");
}
else
{

GoogleMobileAds_Init("My ad ID here");
}

global.useTestAds = false; // Note that before using the button for this you must have added your device ID into objToggleTest
global.bottomRight = false;
global.interLoading = false;

//The "My ad ID here, part is where I put my ID, yes I do not just place ad Id here there!
 
Top