Join Harrison Ferrone for an in-depth discussion in this video Data structure overview, part of Unity: Working with Google Firebase.
- [Instructor] If you're going to be using the project in the exercise files for this video, you're going to need to go into the build folder, TestBuild_01, and replace the GoogleService-Info.plist file with your own from the project. Now you downloaded that in a previous video. You need to switch these out because otherwise, you'll be signing up users in my project, which you won't have access to. Now let's get into talking about structuring our data for Firebase. Unlike sequel databases that use relational data structures, Firebase's real time database is a JSON tree.
So what that means is a JSON tree structure has a series of nodes and in those nodes, you can save information by keys. For example, we're going to be using a root player node for our entire game. Now you can see this file is just a sample text file that I've created to kind of show the right way to structure data for Firebase. So here you can see under our root player node, we have all our players structured underneath a specific key, and the key for this is going to be their unique identifier generated from whatever mobile device or simulator they're playing the game from.
So this is going to give us information like their email, their starting level, or their current level and their current score. Now if we want to add data, we could just put it all under out specific UID inside of our main player node, and Firebase would support that, although madly nesting bunches and bunches of data under a single node is going to slow your program down and you're going to have to do a ton of deconstructing the JSON snapshots that you get back from the Firebase API.
So we can avoid that by making our database flat, or what's called denormalizing your data. It allows your data to quickly and efficiently be pulled down without having to download say the entire player node and then to sort through for what you want. So for example, if we wanted to have a list of quests and we wanted to know what players were currently involved in a certain quest, we wouldn't want to put that information under our player's node, 'cause we would have to pull down or filter through that and that gets really complicated.
So what we would do is we would create a new node called quest, and we would list all the quests. So here I've just made up a couple, so like fire_trial, collect_firewood, blazing_torch, and so on. And inside there, we just store key value pairs with the players UID and just true. That just means that Firebase will recognize that this is a actual value and not a null value. So this is what's called indexing your database and it's a really handy tool because we don't have to go through lists and lists of data to get what we want.
Likewise, if we wanted to store say, high scores for each quest, we wouldn't necessarily want to do that in our quest node because we don't care really who is part of a trial when we just want the high scores. So we break that out into something like a quest high scores node where we again would list them by the title of a quest and then we would store information inside that individual quest. So in this case, fire_trial would have three active participants and each of them would have a different high score.
So as a quick note before we get back to our code, JSON trees aren't relational by default but it is possible and encouraged in some situations to manually create those relationships. So for example, if we're updating a player or we're updating a quest and we need to give player specific information, we would need to update our quest node and our quest high scores node at the same time. While this can generate a fair amount of duplicate data, there's a strong efficiency advantage to structuring your data this way when scaling up your games.
- Working with Firebase
- Adding signup and login functionality
- Building a player class
- Saving data
- Retrieving data
- Displaying data in the Unity GUI
- Making a reusable asset package for other Unity projects