Wait until database...
 
Notifications
Clear all

Wait until database loaded

4 Posts
2 Users
0 Reactions
852 Views
(@jackmariani)
Posts: 11
Eminent Member
Topic starter
 

Hi again and thank you for the help so far.

I've another question.

I have to do login the player and update the database.

The plan is to do the following:

1) Login the player
2) Wait until it finishes loading
3) Update the player

Alas seems I've a few problems with coroutines.

I tried this:

IEnumerator WaitDatabase (bool busy)
{
while (busy)
{
yield return null;
}
}

public IEnumerator LoginPlayer()
{
//Login player
busy = false;
}

public void UpdatePlayer()
{
//update player
}

void Update
{
LoginPlayer();
WaitDatabase();
UpdatePlayer();
}

I'm still learning co routines and I think I really could use a hand.

Thank you.

 
Posted : 05/09/2014 8:01 pm
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

Not sure this question is related to Combu, since Combu doesn't use co-routines but you register you event handlers to do everything and be notified when an action finishes.

Anyway in the sample logic you described above I see two main issues:

  • Update is called every frame, so if you put all the calls there then they will run every frame, that is not what you're looking for; better approach is to move them in Start
  • to start a function as co-routine in C# you must call them like this: StartCoroutine(WaitDatabase())

So at the end your code should be similar to this:

IEnumerator Start() {
LoadDatabase();
yield return StartCoroutine(WaitDatabase());
UpdateDatabase();
}

void LoadDatabase() {
// Load your stuff
}

IEnumerator WaitDatabase() {
// Wait stuff
yield return;
}

void UpdateDatabase() {
// Update stuff
}

Look at the documentation for more examples.

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 05/09/2014 8:18 pm
(@jackmariani)
Posts: 11
Eminent Member
Topic starter
 

Not sure this question is related to Combu, since Combu doesn't use co-routines but you register you event handlers to do everything and be notified when an action finishes.

Yes.

The idea is to Login the Player with OnUserLogin, wait he is logged, and then update him with OnUserUpdate. All using just one function that (1) log him in and (2) updates (and then (3) logs him out).

I just need to wait before having him logged in and I planned to use CoRoutines.

I'm still checking the doc, but if there's a better way to do that avoiding co routines I'll use that.

Thank you very much for the answers so far.

 
Posted : 05/09/2014 8:41 pm
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

Yes, to avoid the coroutines you should: register your event handler for OnLogin.

bool busy = false;

void Start () {
   CBManager cbManager;
   // Find the CBManager instance
   cbManager = (CBManager)FindObjectOfType(typeof(CBManager));
   busy = true;
   cbManager.Login("username", "password");
}

void OnEnable () {
   CBManager cbManager;
   // Find the CBManager instance
   cbManager = (CBManager)FindObjectOfType(typeof(CBManager));
   cbManager.OnLogin += OnUserLogin;
   cbManager.OnUpdateUser += OnUserUpdate;
}

void OnDisable () {
   CBManager.instance.OnLogin -= OnUserLogin;
   CBManager.instance.OnUpdateUser -= OnUserUpdate;
}

void OnUserLogin (CBUser user, string error) {
   if (user != null) {
      Debug.Log(" User logged in ");
      // Logged in, now you can update its data:
      // CBManager.instance.loggedUser
   } else {
      Debug.LogError("Error during login: " + error);
      busy = false;
   }
}

void OnUserUpdate (CBUser user, string error) {
   if (user != null) {
      Debug.Log(" User has been updated ");
   } else {
      Debug.LogError("Error during update: " + error);
   }
   busy = false;
}

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 08/09/2014 11:22 am
Share: