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:
Action<Enemy> cb_whenEnemyDirChange;
Vector2 dir;
public Vector2 Dir {
get {
return dir;
}
set {
dir = value;
if (cb_whenEnemyDirChange != null) {
cb_whenEnemyDirChange(this);
}
}
}
public void RegisterEnemyDirChangeCallBack(Action<Enemy> callback)
{
cb_whenEnemyDirChange += callback;
}
=====================================
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:
Enemy enemy = new Enemy();
enemy.RegisterEnemyDirChangeCallBack( OnEnemyDirChange );
void OnEnemyDirChange( Enemy curEnemy )
{
//just some movement code
}
Any response will be grateful and thanks !