Search Unity

Detect which button was pressed

Discussion in 'Scripting' started by glgamesforfun, Jan 16, 2022.

  1. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hi, i use PlayerPrefs to save usernames. How can i detect on which username i clicked on and then start a game with this username (I just need to know which username i clicked on).
    Thanks!
    Code (CSharp):
    1.     [SerializeField] private int itemsToGenerate;
    2.     public UsernamesExample usernamesExample;
    3.  
    4.     UsernamesExample MakeFreshCopyOfExampleTile()
    5.     {
    6.         var copy = Instantiate<UsernamesExample>(usernamesExample, usernamesExample.transform.parent);
    7.  
    8.         copy.gameObject.SetActive(true);
    9.  
    10.         return copy;
    11.     }
    12.  
    13.     void Start()
    14.     {
    15.         usernamesExample.gameObject.SetActive(false);
    16.  
    17.         itemsToGenerate = PlayerPrefs.GetInt("USERNAME_COUNT");
    18.  
    19.         for (int i = 0; i < itemsToGenerate; i++)
    20.         {
    21.             string player = PlayerPrefs.GetString("USERNAME" + i);
    22.             int value = PlayerPrefs.GetInt("HIGHSCORE" + i);
    23.             var entry = MakeFreshCopyOfExampleTile();
    24.             entry.SetText(player);
    25.             entry.SetText2(value.ToString());
    26.         }
    27.  
    28.         Debug.Log(string.Format("I found {0} sprites.", itemsToGenerate));
    29.         //Array.Sort(, (a, b) => { return a.name.CompareTo(b.name); });
    30.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
  3. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Yeah, i saw it and it helped a lot, but i need to detect, which PlayerPrefs it is.
    Code (CSharp):
    1.             string player = PlayerPrefs.GetString("USERNAME" + i);
    2.             int value = PlayerPrefs.GetInt("HIGHSCORE" + i);
    E.g. So i think i need to detect it and put it instead of the i.
    Then if i start the game, i want to update the Highscore the player is playing on. How can i make this? I'm sorry if I don't see it.
    Thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You want to get away from splattering these playerprefs calls all over your game and wrap it up somewhere centralized, even if it is just in a single static class.

    That way you would have functions that could tell you how many saves there are, that each save name is, etc.

    This lets you break apart the user interface dreck from what it actually means.

    Then you would just bind a function that says (for instance) "Select entry 3"

    The buttons would know NOTHING about what even entry 3 means. That's the point of a delegate. You're just saying "poke button 3" and another function (the delegate) says "load savegame number 3 and go"
     
    glgamesforfun likes this.
  5. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Sorry, I don't really understand. I don't know, how i can click on a username and the start the game. If i win the game, the Highscore should go up (just for the username i selected).
    This is what i have atm:
    Code (CSharp):
    1.         itemsToGenerate = PlayerPrefs.GetInt("USERNAME_COUNT");
    2.  
    3.         for (int i = 0; i < itemsToGenerate; i++)
    4.         {
    5.             string player = PlayerPrefs.GetString("USERNAME" + i);
    6.             int value = PlayerPrefs.GetInt("HIGHSCORE" + i);
    7.             var entry = MakeFreshCopyOfExampleTile();
    8.             entry.SetText(player);
    9.             entry.SetText2(value.ToString());
    10.             // set up what each button does when pressed
    11.             {
    12.                 entry.SetButtonDelegate(
    13.                     () =>
    14.                     {
    15.                     //Debug.Log("1"));
    16.                 }
    17.                 );
    18.             }
    19.         }
    20.  
    21.         Debug.Log(string.Format("I found {0} sprites.", itemsToGenerate));
    22.         // alpha sort by name
    23.         //Array.Sort(, (a, b) => { return a.name.CompareTo(b.name); });
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You have to think of the UI subsystem that you create as being just to get a number from 0 to N-1

    That UI system has these inputs:

    - there are N users
    - here is each of their data (name and high score)
    - go display it all for selection

    On the outside, when the player selects item #3, all that UI side does is say

    - player selected item 3

    The UI is torn down and that resulting "3" is passed to your game start to choose what you want.
     
  7. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hi, i'm so sorry, but i don't understand. I understand what you want to tell me, but i can't use it on my code. Can you maybe give me an example based on my code?
    Should i put the code i want in between this breaket?
    Code (CSharp):
    1.                 entry.SetButtonDelegate(
    2.                     () =>
    3.                     {
    4.                  
    5.                 }
    6.                 );
    Thanks!
     
  8. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    I put Debug.Log(player); in between the bracket and now i see on which player i clicked.
    And now i want to switch the scene and play the game. After it, the Highscore of this player should change. How can i make this?
    (Sorry, i think i have a Burnout atm)