#it loads firebase and everything from

1 messages · Page 1 of 1 (latest)

quasi coyote
#

what does get initial data do?

split oriole
#

look in the second class i sent
public async UniTask GetInitialData() => _dataSnapshot = await _databaseReference.GetValueAsync();

quasi coyote
#

sorry it wasn't formatted hard to see

#

suffice it to say not even discord is formatting this well

split oriole
#
private async UniTaskVoid LoadFirebaseAndGame()
{
    Application.targetFrameRate = 120;
    await FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(async task =>
    {
        if (task.Exception == null)
        {
            FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
            await _databaseManager.GetInitialData();
            ScenesLoader.Instance.LoadGame();
            Destroy(gameObject);
        }
        else
        {
            _canvas.gameObject.SetActive(true);
        }
    });
}
#

like this?

#
public class DatabaseManager : MonoBehaviour, IDatabase
{
    private DatabaseReference _databaseReference;
    private DataSnapshot _dataSnapshot;
    private void Awake() => _databaseReference = FirebaseDatabase.DefaultInstance.RootReference.Child(SystemInfo.deviceUniqueIdentifier);
    public async UniTask GetInitialData() => _dataSnapshot = await _databaseReference.GetValueAsync();
    public object GetValueFromDatabase(string path) => _dataSnapshot.Child(path)?.GetValue(true);
    public void SetValueToDatabase(string path, object value) => _databaseReference.Child(path).SetValueAsync(value);
quasi coyote
#

so did you author this code or did a chatbot? it looks like you authored it

#

i am just checking

#

why are you using the deviceuniqueidentifier?

#

as a child?

split oriole
#

its an idle game i want to publish so i want everyone to have its own stats

quasi coyote
#

it isn't unique

#

on ios

#

and i don't know if it's unique in editor

split oriole
#

idk about ios but on editor its unique

quasi coyote
#

there is a lot going on

#

in your code

#

for something that is very simple

split oriole
#

probably

#

its my first time doing it

#

i wanted to read all the data once and put it in a datasnapshot, only then load the next scene and have everything read from that datasnapshot

#

to reduce reads from the database

#

does it make sense?

quasi coyote
#

add unirx. then:

private async UniTask<IReactiveProperty<int>> GetScoreReactive(string userScorePath) {
  await FirebaseApp.CheckAndFixDependenciesAsync().ToUniTask();
  // you will want persistence
  FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(true);
  var root = FirebaseDatabase.DefaultInstance.RootReference;
  var lastSeenScore = (int)(await root.Child(userScorePath).GetValueAsync()).Value;
  var score = new ReactiveIntProperty(lastSeenScore);

  // whenever you write to the score, save throttled to the database
  // todo: application quit is probably more complicated than this
  score
   .Sample(
      // save the latest score every 5 seconds or when the user quits
      Observable.Timer(TimeSpan.FromSeconds(5f))
        .Merge(Observable.OnceApplicationQuit())
   )
   .Subscribe(async (latestScore) => {
     await root.Child(userScorePath).SetValueAsync(latestScore);
   })
   .AddTo(this);

  return score;
}

that's it.

#

@split oriole does this make sense?

quasi coyote
#

i don't think you explicitly save to the database

#

it saves for you

#

it can deal with syncing separately

#

and choose a good schedule