ddk4113
January 18, 2018, 10:10am
1
Here’s a piece of code that i want to execute but how can i wait for a specific bool to be true inside a function
bool one = false;
bool two = false;
public bool A(){
if (B())
return true;
else
return false;
}
private bool B(){
one = false;//1
StartCoroutine (C ());//2
// I need to wait until bool two is true and then return one
yield return one;//5
}
private IEnumerator C (){
two = false;//3
yield return new WaitForSeconds (1f);
if(something is true)
one = true;
else
one = false;
yield return one;
two = true;//4
}
My current execution order is 1,2,5,3,4 but i need the execution order as 1,2,3,4,5.
Please Help??
I think you’re looking for
yield return new WaitUntil( () => B() );
// where B is your boolean method.
In your case, use
yield return new WaitUntil( () => two == true );
Hellium
January 18, 2018, 10:53am
3
Further to your real code, here is how I would do it. I haven’t tested the following code though.
#region Checking For Internet
public void CheckForInternet( System.Action onInternetAvailable, System.Action onInternetUnavailable )
{
if( !IsInternetReachable() )
{
if( onInternetUnavailable != null )
onInternetUnavailable();
return ;
}
StartCoroutine( CheckForInternetConnectivityIEnum( onInternetAvailable, onInternetUnavailable ) ) ;
}
private bool IsInternetReachable()
{
return Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork || Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork ;
}
System.Collections.IEnumerator CheckForInternetConnectivityIEnum(System.Action onInternetAvailable, System.Action onInternetUnavailable)
{
WWW www = new WWW ("some api which gives sucess 1");
yield return www;
if ( www.error == null )
{
JsonData jsonDat = JsonMapper.ToObject (www.text);
Debug.Log( "Login API success = " + jsonDat["success"] );
if ( jsonDat["success"].ToString() == "1" )
{
if( onInternetAvailable != null )
onInternetAvailable();
}
else
{
if( onInternetUnavailable != null )
onInternetUnavailable();
}
}
else if( onInternetUnavailable != null )
onInternetUnavailable();
}
#endregion
And call it as follow :
public void Foo()
{
CheckForInternet( OnInternetAvailable, OnInternetUnavailable ) ;
}
public void OnInternetAvailable()
{
Debug.Log("Internet is available");
}
public void OnInternetUnavailable ()
{
Debug.LogWarning("Internet is unavailable");
}
#INITIAL ANSWER
I don’t think what you are trying to achieve is possible without more coroutines and callbacks. Here is my suggestion:
bool one = false;
bool two = false;
public void A( System.Action<bool> onResultFetched )
{
StartCoroutine( B( onResultFetched ) );
}
private System.Collections.IEnumerator B( System.Action<bool> onResultFetched )
{
one = false;
yield return StartCoroutine( C() );
onResultFetched.Invoke( one );
}
private System.Collections.IEnumerator C()
{
two = false;
yield return new WaitForSeconds( 1f );
if ( something is true )
one = true;
else
one = false;
two = true;
}
And then, you call A
with a callback:
private void Test()
{
A( ( result ) => Debug.Log( result ) );
}