Search Unity

Question Advanced Game Controller System

Discussion in 'Scripting' started by natmaxex, Jul 12, 2020.

  1. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I'm not sure if this is advanced, but i had this idea for a game controller that allows you to swap between sub-level mechanics without changing the main systems script. And I'm not sure how to go about doing this.
    Example:
    Main Game Mechanics Script:
    Button
    Light
    Door
    Code (CSharp):
    1. public class mainScript : MonoBehaviour
    2. {
    3.     public MyOtherScript myOtherScript;
    4.     public Light myLight;
    5.     public GameObject myDoorObj;
    6.     float playerHealth = 10.0f;
    7.     int playerScore = 0;
    8.     string playerName = "name";
    9.     public void Start()
    10.     {
    11.         myLight.color = Color.black;
    12.     }
    13.     public void OnTriggerEnter(Collider other)
    14.     {
    15.         if (other.gameObject.tag == "button") {
    16.             myOtherScript.ButtonPressed();
    17.         }
    18.     }
    19.     public void OtherPosableControls() {
    20.         //No other controls at this time
    21.     }
    22. }
    Sub Game Mechanics Script 1 Actions: (Level 1)
    Press button once only
    The light turns on only
    Door Opens only
    Code (CSharp):
    1. public class subScriptOne : MonoBehaviour
    2. {
    3.     public myParentScript mainScriptParent;
    4.     public void ButtonPressed() {
    5.         mainScriptParent.myLight.color = Color.white;
    6.         mainScriptParent.myDoorObj.SetActive(false);
    7.     }
    8. }
    Sub Game Mechanics Script 2 Actions: (Level 2)
    Press button 2 times
    Light turns on
    Press button
    Light blinks
    Door Opens
    Code (CSharp):
    1. public class subScriptTwo : MonoBehaviour
    2. {
    3.     public myParentScript mainScriptParent;
    4.     int buttonCount;
    5.     bool blinkFastIO;
    6.     public void ButtonPressed() {
    7.         if (buttonCount == 2) {
    8.             mainScriptParent.myLight.color = Color.blue;
    9.         }
    10.  
    11.         buttonCount += 1;
    12.     }
    13.  
    14.     public void Update()
    15.     {
    16.         if (buttonCount >= 3)
    17.         {
    18.             if (blinkFastIO)
    19.             {
    20.                 mainScriptParent.myLight.color = Color.green;
    21.             }
    22.             else
    23.             {
    24.                 mainScriptParent.myLight.color = Color.red;
    25.             }
    26.             blinkFastIO = !blinkFastIO;
    27.             mainScriptParent.myDoorObj.SetActive(false);
    28.         }
    29.     }
    30. }
    Sub Game Mechanics Script 3 Actions: (Level 3)
    Door Opens and closes
    Pressing button will either open or close door and light
    Door Opens
    Code (CSharp):
    1. public class subScriptThree : MonoBehaviour
    2. {
    3.     public myParentScript mainScriptParent;
    4.     bool myButtonState;
    5.     public void ButtonPressed() {
    6.         if (myButtonState)
    7.         {
    8.             mainScriptParent.myLight.color = Color.white;
    9.             mainScriptParent.myDoorObj.SetActive(true);
    10.         }
    11.         else {
    12.             mainScriptParent.myLight.color = Color.black;
    13.             mainScriptParent.myDoorObj.SetActive(false);
    14.         }
    15.     }
    16. }
    Idk I think it would be nice to be able to have the main script that you can swap game mechanics for each level.
     
    Last edited: Jul 12, 2020
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    So, from what I understand, you want to have some control over what a simple button press does? So, for level 1, if you press the button once, those actions get executed, in level 2 you have to press it twice and in level 3 it does something completely different at a different moment.

    I've worked on something similar and it can be done with a ton of work, the question is, do you really need it?
    Think about all of these as certain actions. A button press is an action, the light turning on and off is an action, an animation playing somewhere is an action, etc. What you could do, is instead of writing a script per level (or in this context, per list of actions), you could write a script per action (so, pressing the button would be one action). That action would be complete once a certain condition is met (so, pressing the button X times would set the isDone to true) in which case you want to move onto the next action in the list. By doing this, you don't have to write a single script of this type ever again. You just list the actions in the order you want them to be executed and set their parameters manually.

    Code (CSharp):
    1. public class Action
    2. {
    3.     public bool isDone;
    4.  
    5.     public void DoExecute()
    6.     {
    7.         // Additional code if needed
    8.         Execute();
    9.     }
    10.  
    11.     protected virtual void Execute() { }
    12. }
    13.  
    14. public class ButtonPress : Action
    15. {
    16.     [SerializeField] private int pressesRequired = 1;
    17.  
    18.     private int playerPressed = 0;
    19.  
    20.     protected override void Execute()
    21.     {
    22.         playerPressed++;
    23.      
    24.         isDone = playerPressed == pressesRequired;
    25.     }
    26. }
    You would also need some type of controller that controls which action is being worked on at the moment etc.
    It's a ton (srsly, a ton) of work to make this work and think really hard if you need it. It will save you hours or days in the long run, but in the short run, it's a waste of time to bother with it.
     
    natmaxex likes this.
  3. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I figured as much, thanks for the answer. I was hope i wouldn't half to put everything in the same script.