Scene Variable vs Global Variable: Cloud Firestore Database

I’m super excited to be making my first-ever mobile game. I’ve built out a prototype and was able to figure out how to set up Firebase for authentication.

Now, I’m in the process of setting up the saving and loading so that when you enter the game or into a new scene your stats are loaded / progress is saved during play.

It’s a very simple game. I have one external layout & external event sheet for the main character and its actions and then I have a bunch of very simple scenes the player goes through.

My thinking about how to set this up was when my global variables change (Game States, like the scene you’re on, or User Details like the player’s level) I update a field of a document. This seems to be possible for the saving part. As I moved on to loading, however, I thought to try “Get a field of a document” but I can’t store the value into a global variable, only a scene variable.

The Ask:
Do I need to go into every single scene now and set up all of my variables again as scene variables? Would I then transition them into global variables at the beginning of a scene via my external event sheet? Or do I need to also have the change scene variable to global variable at the scene event level?

Currently, as a prototype, I have about 30 scenes… That process already sounds like a tedious nightmare… If I grew this to say 100 scenes though?

Is my thinking about this correct? Is my process totally way off? Can anyone help point me in the right direction?

I’ve tried checking out the wiki here and watched some youtube videos but I’m still not certain. If you have any other resources you could point me to on the topic, i’d be forever grateful!

Thank you. :slightly_smiling_face:

Im really confused because my external event sheet looks like its set specially to Scene A. So if i dont declare the scene variables, and then go to Scene B, will my external events still work?

You can write the content of the scene variable directly into global variables after loading.

It is also possible with queries. And it is also to load the entire document into a structure variable with an action, but I have never done that.

1 Like

Hey Jack, I appreciate you getting back.

Are you setting this at the scene level or an external event level?

I have 10 variables and 30 scenes. Multiply that by 2 (for adding a change scene variable to global variable action) and i’ll need to add 600 more actions into my game to make loading work???

Can we not just make write field to global variable a thing?

I’m not sure what you mean. You only need to do this once in your first scene, then the loaded global variables are available in every scene and every external sheet.

As option 4 there is also the possibility to save it as JSON, then all childs of a global structure variable are filled with one action.
For the beginning, with only 10 variables, I would live with the 10 extra actions to convert scene to globe.

If it were up to me, absolutely yes. I’ve wish this since the firebase feature came out and have it on my current list. #17 https://forum.gdevelop.io/t/open-for-discussion-a-few-improvement-ideas-for-open-source-contributors/58938
Maybe it’s more complicated than we think and the developer will say, you shouldn’t load 10+ fields manually.

1 Like

:exploding_head: woah, just blew my mind. :exploding_head: okay, so wait. Does this also include my resource bars? If I load the resource bar at sign in, it will automatically update itself at every scene? Variables also change things in the game, like an objects animation, or an item unlock, etc. These would all just get loaded by themselves in every new scene started?

hmmm… :thinking: that’s a thought… I had them under two structures, but maybe I just do only one… :thinking:

I stan this 100%

I would say yes, that’s the point of global variables :slight_smile:

1 Like

Bless your soul. Ill give this a go and report back :saluting_face:

hey @jack, thanks for helping me understand that global variables update across all scenes!

I’m still having trouble writing my fields to firebase and I’m wondering if you can help me troubleshoot?

How do I

Write and load variables to firebase and what corresponding rules will allow only those users to create/access/delete, etc only their own documents?

What is the expected result

I’m trying to write a field of my global structure variable game_states

(which has every variable in my game right now… userid, hunger_status, level, scene #, animation settings, etc.)

to a document based on the unique firebase uid, in a “Users” collection.

What is the actual result

With open security rules (see screenshots), I get an “ok” callback error when I go to write a field.

In Firebase, things don’t look how they should - there aren’t as many variables as I want to write + they don’t seem to be labelled properly.

Then to no surprise, when I “get a field” I see an “ok” callback error, but none of my states load (bars are empty, not the right scene, etc).

Related screenshots

Scene 1 - Register or Login

Scene 2 - Did they log in or register?

tried so many different rules but this is what I have right now…

this is what is storing

any idea what I’m doing wrong or how I can fix it?

A problem will be the delay when loading from database.
The UID expression will be empty in this place. And converting scene to global variables does not work directly after the load field action. There must be an extra event that is executed when the result state variable is “ok”.

Because of the rules, I think it should be match /Users/{document=**} {

So, if I’m understanding correctly…

Last scene will = 1 & firebase will load game_state.hunger to scene variable… here I should add a condition … something like wait 0.5 seconds if the result is “ok” and then set the global hunger variable? I guess I’ll repeat this for the rest of them (hunger, sleep, scene, etc) breaking it up into where I have a bunch of checks for the same condition, (i.e., “if scene = 1”) but separated with their own actions (next is sleep) & conditions (wait)? … should I also wait with the write actions?

Thanks, i’ll go see what changes this makes! :slight_smile:

Update
Instead of many Get fields, you can use the Get document action if you have a scene structure variable, with children who have the same names as the database fields.

1 Like

hmm… :thinking: let me see what I can do… :thinking: I’ll write back after some tinkering :thinking: thanks a lot @jack :slight_smile:

hey @jack thank you so much for all your support! You are the best. Seriously!!!

Using documents over fields was better. I ended up having to do some really funky stuff with my scene variables since writing or loading global variables to documents is not an option.

I’m now writing & loading as expected to the database, woohoo. :partying_face: :tada:

1 Like

Yey that’s nice to hear🎉