how do i seperate this method with two parameters into two methods so i can use it in ui

heres the code

//The method to call to add to stats
    public void AddToStat(string statName, int amountToAdd)
    {
        var currentValue = GetStatValue(statName);
        int newValue = currentValue + amountToAdd;
        SetStatValue(statName, newValue);
    }

how do i make this into multiple methods so i can actually call this method in a button?

You can add your own script to the GameObject that already has the Button, then just expose public fields in that script, and call this method anytime the button hits.

Let’s say this script is called ButtonAddStatsDriver or something…

It goes a bit like this:

// fields to set up in your script
public string statNameToSend;
public int amountToAdd;

and then this code:

void Start()
{
  UnityEngine.UI.Button b = GetComponent<Button>();

  b.onClick.AddListener( delegate {
      // put any particular "I want this to happen when"
      // the button gets pressed" code here:
      StatsContainerThingy.AddToStat( statNameToSend, amountToAdd);
  });
}

Put this directly on the GameObject with the button, configure the args, then off she goes.

When you do this, you do NOT set up any calls in the Button itself, unless you want other stuff to get called too.

1 Like

is there any way i can do this but in the same script as my addtostat script?

If you set up your AddToStat script so that there is only one per button and it is filled out with the values you want, then yes, but you still need that wrapper to call the multi-argument function.

sry my smol bran is too smol to handle this, could u explain this to me like i am a 5 year old? ;-; sry

when i added this, it didn’t do anything and there wasnt any new listener on the button ;-;

The listener won’t show because it is not handle-able by Unity’s display mechanism. Use Debug.Log() to ensure it is being called. It does work, I use that construct ALL the time, as does every UI engineer at my company.

o yea btw, how do i add stuff to this dictionary?
im stupid

Dictionary<string, int> stats = new Dictionary<string, int>();

bc when i click the button, the health doesn’t go up, i even added debug.logs showing the maxhealth

The same as with any dictionary: Add!

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.add?view=net-5.0#System_Collections_Generic_Dictionary_2_Add__0__1_

Keep adding debugging. What was the value right before the addition? What was the value right after? What was the value you added? Did any of that code even run? Why not? etc. etc. etc. Nobody here can do this for you.

ok so i added all my stats to the dictionary but it still doesn’t work
heres my whole script for playerstats

using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class PlayerStats : MonoBehaviour
{
    [Header("Health and Defense")]
    public int maxHealth;
    private int currentHealth;
    public int startDefense;
    private int currentDefense;

    [Header("Critical Chance and Damage")]
    public int startCritChance;
    [HideInInspector]
    public int currentCritChance;

    public int startCritDamage;
    [HideInInspector]
    public int currentCritDamage;

    [Header("Movement")]
    public int startSpeed;
    [HideInInspector]
    public int currentSpeed;

    [Header("Stats UI")]
    public TMP_Text healthText;

    [Header("References")]
    public Dictionary<string, int> stats = new Dictionary<string, int>();



    // Start is called before the first frame update
    void Start()
    {
        AddStatsToDict();
        currentHealth = maxHealth;
        currentDefense = startDefense;
        currentCritChance = startCritChance;
        currentCritDamage = startCritDamage;
        currentSpeed = startSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        if (currentHealth <= 0)
        {
            Debug.Log("You ded");
        }

        healthText.text = maxHealth.ToString();
        Debug.Log(maxHealth.ToString());
    }

    public void TakeDamage(int damageToTake)
    {
        currentHealth -= damageToTake;
    }

    void AddStatsToDict()
    {
        stats.Add("maxHealth", 1);
        stats.Add("currentDefense", 2);
        stats.Add("currentCritChance", 3);
        stats.Add("currentCritDamage", 4);
        stats.Add("currentSpeed", 5);
    }

    //Retrieve a stat value
    int GetStatValue(string statName)
    {
        if (stats.TryGetValue(statName, out int val))
        {
            return val;
        }

        return 0;
    }

    //Set the value of the stats
    void SetStatValue(string statName, int newValue)
    {
        stats[statName] = newValue;
    }

    //The method to call to add to stats
    public void AddToStat(string statName, int amountToAdd)
    {
        var currentValue = GetStatValue(statName);
        int newValue = currentValue + amountToAdd;
        SetStatValue(statName, newValue);
    }
}

and heres the script on the button

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonAddStatsDriver : MonoBehaviour
{
    public string statNameToSend;
    public int increments;

    public PlayerStats pS;

    // Start is called before the first frame update
    void Start()
    {
        UnityEngine.UI.Button b = GetComponent<Button>();

        b.onClick.AddListener(delegate
        {
            pS.AddToStat(statNameToSend, increments);
            Debug.Log(statNameToSend + " was increased by: " + increments);
        });
    }
}

and heres what i assigned in the inspector 6892311--806103--Screen Shot 2021-03-01 at 8.24.27 PM.png
why doesn’t it work? btw ive never used dictionaries before so its COMPLETELY new to me

@Kurt-Dekker help

In my very first post I’ve shown you all the steps, I’m sorry I cannot reach through the screen and make you do them.

ummmm, the first post u posted in this thread? how do i add it to dictionaries tho ;-;

I’m starting to think perhaps you’re simply not reading my posts, so instead of answering I’m going to tell you to go up and see the link I posted in response to the FIRST time you asked how to add stuff to dictionaries.

Keep in mind this thread started with UI button callback questions and has now migrated into basic data processing questions. If you are struggling to create your own stats system, just STOP NOW because this is not a thing you will create by random combinations.

Stats systems (like inventory systems) are about an 8 or 9 out of 10 as far as complexity and difficulty of common game constructions. They get HAIRY, trust me.

If you want success, start by working through a few tutorials. There are many on Youtube, and like I said, they are NOT a simple thing you can slap together.

6894443--806552--Screen Shot 2021-03-02 at 10.34.05 AM.png