Repetitive pattern, what is the best approach to solve this?

Hello Everyone!
I have a repetitive pattern in my functions. It’s working, but I know that is a terrible approach.
The thing is: I have 4 buttons, each one is enabled by a boolean in a separated script. When I press 1 of them, I need to disable the other 3.

public void TaskOne()
    {
        PlayerPrefs.SetInt("task1", task1 ? 1 : 1);
        PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
        PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
        PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    }
    public void TaskTwo()
    {
        PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
        PlayerPrefs.SetInt("task2", task2 ? 1 : 1);
        PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
        PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    }
    public void TaskThree()
    {
        PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
        PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
        PlayerPrefs.SetInt("task3", task3 ? 1 : 1);
        PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    }
    public void TaskFour()
    {
        PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
        PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
        PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
        PlayerPrefs.SetInt("task4", task4 ? 1 : 1);
    }

I don’t really know why you used the ? operator, it didn’t seem to be doing anything for you.

You could just for loop through your tasks, something like this maybe.

public void SetTask(int taskNum)
{
  for(int i=1; i<=numberOfTasks; i++)
    PlayerPrefs.SetInt("task" + i, taskNum == i ? 1 : 0);
}
1 Like

Thank you! I see how I can use a loop to disable the other 3 Buttons.
But I’ll still need 4 methods to ‘turn on’ the clicked task.
I wonder if theres a way to get the ‘id’ of the button, to reach something like this:

    void Task()
    {  
        for (int i = 1; i <= numberOfTasks; i++)
            PlayerPrefs.SetInt("task" + i, taskNum == i ? 1 : 0);

        PlayerPrefs.SetInt("IDButton", IDbutton ? 1 : 1);
    }

which the loop would disable all buttons, and the single command below would enable just the clicked button.

Or even better, if the loop could make an exception, and dont change the current bool state of the ‘IDButton’

I don’t quite understand the problem you’re having with my code, it already sets the int of the correct task to 1 given the parameter of the method, and turns the rest to 0. If you’re using a button, you can set the onClick event to call this method, and it will allow you to insert a value for the parameter.

You also used the ? operator again in the code, but are still using it incorrectly
IDbutton ? 1 : 1
is the same thing as
1

Yep. Working like a charm. Thank you!
I didn’t get it at first that I could insert the parameter in the inspector.

I’m was using the ? operator because I thought that I needed him to convert a Bool to an Int with PlayerPrefs.

You would, if you were converting anything, but you’re literally just setting it to always 1, so you could just put 1 instead.