Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by sergio_gn, Aug 21, 2022.

  1. sergio_gn

    sergio_gn

    Joined:
    Aug 30, 2020
    Posts:
    3
    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.

    Code (CSharp):
    1. public void TaskOne()
    2.     {
    3.         PlayerPrefs.SetInt("task1", task1 ? 1 : 1);
    4.         PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
    5.         PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
    6.         PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    7.     }
    8.     public void TaskTwo()
    9.     {
    10.         PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
    11.         PlayerPrefs.SetInt("task2", task2 ? 1 : 1);
    12.         PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
    13.         PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    14.     }
    15.     public void TaskThree()
    16.     {
    17.         PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
    18.         PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
    19.         PlayerPrefs.SetInt("task3", task3 ? 1 : 1);
    20.         PlayerPrefs.SetInt("task4", task4 ? 0 : 0);
    21.     }
    22.     public void TaskFour()
    23.     {
    24.         PlayerPrefs.SetInt("task1", task1 ? 0 : 0);
    25.         PlayerPrefs.SetInt("task2", task2 ? 0 : 0);
    26.         PlayerPrefs.SetInt("task3", task3 ? 0 : 0);
    27.         PlayerPrefs.SetInt("task4", task4 ? 1 : 1);
    28.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    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.
    Code (CSharp):
    1. public void SetTask(int taskNum)
    2. {
    3.   for(int i=1; i<=numberOfTasks; i++)
    4.     PlayerPrefs.SetInt("task" + i, taskNum == i ? 1 : 0);
    5. }
     
    Yoreki likes this.
  3. sergio_gn

    sergio_gn

    Joined:
    Aug 30, 2020
    Posts:
    3
    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:

    Code (CSharp):
    1.     void Task()
    2.     {  
    3.         for (int i = 1; i <= numberOfTasks; i++)
    4.             PlayerPrefs.SetInt("task" + i, taskNum == i ? 1 : 0);
    5.  
    6.         PlayerPrefs.SetInt("IDButton", IDbutton ? 1 : 1);
    7.     }
    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'
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    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
     
  5. sergio_gn

    sergio_gn

    Joined:
    Aug 30, 2020
    Posts:
    3
    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.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You would, if you were converting anything, but you're literally just setting it to always 1, so you could just put 1 instead.