Hello everyone,
I am using callback functions for the first time and I have a behaviour which I cannot explain. I hope you can help me.
My aim is to get a return value (a warning text from a firebase connection) from within an IEnumerator from another script.
UIManager Skript:
public void RegisterButton()
{
string warningText = "";
StartCoroutine(FirebaseManager.instance.Register(emailRegisterField.text,
passwordRegisterField.text,
passwordRegisterVerifyField.text,
usernameRegisterField.text,
callbackWarningText =>
{
warningText = callbackWarningText;
Debug.Log("INSIDE: " + warningText);
}));
Debug.Log("OUTSIDE: " + warningText);
warningRegisterText.text = warningText;
}
FirebaseManager Skript:
public IEnumerator Register(string _email, string _password, string _confirmPassword, string _username, System.Action<string> callbackWarningText)
{
if (_username == "")
{
//If the username field is blank show a warning
callbackWarningText("Missing Username");
}
else if (_password != _confirmPassword)
{
//If the password does not match show a warning
callbackWarningText("Password Does Not Match!");
}
else
{
//Call the Firebase auth signin function passing the email and password
var RegisterTask = fbAuth.CreateUserWithEmailAndPasswordAsync(_email, _password);
//Wait until the task completes
yield return new WaitUntil(predicate: () => RegisterTask.IsCompleted);
if (RegisterTask.Exception != null)
{
//If there are errors handle them
Debug.LogWarning(message: $"Failed to register task with {RegisterTask.Exception}");
FirebaseException firebaseEx = RegisterTask.Exception.GetBaseException() as FirebaseException;
AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
string message = "Register Failed!";
switch (errorCode)
{
case AuthError.MissingEmail:
message = "Missing Email";
break;
case AuthError.MissingPassword:
message = "Missing Password";
break;
case AuthError.WeakPassword:
message = "Weak Password";
break;
case AuthError.EmailAlreadyInUse:
message = "Email Already In Use";
break;
}
callbackWarningText(message);
...
I mentioned that it works partially in the title since the first 2 checks (username + password) in the firebaseManager work as expected. If one of them is true I get the following debug output:
INSIDE: Missing Username
OUTSIDE: Missing Username
However, if something from within the switch statement triggers, the debug output is:
OUTSIDE:
INSIDE: Missing Email
So, I do not get a return value in these cases.
What got my attention is that in the latter case the OUTSIDE string is logged first. This could mean that the code within RegisterButton() continues before the warningText is set within the FirebaseManager, but why and why only do I have different behaviours?