Advanced Game Controller System

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

public class mainScript : MonoBehaviour
{
    public MyOtherScript myOtherScript;
    public Light myLight;
    public GameObject myDoorObj;
    float playerHealth = 10.0f;
    int playerScore = 0;
    string playerName = "name";
    public void Start()
    {
        myLight.color = Color.black;
    }
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "button") {
            myOtherScript.ButtonPressed();
        }
    }
    public void OtherPosableControls() {
        //No other controls at this time
    }
}

Sub Game Mechanics Script 1 Actions: (Level 1)
Press button once only
The light turns on only
Door Opens only

public class subScriptOne : MonoBehaviour
{
    public myParentScript mainScriptParent;
    public void ButtonPressed() {
        mainScriptParent.myLight.color = Color.white;
        mainScriptParent.myDoorObj.SetActive(false);
    }
}

Sub Game Mechanics Script 2 Actions: (Level 2)
Press button 2 times
Light turns on
Press button
Light blinks
Door Opens

public class subScriptTwo : MonoBehaviour
{
    public myParentScript mainScriptParent;
    int buttonCount;
    bool blinkFastIO;
    public void ButtonPressed() {
        if (buttonCount == 2) {
            mainScriptParent.myLight.color = Color.blue;
        }

        buttonCount += 1;
    }

    public void Update()
    {
        if (buttonCount >= 3)
        {
            if (blinkFastIO)
            {
                mainScriptParent.myLight.color = Color.green;
            }
            else
            {
                mainScriptParent.myLight.color = Color.red;
            }
            blinkFastIO = !blinkFastIO;
            mainScriptParent.myDoorObj.SetActive(false);
        }
    }
}

Sub Game Mechanics Script 3 Actions: (Level 3)
Door Opens and closes
Pressing button will either open or close door and light
Door Opens

public class subScriptThree : MonoBehaviour
{
    public myParentScript mainScriptParent;
    bool myButtonState;
    public void ButtonPressed() {
        if (myButtonState)
        {
            mainScriptParent.myLight.color = Color.white;
            mainScriptParent.myDoorObj.SetActive(true);
        }
        else {
            mainScriptParent.myLight.color = Color.black;
            mainScriptParent.myDoorObj.SetActive(false);
        }
    }
}

Idk I think it would be nice to be able to have the main script that you can swap game mechanics for each level.

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.

public class Action
{
    public bool isDone;

    public void DoExecute()
    {
        // Additional code if needed
        Execute();
    }
 
    protected virtual void Execute() { }
}

public class ButtonPress : Action
{
    [SerializeField] private int pressesRequired = 1;
 
    private int playerPressed = 0;
 
    protected override void Execute()
    {
        playerPressed++;
     
        isDone = playerPressed == pressesRequired;
    }
}

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.

1 Like

I figured as much, thanks for the answer. I was hope i wouldn’t half to put everything in the same script.