Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Implementing Ads Into Your Game

edited June 2018 in Code Sharing Posts: 237

I remembered today that I promised I would share the code required to add ads into your game. The reason why it took me so long to get to it was because I stopped coding for school, I just didn't have time. But I know you don't want to hear my excuses. The ad company I've worked with is Chartboost, any other company you will have to write the code yourself... sorry :( and this is only for the people that have gotten as far as to export their game into xCode and have been working on it from there.
1. Make an account with Chartboost chartboost.com
2. After you have made an account, it will walk you through what how to add campaigns if I recall correctly, but if it doesn't, make an app in your dashboard and fill out its information
3. The add a publishing campaign, if it doesn't allow you to, I highly recommend that you fill out the tax and billing information because that will restrict you from creating more campaigns.
4. For the publishing campaign, fill out the correct info, Static interstitial is a quick pop-up, video interstitial is a, well, video, and a playable interstitial is an interactive playable ad
5. Once you have created the campaign, you will need to go to xCode > "Your app name" > Addons, and add the two files given below
6. Go to your AppDelegate.mm and go to the top above where it says @implementation AppDelegate and type in the following code:

#import "CBAds.h"

#import <ChartBoost/ChartBoost.h>

@interface AppDelegate ()<ChartboostDelegate>
@end
  1. Now go inside the first function - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions under where self.viewController is instantiated and type in the following code:
[self.viewController registerAddon:[CBAds sharedInstance]];
[Chartboost startWithAppId:@"Your App ID"
              appSignature:@"Your App Signature"
              delegate:self];
  1. You will have to go back to your Chartboost account to retrieve the app ID and app signature, and replace in the respective locations.
  2. While you are on your account, when you made your app, if it doesn't have a valid iTunes URL (which means you haven't made your app on iTunes Connect) set Test Mode to enabled
  3. Also what you will need to do is download the Chartboost framework https://answers.chartboost.com/en-us/articles/download
  4. Once you have downloaded it, take the framework and drag it into xCode > Frameworks
  5. Now that we are back in xCode, make sure you have put in your app's app ID and Signature in the right spots, the go to the bottom of AppDelegate.mm right above @end and paste the following code:
- (void)didDismissInterstitial:(NSString *)location {
    NSLog(@"Interstitial dismissed");
    StandaloneCodeaViewController *currentCodea = self.viewController;
    if (currentCodea.paused) {
        currentCodea.paused = NO;
    }
    [CBAds sharedInstance].isPlaying = NO;
}

- (void) didDisplayInterstitial:(NSString *)location {
    StandaloneCodeaViewController *currentCodea = self.viewController;
    currentCodea.paused = YES;
    [CBAds sharedInstance].isPlaying = YES;
}

- (void) didFailToLoadInterstitial:(CBLocation)location withError:(CBLoadError)error {
    const char *errorText = nullptr;
    switch(error){
        case CBLoadErrorInternal: {
            errorText = "Failed to load, internal error.";
        } break;
        case CBLoadErrorInternetUnavailable: {
            errorText = "Failed to load, no Internet connection.";
        } break;
        case CBLoadErrorTooManyConnections: {
            errorText = "Failed to load, too many connections.";
        } break;
        case CBLoadErrorWrongOrientation: {
            errorText = "Failed to load, wrong orientation.";
        } break;
        case CBLoadErrorFirstSessionInterstitialsDisabled: {
            errorText = "Failed to load, first session.";
        } break;
        case CBLoadErrorNetworkFailure: {
            errorText = "Failed to load, network error.";
        } break;
        case CBLoadErrorNoAdFound : {
            errorText = "Failed to load, no ad found.";
        } break;
        case CBLoadErrorSessionNotStarted : {
            errorText = "Failed to load, session not started.";
        } break;
        case CBLoadErrorImpressionAlreadyVisible : {
            errorText = "Failed to load, impression already visible.";
        } break;
        case CBLoadErrorUserCancellation : {
            errorText = "Failed to load, impression cancelled.";
        } break;
        case CBLoadErrorNoLocationFound : {
            errorText="Failed to load Interstitial, missing location parameter.";
        } break;
        case CBLoadErrorAssetDownloadFailure : {
            errorText = "Failed to load, asset download failed.";
        } break;
        case CBLoadErrorPrefetchingIncomplete : {
            errorText = "Failed to load, prefetching of video content is incomplete !";
        } break;
        default: {
            errorText = "Failed to load, unknown error.";
        }
    }
    NSString *OBJCError=[[NSString alloc] initWithUTF8String:errorText];
    [CBAds sharedInstance].errorText=OBJCError;
    [CBAds sharedInstance].errorReturned=YES;
    NSLog(@"Error Returned Bool %d",[CBAds sharedInstance].errorReturned);
    NSLog(@"%@",OBJCError);
}
  1. Now you have officially finished the Objective-C stuff, and now we can move onto the Lua implementation!

Link to OBJC add files: Here and here

Tagged:

Comments

  • Posts: 237

    For the lua implementation, you may want to do it all in codea before exporting, then make necessary changes in xCode. Create a blank file in Codea and paste this in:

    Ads={check=false}
    
    function Ads:update()
        if self.check then
            if self.getErrorText() then
                alert("Failure to load ad","Network Error")
                self.onFailure()
                self.onFailure=function()end
                self.onEnded=function()end
                self.check=false
                self.NMA=true
            end
            if self.playing() and not self.preliminaryCheck then
                self.preliminaryCheck=true
            elseif self.preliminaryCheck and not self.playing() and not self.safetyCheck then
                self.safetyCheck=0
            end
            if self.safetyCheck then
                self.safetyCheck = self.safetyCheck + 1
                if self.safetyCheck>=3 then
                    self.check=false
                    self.onEnded()
                    self.onEnded=function()end
                    self.onFailure=function()end
                end
            end
        end
    end
    
    function Ads.available()return chartboost~=nil end
    
    function Ads:showInterstitial()
        if self.available() and not self.NMA then
            chartboost.showInterstitial()
            self.check=true
            self.preliminaryCheck=false
            self.safetyCheck=nil
        end
    end
    function Ads:showRewardedVideo()if Ads.available() then chartboost.showRewardedVideo()end end
    function Ads:showMoreApps()if Ads.available() then chartboost.showMoreApps()end end
    function Ads.playing()if Ads.available() then return chartboost.playing()end return nil end
    function Ads.getErrorText()if Ads.available() then return chartboost.getErrorText() end return nil end
    function Ads.onEnded()end
    function Ads.onFailure()end
    

    Of course, the adds will not show in codea, but add them in the necessary spots, to show in add, simply says, Ads:showInterstitial(), but only say it once! DO NOT put it in the draw function. It is best to do it with touch or when a class is initialized. Before you show the interstitial, set a onEnded and onFailure function for callbacks so the ad does something when it succeeds or fails, respectively. I hope this helps, my OBJ-C coding skills are awful, I used the other classes as support and worked from there, so if anybody wants to add more to the class and improve, please feel free to do so and let me know so I can also make the additions. Enjoy your monetization!

Sign In or Register to comment.