Search Unity

Multiple events -

Discussion in 'Scripting' started by RyanPaterson, Mar 30, 2014.

  1. RyanPaterson

    RyanPaterson

    Joined:
    Dec 14, 2013
    Posts:
    77
    Hi!

    After watching a lecture/talk from Ken Levine about a rpg like game narratives, I was interested in trying out a situation like that.
    My example is 2 NPC's - borog and justin

    Borog likes shrines being built - he does not like wells
    Justin likes wells being built - he does not like shrines

    I was thinking events could be a good way of holding the 'build states' so I was thinking... like below, have two events, is the best way to do something like this?

    I want to be efficient

    Code (csharp):
    1.  
    2.         public delegate void buildShrine();
    3.     public static event buildShrine onBuildShrine;
    4.  
    5.     public delegate void buildWell();
    6.     public static event buildShrine onBuildWell;
    7.    
    8.     void OnGUI()
    9.     {
    10.        
    11.         if(GUI.Button (new Rect (10, 140, 100, 20), "Build a shrine")){
    12.             onBuildShrine();
    13.         }
    14.  
    15.         if(GUI.Button (new Rect (10, 170, 100, 20), "Build a well")){
    16.             onBuildWell();
    17.         }
    18.        
    19.     }
    20.  

    and a 'borog' script
    Code (csharp):
    1.  
    2.     void OnEnable()
    3.     {
    4.         eventManager.onBuildShrine += shrineBuilt;
    5.                 eventManager.onBuildWell += wellBuilt;
    6.     }
    7.        
    8.     void OnDisable()
    9.     {
    10.         eventManager.onBuildShrine -= shrineBuilt;
    11.                 eventManager.onBuildWell -= wellBuilt;
    12.     }
    13.    
    14.     void shrineBuilt()
    15.     {
    16.         currentMacro += 20;
    17.     }
    18.  
    19.     void wellBuilt()
    20.     {
    21.         currentMacro -= 20;
    22.     }
    23.  
    24.     void OnGUI()
    25.     {
    26.         windowRect = GUI.Window(1, windowRect, DoMyWindow, "Borog the orc");
    27.     }
    28.    
    29.     void DoMyWindow(int windowID)
    30.     {
    31.  
    32.         GUI.Label (new Rect (10, 20, 200, 23), "Name: Borog");
    33.         GUI.Label (new Rect (10, 40, 200, 23), "Borog Macro - " + currentMacro + " / " + maxMacro);
    34.         GUI.Label (new Rect (10, 60, 250, 23), "Dialogue: " + borogText);
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.  
    40.         if (currentMacro < 40)
    41.             borogText = borogsDialogue [0];
    42.  
    43.         if (currentMacro > 39  currentMacro < 61)
    44.             borogText = borogsDialogue [1];
    45.  
    46.         if (currentMacro >= 60)
    47.             borogText = borogsDialogue [2];
    48.     }
    49.  

    and a and a 'justin' script
    Code (csharp):
    1.  
    2.     void OnEnable()
    3.     {
    4.         eventManager.onBuildShrine += shrineBuilt;
    5.                 eventManager.onBuildWell += wellBuilt;
    6.     }
    7.    
    8.     void OnDisable()
    9.     {
    10.         eventManager.onBuildShrine -= shrineBuilt;
    11.                 eventManager.onBuildWell -= wellBuilt;
    12.     }
    13.  
    14.     void shrineBuilt()
    15.     {
    16.         currentMacro -= 20;
    17.     }
    18.  
    19.     void wellBuilt()
    20.     {
    21.         currentMacro += 20;
    22.     }
    23.    
    24.     void OnGUI()
    25.     {
    26.         windowRect = GUI.Window(0, windowRect, DoMyWindow, "Justin the shrew");
    27.     }
    28.    
    29.     void DoMyWindow(int windowID)
    30.     {
    31.        
    32.         GUI.Label (new Rect (10, 20, 200, 23), "Name: Justin");
    33.         GUI.Label (new Rect (10, 40, 200, 23), "Justin Macro - " + currentMacro + " / " + maxMacro);
    34.         GUI.Label (new Rect (10, 60, 250, 23), "Dialogue: " + justinText);
    35.        
    36.     }
    37.    
    38.     void Update()
    39.     {
    40.        
    41.         if (currentMacro < 40)
    42.             justinText = justinDialogue [0];
    43.        
    44.         if (currentMacro > 39  currentMacro < 61)
    45.             justinText = justinDialogue [1];
    46.        
    47.         if (currentMacro >= 60)
    48.             justinText = justinDialogue [2];
    49.     }
    50.  
     
    Last edited: Mar 30, 2014