Nothing big, just looking for a more logical way to put my code together. I have 3 resources in my game, trees, rocks, and fish. I want to have it where when a player walks up to a resource I can use 1 function that sends the appropriate “damage” to the node.
I am working on a multiplayer game, so there’s a little extra code I have to put in.
This is what I have so far. The code all works, but it feels like I could be doing it more efficiently:
void CmdGatherResource (NetworkInstanceId netId)
{
GameObject focus = NetworkServer.FindLocalObject (netId);
GatherTree resourceTree = focus.GetComponent<GatherTree>();
GatherRock resourceRock = focus.GetComponent<GatherRock>();
GatherFish resourceFish = focus.GetComponent<GatherFish>();
if (focus)
{
PlayerStats playerStats = GetComponent <PlayerStats>();
if (resourceTree != null)
{
resourceTree.Gather (playerStats.woodCutDmg);
}
if (resourceRock != null)
{
resourceRock.Gather (playerStats.rockMineDmg);
}
if (resourceFish != null)
{
resourceFish.Gather (playerStats.fishCatchDmg);
}
}
}
Basically I just want to know if after I set the GameObject focus if I can check what type of resource it is. Is there a way to set a bool or string on the resource code that I can check for? If the bool or string is set to 1 of the 3 resource types, then I can use the specific code necessary. I realize I could use tags, but the tag is already used for other code so I need an alternative.