c# - How to save a method in a class to be executed later -


i need know how pass method class constructor can called later. idea have bullet class has 2 properties, damage integer , method can called when bullet of type has hit object. code below should explain bit better:

public class bullet {     public method onhit;     public int damage;     public bullet(int damage,method onhit)     {         this.damage = damage;         this.onhit = onhit;     } } 

this can make bullets preform different tasks upon impact running bullet.onhit(hitgameobject).

you can use action pass function function store in action. function stored can called action.invoke().

public class bullet {     public int damage;     system.action savedfunc;      public bullet(int damage, system.action onhit)     {         if (onhit == null)         {             throw new argumentnullexception("onhit");         }          this.damage = damage;         savedfunc = onhit;     }      //somewhere in bullet script when bullet damage == damage     void yourlogicalcode()     {         int somebulletdamage = 30;         if (somebulletdamage == damage)         {             //call function             savedfunc.invoke();         }     } } 

usage:

void start() {     bullet bullet = new bullet(30, mycallbackmethod); }  void mycallbackmethod() {  } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -