unity3d - calling a function in multiple derived classes in c# -


how can call function in derived classes when global game state changes?

public abstract class gamestatebehaviour {      public event eventhandler<gamestateevents> statechanged;      protected virtual void onstatechanged(gamestateevents e) {         // call function in derived     }  } 

if want inform other classes state change, use 'observer pattern' (see: http://en.wikipedia.org/wiki/observer_pattern). there 2 parties:

  • one party "the object", change state. has duty inform others.
  • the other party "the observer". multiple observers want observe state change of object. want informed.

try this:

class theobject {     public event eventhandler<gamestateevents> statechanged;      // if object changes state, function should called. informs observers invoking statechanged delegate.     protected virtual void onstatechanged(gamestateevents e)     {         if (statechanged != null)              statechanged(this, e);     } }  class theobserver {      public theobserver(theobject o)      {          o.statechanged += object_statechanged;      }       // function called object when object changes state.      private void object_statechanged(object sender, gamestateevents e)      {      } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -