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. Dismiss Notice

[Solved]Why my callback function gets triggered multiple times?

Discussion in 'Scripting' started by Yuzuke, Nov 27, 2016.

  1. Yuzuke

    Yuzuke

    Joined:
    Nov 27, 2016
    Posts:
    3
    Ok, I'll try to keep this short:
    there are 2 classes:
    Enemy is a base class and EnemyController derive from monobehaviour.
    the basic idea is all
    enemies will have a variable called dir which means direction, that i can change it in the runtime like through GameManager or whatever else, thus triggers the action delegate callback function and drive each enemy move.
    it works perfectly fine with my
    PlayerController and Player class. the only difference is that in the PlayerController class I use update() function to catch keyboard input and change the dir variable of the player; for enemies I use coroutine and a for loop to iterate through all enemies and things went ugly. the callback function gets triggered like 3-5 times by each enemy whenever their Dir property changes only once.

    so the question is:
    a) - How is that happened ? or what should I avoid to do?
    b) - For enemies, is there a better approach ( surely there is ) aside from this callback-I-dont-quite-get-it ? my game is 2D tile-base roguelike BTW.

    here's some 'fake code' to make things simple ( cuz true code is a disaster ) :
    in the
    Enemy class I have something like this:
    Code (CSharp):
    1.  
    2.     Action<Enemy> cb_whenEnemyDirChange;
    3.     Vector2 dir;
    4.     public Vector2 Dir {
    5.         get {
    6.             return dir;
    7.         }
    8.         set {
    9.             dir = value;
    10.             if (cb_whenEnemyDirChange != null) {
    11.                 cb_whenEnemyDirChange(this);
    12.             }
    13.         }
    14.     }
    15.  
    16.     public void RegisterEnemyDirChangeCallBack(Action<Enemy> callback)
    17.     {
    18.         cb_whenEnemyDirChange += callback;
    19.     }

    =====================================
    in GameManager some where will detect the start of enemy turn and startcoroutine to iterate over all enemies and change the dir of them, maybe I'll skip that part.

    finally something like this in the
    EnemyController class:
    =====================================

    Code (CSharp):
    1.  
    2.   Enemy enemy = new Enemy();
    3.   enemy.RegisterEnemyDirChangeCallBack( OnEnemyDirChange );
    4.  
    5.   void OnEnemyDirChange( Enemy curEnemy )
    6. {
    7.      //just some movement code
    8. }
    Any response will be grateful and thanks !
     
    Last edited: Nov 27, 2016
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Impossible to say, there isn't enough code to see what is going on. I would suggest adding a debug in the method(s) to see who is calling it from where. Or add debugs to where the callback is being called to see who is making the calls.
     
  3. Yuzuke

    Yuzuke

    Joined:
    Nov 27, 2016
    Posts:
    3
    thanks for the tip! However I think the problem is this callback system somehow WILL trigger more than once in a single frame. I solve it by adding a boolean as a restrain. How wonderful the life is!:D( though the question's still there but at least.. it works.. for nowo_O )

    with great gratitude!
     
    Last edited: Nov 27, 2016