Translations

Know another language? Translate this tutorial!

Stats

2,139 visitors
3.8K page views

Getting Started with Firebase

Favourite 6 favourites
Tutorial written by DaemonOriginally published on 13th, March 2016 - 1 revision

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 http://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 http://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:
Open full size image

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.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

0
mvitlov 1,789 rep

Thanks! Helped me a lot!

Tuesday, June 14, 2016 at 9:20:11 PM
0
redfoc 4,323 rep

thanks :-)

Friday, August 05, 2016 at 5:37:17 PM
0
iceangel 32.6k rep

Thanks for sharing!

Wednesday, October 12, 2016 at 4:05:11 PM
0
yenleidong 2,674 rep

this is very helpfully, thanks for sharing.

Tuesday, April 11, 2017 at 4:32:01 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.