Unity/C# one function to set any int?

Viewed 529

I've got a small project where I would like to avoid creating a setter for each integer. So I'm thinking if it's even possible.

So I would like to set an amount of
player.setResource(player.money, 100);,
player.setResource(player.coal, 20);,
etc.

I'm not sure first of all if it's possible, and second of all how to write a function itself.

public void setResource(int resource, int amount)
{
        ???
}
5 Answers

Use an enum for this. Define an enum in your project like so

public enum ResourceType { Coal = 0, Money = 1, Health = 2 }

Now, you can add a switch case in your setResource function to check what enum you've passed, and set the corresponding value. This is however assuming all your values are integers. you can make a separate one for floats, or just use floats for everything, upto you.

This will be your new SetResource Function, assuming you have a reference to your player.

public void setResource(ResourceType resource, int amount)
{
    switch(resource)
    {
         case ResourceType.Money:
                 player.money = amount;
                 break;

        case ResourceType.Coal:
                player.coal = amount;
                break;


    }
}

You can use an Enum to define the resources types and a Dictionary to store the values of each resource. Here's an example:

public enum ResourceType
{
    Coal,
    Money
}

private Dictionary<ResourceType, int> _resources = new Dictionary<ResourceType, int>();

public void SetResource(ResourceType resourceType, int value)
{
    _resources[resourceType] = value;
}

public int GetResource(ResourceType resourceType, int defaultValue = 0)
{
    if (_resources.TryGetValue(resourceType, out var value))
        return value;
    else
        return defaultValue;
}
public class Player
{
    public int money { get; set; }
    public int coal { get; set; }

    public void setResource(string resource, int amount)
    {
        this.GetType().GetProperty(resource).SetValue(this, amount);
    }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        Player player = new Player();
        player.setResource(nameof(player.coal), 4);
    }
}

In your method, the target is not passed, the value of the integer is assigned to a local variable resource that only exists within the method.money and coal are not affected.

You need to pass the address of those data.

public void setResource(ref int resource, int amout)
{
    resource = amount;
}

player.setResource(ref player.coal, 100);

If nothing else happens in the method, a get/set property would do the same.

There are multiple ways how you could do this. I often use this aproach for quick prototyping.

First you create two variables for the data you want to assing, then you create a Setter function and with this you can set variables through other scripts.

int value 1;
int value 2;

public void SetData(int value1, int value2)
{
   this.value1 = value1;
   this.value2 = value2;
}

The same aproach can be used for other data types, you just need to create the variables.

Related