Getting Started with Firebase

2
  • 7 favourites

Stats

7,472 visits, 11,273 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

DISCLAIMER:

Although the things covered in this tutorial are not hard to do, I wouldn't suggest using Firebase to anyone who's not familiar with Construct 2.

Setup

First, you need to get a Firebase account. Go to firebase.com and sign up with a Google account. Then you can create an app: the URL you set here will be the way you access your data from Construct 2.

To use Firebase, you need to download the plugins from RexRainbow. Luckily, Rex made lots of different plugins to suit your needs. Of course almost all of Firebase's functionality could be unleashed with just the Core plugin, but the rest make it easier to do certain things, for example make a leaderboard or a counter. I recommend getting the following plugins:

Core - http://c2rexplugins.weebly.com/rex_firebase.html

Auth - http://c2rexplugins.weebly.com/rex_firebase_authentication.html

Itemtable - http://c2rexplugins.weebly.com/rex_firebase_itemtable.html

Of course you can always get any more of them at c2rexplugins.weebly.com

After you have installed these plugins, put them in your project, and in the properties tab you need to individually paste your App URL into every plugin's properties. In the

Usage

So you're done everything so far? Great. Using it won't be hard. I'll be covering the usage of the Itemtable plugin, since I think that has the most potential compared to how easy it is to use. Writing into your database is really easy. What I think is hard is figuring out how you want to organize things. You see Firebase uses the JSON format, and saves data that way. It doesn't have tables of data, and I find it harder to understand this way. For the purpose of this tutorial, we are going to make a simple database like this:

    appname
        parent
            key1: value
            key2: different value

This, by the way, is a good representation of how Firebase stores everything. You can always organize things by having multiple levels of parents. To only have one level, just like in the example, you need to set the ItemTable object's Sub domain property to nothing. This will eliminate the first layer.

Writing

To write to your database with the Itemtable plugin, you use the Prepare: Set value action, then Save: save. In Prepare: Set value, you can set the key1 and the value from the example you can see above. In the Save: save, the only thing you really need to worry about is the itemID, which you will see as the parent in the example. If you want to set multiple items at once, you can add another Prepare: Set value. This is how it will look like in Constuct 2:

And this is the result in Firebase:

Reading

The easiest way to read things with the Itemtable plugin is to just have it download everything from the database, and filter it by itemID locally. Of course if the database is too big, you may have to try a different technique. Now to do take the easy route, just put a Load: Load all action somewhere BEFORE the time you want the data. It doesn't have to be on start of the layout, but just keep in mind that this download takes at least 0.1 second. You should set a tag, something like "all" or "main". If you need the data as fast as possible, use the Load: On load complete event. Use the ItemTable.At expression to get the value associated with a certain key. For example, if you want to get the value associated with key2 from the example above, you use ItemTable.At("parent", "key2").

Afterword

First of all, thank you for reading. Thanks to RexRainbow for making these awesome plugins. Thanks to Firebase for offering this service for free for everyone. I hope that this tutorial has helped you make great games and if you have any questions, feel free to leave a comment.

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • I installed all the plugins as it is written I made changes but when I export to cordova the project, I get an error, as it does not allow compilation due to firebase ??? how can I more correctly import the pages and give to export through cordova?

  • How to make so that at startup variables are not replaced if they are already in the database?