- Home /
Having a reference to only one variable.
This might be a very confusing question, so i'll try to explain it well.
I have a game, where right now i am making it so that i can deposit pickaxes, axes, fishing rods and all kinds of things to gather resources automatically. So, in the "DepositTool" script i need to actually set the amount of tools i want to be in a certain place, which is where i have the problem. As i have tons of different tools, i want them to be set automatically. (What i mean by that is that i otherwise would need tons of switch case statements which would be really annoying after making more tools).
I know i also could make it an array, but that becomes really confusing because i have to refer to them as indexes not the actual name of the tool
Is there a way to either have an array where i refer to the objects as their actual names or a way to reference only a single variable to change it.
Here is my code if that helps public static ToolSlot pickaxe; public static ToolSlot axe; public static ToolSlot rod; public static ToolSlot net; public static ToolSlot pot; public static ToolSlot weapon;
The ToolSlot is just the tool and how many tools i have.
void Set(int num){
//PlayerStats.selectedtool.count = num;
}
That selectedTool is the thing i'm going for, which is why it's commented out, because it does not exist.
Answer by Lenny991 · Nov 23, 2021 at 10:42 AM
Hey, if anyone was looking for the answer,
switch(tool.type){
case Tool.toolType.pickaxe:
i = 0;
break;
case Tool.toolType.axe:
i = 1;
break;
case Tool.toolType.net:
i = 2;
break;
case Tool.toolType.rod:
i = 3;
break;
case Tool.toolType.pot:
i = 4;
break;
case Tool.toolType.weapon:
i = 5;
break;
}
that is as efficient as i can think to make it.
IF ANYONE HAS A BETTER SUGGESTION PLEASE TELL IT!!!
Answer by comiljou · Nov 23, 2021 at 10:57 AM
You probably just need to use dictionary something like
private Dictionary<ToolType, int> _toolCount = new Dictionary<ToolType, int>();
void SetToolCount(ToolType tool, int count)
{
_toolCount[tool] = count;
}
Haven't tested it yet, i have never heard of a dictionary before. This looks like what i'm looking for, thanks a ton :)