So I have a small “shop” that allows the player to increase or decrease their workforce. I am trying to use a combination of buttons to enable the player to do so, but I don’t want to write multiple functions when one will suffice.
This is, essentially what I am trying to simplify:
public void AddToProperty1()
{
player.properties.Property1++;
}
public void AddToProperty2()
{
player.properties.Property2++;
}
// and so on...
Ideally, it would look something like…
public void AddToProperty(string property)
{
player.properties.property++;
}
But this code doesn’t work, something is missing or this is not how I communicate to the computer what I am trying to do. Is it possible to simplify the function in this way?
int property1;
int property2;
public void AddToProperty(string property)
{
switch (property)
{
case "property1":
property1++;
break;
case "property2":
property2++;
break;
}
}
You could also overload ‘AddToProperty’ so whenever you call it, it does slightly different things based on parameters and return type. You could pass in a reference to your property, so you’d always be changing the one currently passed in.
public void AddToProperty(ref float _floatProperty)
{
_floatProperty++;
}
public void AddToProperty(ref int _intProperty)
{
_intProperty++;
}
//etc.
Haven’t tested this, so it might not work exactly how I imagine, but I do think it’s a viable idea.