Question regarding delegates and events
I have been recently learning about delegates & events. I would like to know performance wise if there is another way to do this. Or should I just write events for every single command like so:
public delegate void IndividualUnitAttackingDelegate(GlobalAISensor aiSensor);
public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack1;
public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack2;
public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack3;
public delegate void IndividualDefendingUnitDelegate(Vector3 destination);
public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend1;
public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend2;
public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend3;
here is how it's being used in this main event script if for whatever reason you need that information:
void DetermineTargetDestination()
{
if (Enemy1DefendPlanet)
MoveEnemyUnitToDefend1(defendPosition1);
if (Enemy2DefendPlanet)
MoveEnemyUnitToDefend2(defendPosition2);
if (Enemy3DefendPlanet)
MoveEnemyUnitToDefend3(defendPosition3);
if (!Enemy1DefendPlanet && aiSensor1 != null && MoveEnemyUnitToAttack1 != null)
MoveEnemyUnitToAttack1(aiSensor1);
if (!Enemy2DefendPlanet && aiSensor2 != null && MoveEnemyUnitToAttack2 != null)
MoveEnemyUnitToAttack2(aiSensor2);
if (!Enemy3DefendPlanet && aiSensor3 != null && MoveEnemyUnitToAttack3 != null)
MoveEnemyUnitToAttack3(aiSensor3);
Enemy1DefendPlanet = false;
Enemy2DefendPlanet = false;
Enemy3DefendPlanet = false;
}
as a note this is regarding over a 1000 active units with commands being passed in and out that is why I am currently using delegates...
$$anonymous$$ay I ask why you use 3 different events of each type? Do they have different subscribers?
Answer by YoungDeveloper · Sep 15, 2015 at 09:02 PM
Why not pass the command as secondary param?
do you mean create something like IndividualUnitDelegate
and then add both of these delegates to that delegate? I'm not adding a 2nd param and just using 1 delegate because then it would be processing that 2nd param in code where it's not needed.
I mean passing in function everything you need $$anonymous$$yDelegate(Sensor sensor, AttackType AttackType);
And or receive
switch(attackType){
case AttackType.$$anonymous$$elee:
//work
break;
}
that's where my issue is sensor would only be used in attack and the vector3 would only be used in defend. maybe I should just do $$anonymous$$yDelegate(AttackType AttackType)? Would this actually process information faster this way? performance matters a lot here it's 1000 units of data passing through scripts.
Your answer
Follow this Question
Related Questions
Instance variables and this == null in event handler 1 Answer
Different approach to an upgrade system 4 Answers
Event NullReferenceException 1 Answer
Argument Exception when no arguments? Method Arguments are Incompatible 0 Answers
Keycode returning none even when I filter KeyCode.None out? 0 Answers