Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unsolvable issue with System.Action and IEnumerator

Discussion in 'Scripting' started by Diablo404, Oct 13, 2014.

  1. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    I have 2 scripts. One in UnityScript called Calling.js, another in C# called DelegateHandler.cs, here are the two scripts:

    Calling.js:

    Code (JavaScript):
    1.  function Awake ()
    2.     {
    3.         var myHandler : DelegateHandler = DelegateHandler.Get().OnEvent(Callback);
    4.         myHandler.TryHandler();
    5.     }
    6.    
    7.     function Callback( _string : String ) : IEnumerator
    8.     {
    9.         //yield WaitForSeconds(1);
    10.         print("value = "  + _string);
    11.     }
    12.  
    And DelegateHandler.cs:

    Code (CSharp):
    1.  public class DelegateHandler : MonoBehaviour
    2.     {
    3.         public static System.Action<string> callbackAction;
    4.    
    5.         public static DelegateHandler  Get()
    6.         {
    7.             DelegateHandler delegateHandler = GameObject.Find ("Main Camera").AddComponent<DelegateHandler>();
    8.             return delegateHandler;
    9.         }
    10.    
    11.    
    12.         public DelegateHandler  OnEvent( System.Action<string> _callback)
    13.         {
    14.             callbackAction = _callback;
    15.             return this;
    16.         }
    17.    
    18.         public void TryHandler()
    19.         {
    20.              callbackAction("test") ;
    21.         }
    22.        
    23.     }
    I need OnEvent to be DelegateHandler typed.

    Problem is: If I uncomment the yield WaitForSeconds(1) on the .js , the Callback function is never called.

    I've searched for hours for workaround with delegates() , abstract methods and everything but I can't find anything to make this work... I think I have teared enough hair of my head to make myself a new wig!

    Any help/hints would be really apreciated
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    My first thought (not knowing about Unitys internal handling of IEnumerator) is that maybe (and I know I read somewhere but can't remember where) unitys lack of support for actions and events is the possible cause for it not being called after the yield line. My first thought for a solution is to create another method (for the ienumerator) and call startcoroutine on that inside the action callback. Might work? hope this helps! :)
     
  3. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Yeap, I already tried with success something like this

    Code (JavaScript):
    1. function Callback( _string : String )
    2.    {
    3.        CallbackFake( _string );
    4.    }
    5.  
    6. function CallbackFake( _string : String ) : IEnumerator
    7.    {
    8.       yield WaitForSeconds(1);
    9.        print("value = " + _string)
    10.    }
    But I want to avoid it. And just have one real callback function
     
    Last edited: Oct 13, 2014
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
  5. Hippiecode

    Hippiecode

    Joined:
    Feb 13, 2012
    Posts:
    110
    try to use invoke function insteads of yield WaitForSeconds
     
  6. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Why would I do such a thing?