Moderator: marauder2k9
dependencies { implementation 'com.google.android.gms:play-services-ads:18.2.0' }Then in the AndroidManifest.xml u need to add meta data to your application settings like so
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:hardwareAccelerated="true" android:hasCode="true" tools:ignore="GoogleAppIndexingWarning"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-################~##########"/> <activityandroid:value="your app id on admob"
public static void ToggleLoadAd(final Context context, final boolean show) { if (show) { final Activity activity = (Activity) context; activity.runOnUiThread(new Runnable() { @Override public void run() { MobileAds.initialize(activity, new OnInitializationCompleteListener(){ @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); mInterstitialAd = new InterstitialAd(activity); mInterstitialAd.setAdUnitId("ca-app-pub-################/###########"); mInterstitialAd.loadAd(new AdRequest.Builder().build()); Log.i("TAG", "The interestitial is being loaded"); mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { //load new ad mInterstitialAd.loadAd(new AdRequest.Builder().build()); } }); } }); }Log.i("TAG","The ad load is set to false"); } public static void ToggleShowAd(final Context context, final boolean show) { if (show) { final Activity activity = (Activity) context; activity.runOnUiThread(new Runnable(){ @Override public void run() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); Log.i("TAG", "Showing the ad"); } else { Log.i("TAG", "The interestitial is not ready"); } } });Log.i("TAG", "The ad show is set to false"); } }mInterstitialAd.setAdUnitId("ca-app-pub-################/###########"); should be your interstitial ad unit id.
extern bool android_DumpDirectories(const char *basePath, const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath);add
extern void toggleLoadAd(bool show); extern void toggleShowAd(bool show);then in T2DActivity.cpp after the double timeGetTime function closes add these 2 functions
void toggleLoadAd(bool show) { // Attaches the current thread to the JVM. jint lResult; jint lFlags = 0; JavaVM* lJavaVM = platState.engine->app->activity->vm; JNIEnv* lJNIEnv = platState.engine->app->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) { return; } // Retrieves NativeActivity. jobject lNativeActivity = platState.engine->app->activity->clazz; jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity); jmethodID getClassLoader = lJNIEnv->GetMethodID(ClassNativeActivity,"getClassLoader", "()Ljava/lang/ClassLoader;"); jobject cls = lJNIEnv->CallObjectMethod(lNativeActivity, getClassLoader); jclass classLoader = lJNIEnv->FindClass("java/lang/ClassLoader"); jmethodID findClass = lJNIEnv->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); jstring strClassName = lJNIEnv->NewStringUTF("com.garagegames.torque2d.T2DUtilities"); jclass T2DUtilitiesClass = (jclass)lJNIEnv->CallObjectMethod(cls, findClass, strClassName); jmethodID MethodT2DUtilitiesClass = lJNIEnv->GetStaticMethodID(T2DUtilitiesClass, "ToggleLoadAd", "(Landroid/content/Context;Z)V"); lJNIEnv->CallStaticVoidMethod(T2DUtilitiesClass, MethodT2DUtilitiesClass, lNativeActivity, (jboolean)show); // Finished with the JVM. lJavaVM->DetachCurrentThread(); } void toggleShowAd(bool show) { // Attaches the current thread to the JVM. jint lResult; jint lFlags = 0; JavaVM* lJavaVM = platState.engine->app->activity->vm; JNIEnv* lJNIEnv = platState.engine->app->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) { return; } // Retrieves NativeActivity. jobject lNativeActivity = platState.engine->app->activity->clazz; jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity); jmethodID getClassLoader = lJNIEnv->GetMethodID(ClassNativeActivity,"getClassLoader", "()Ljava/lang/ClassLoader;"); jobject cls = lJNIEnv->CallObjectMethod(lNativeActivity, getClassLoader); jclass classLoader = lJNIEnv->FindClass("java/lang/ClassLoader"); jmethodID findClass = lJNIEnv->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); jstring strClassName = lJNIEnv->NewStringUTF("com.garagegames.torque2d.T2DUtilities"); jclass T2DUtilitiesClass = (jclass)lJNIEnv->CallObjectMethod(cls, findClass, strClassName); jmethodID MethodT2DUtilitiesClass = lJNIEnv->GetStaticMethodID(T2DUtilitiesClass, "ToggleShowAd", "(Landroid/content/Context;Z)V"); lJNIEnv->CallStaticVoidMethod(T2DUtilitiesClass, MethodT2DUtilitiesClass,lNativeActivity, (jboolean)show); // Finished with the JVM. lJavaVM->DetachCurrentThread(); }in the same file in the static int engine_init_display there is if (SetupComplete == false)
if (SetupCompleted == false) { toggleLoadAd(false); toggleShowAd(false);Further down in the same file you will see consolefunctions after the console function for dumpFontList around line 2068 add
ConsoleFunction(loadAd,void,1,1,"load ad") { toggleLoadAd(true); } ConsoleFunction(showAd,void,1,1,"show ad") { toggleShowAd(true); }Now in your games module you call loadAd() to load an ad and showAd to show an ad whenever and wherever you like in your script. What i think is good practice is to loadAd when your game is launched this way whenever you decide to show the ad it should be loaded properly and ready for displaying to the user. Included in this code is a bit of code that will be executed if you try to show an ad that isnt ready so it shouldnt cause any problems for end users.
<bool name="isTablet">false</bool>between the <resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="isTablet">true</bool> </resources>repeat this process for a folder called values-xlarge
public static int GetDeviceType() { if (isTabletSize) { return 1; } else { return 0; } } }in MyNativeActivity.java
public class MyNativeActivity extends NativeActivity { static { System.loadLibrary("openal"); }add:
public static boolean IsTabletSize; }scroll on down to about line 40 you should see
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);after this add:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet); if(tabletSize){ IsTabletSize = true; } } public static boolean getData() { return IsTabletSize; }now to link it all to script!
extern int GetDeviceType();then in T2DActivity.cpp
int GetDeviceType(){ // Attaches the current thread to the JVM. jint lResult; jint lFlags = 0; JavaVM* lJavaVM = platState.engine->app->activity->vm; JNIEnv* lJNIEnv = platState.engine->app->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) { return 0; } // Retrieves NativeActivity. jobject lNativeActivity = platState.engine->app->activity->clazz; jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity); jmethodID getClassLoader = lJNIEnv->GetMethodID(ClassNativeActivity,"getClassLoader", "()Ljava/lang/ClassLoader;"); jobject cls = lJNIEnv->CallObjectMethod(lNativeActivity, getClassLoader); jclass classLoader = lJNIEnv->FindClass("java/lang/ClassLoader"); jmethodID findClass = lJNIEnv->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); jstring strClassName = lJNIEnv->NewStringUTF("com.garagegames.torque2d.T2DUtilities"); jclass T2DUtilitiesClass = (jclass)lJNIEnv->CallObjectMethod(cls, findClass, strClassName); jmethodID MethodT2DUtilities = lJNIEnv->GetStaticMethodID(T2DUtilitiesClass, "GetDeviceType", "()I"); jint jret = lJNIEnv->CallStaticIntMethod(T2DUtilitiesClass, MethodT2DUtilities); if(jret == 1) { __android_log_write(ANDROID_LOG_INFO,"TAG","setting device type is tablet"); Con::setVariable("$deviceType","tablet"); } else { __android_log_write(ANDROID_LOG_INFO,"TAG","setting device type is phone"); Con::setVariable("$deviceType","phone"); } lJNIEnv->DeleteLocalRef(strClassName); // Finished with the JVM. lJavaVM->DetachCurrentThread(); return jret; }Then find
if (SetupCompleted == false)and inside this if statement add
GetDeviceType();This will make sure the code is run when the app is started and it will assign the values. Now anywhere inside the scripts for your game you would do this:
if ($deviceType $= "tablet") { //make tablet player suffer } else { //haha u got a phone }I mostly use this just for setting different camera zooms and difficulty settings for different devices so you can design your game to work on tablet and then zoom in a bit and now you have it set for phone. Also a different hud for each device is a must usually.