Use Firebase Realtime Database with Cocos Creator.

Introduction

This is the first post. Hello. Nowadays, when it comes to cross-platform game engines, Unity is the choice. I think that UE will come out somehow at the runner-up. In the meantime, I'm glad I started working on cocos creator, but I'm having trouble with the lack of information, so I'll post a memorandum.

What you want to do

Access Firebase's Realtime Database with an app being developed by cocos creator.

Method

Since cocos creator uses Node.js, basically npm is used. However, this method did not work on Android. (No problem on iOS or Web) I had no choice but to install Firebase Database in Android Studio and call it through Java.

npm install --save firebase Using a terminal etc. in the project folder of Cocos creator

npm install --save firebase

To execute. The following article was very helpful in this regard. Implement Firebase Authentication using Cocos Creator

At first, the module did not load well, but when I restarted Cocos Creator, it seems to load successfully. After that, you can run it without problems by using Firebase with Node.js. Add Firebase to your JavaScript project (https://firebase.google.com/docs/web/setup?hl=ja)

Android The module is not available on Android for some reason. There may be something wrong with the fact that you are using Node.js npm. (I can't figure it out) In the above article, at least Firebase Authentication worked fine, but I couldn't use the Firebase Realtime Database in my environment, so I decided to use Java directly.

First, you need to have Firebase in your project. Add Firebase to your Android project (https://firebase.google.com/docs/android/setup?hl=ja) We will utilize the Firebase Realtime Database introduced in this way through Java. How to use Java with javascript from cocos creator

jsb.reflection.callStaticMethod(className, methodName, methodSignature, parameters...)

Use the. How to Call Java methods using JavaScript on Android

Specifically, put the Java package you want to use in org.cocos2dx.javascript and call it from javascript. At first I didn't know the location of org.cocos2dx.javascript, so I'll write it down. /Project_Folder/build/jsb-default/frameworks/runtime-src/proj.android-studio/src/org/cocos2dx/javascript/ It is in.

there,

FirebaseWrapper.java


package org.cocos2dx.javascript;

import android.util.Log;
import com.google.firebase.database.FirebaseDatabase;

public class FirebaseWrapper {
    private static FirebaseDatabase database=null;

    public static void init(){
        if(null==database){
            database=FirebaseDatabase.getInstance();
            Log.d("Firebase Wrapper","Firebase Initialized!");
        }
    }
}

I made something like that. To do this on javascript

jsb.reflection.callStaticMethod("org/cocos2dx/javascript/FirebaseWrapper","init","()V")

Is called. Unfortunately, `result = jsb.reflection.callStaticMethod ()` can pass both arguments and return values. Specifically, you can only pass int, float, boolean, String. So you can't pass an array etc. Something needs to be done, such as handing them one by one.

But for now, you can safely access the Firebase Realtime Database.

At the end

This time, I made the app for the first time using cocos creator, but honestly it was very tough due to my lack of technology. There are still many aspects of lack of information, and I felt that amateurs shouldn't have done anything because basically they had no choice but to collect information in English or Chinese. However, because of this, it is difficult to increase the amount of information, so I will post this article for someone in the future or myself.

I've learned a lot, but I think I'll probably use Unity obediently next time. Lol

Recommended Posts

Use Firebase Realtime Database with Cocos Creator.
[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore
Use ProGuard with Gradle
Use Puphpeteer with Docker
Use XVim2 with Xcode 12.0.1
Use CentOS with LXD
Use ngrok with Docker
Use webmock with Rspec
Use WebJars with Gradle
Use jlink with gradle