I have a struct used to store 4 integers that are used to represent resources in my game. I have a gameobject that harvests the resources and sends them to be stored in the struct. I want to create a price struct to store values to be used as a price to create buildings, units, etc.
How would I compare the two different struct values? Is it as simple as this;
If (structRESOURCES.wood >= structPRICE.wood && structRESOURCES.food >= structPRICE.food etc…)
{
blah
}
Or is there a way to JUST use the one struct that stores those values and set a price on each gameobject that would require those integers to be a certain amount to be able to create the gameobject?
I think you are asking how to be sure you have enough of the four resources to build your building, unit or whatever.
One handy way is to create something called a “recipe” for each item you can build.
The recipe for a house might say wood = 4, metal = 1, water = 0, magic = 0 for instance.
The class you are using to actually store what a player has could then have two methods:
bool DoWeHaveEnoughForThisRecipe( Recipe recipe);
that returns true or false.
And then another one to actually “consume” the resources.
void ConsumeResourcesForRecipe( Recipe recipe);
That way when you go to build one you just call the first method to see if you can afford it. This can trivially be called for things like UI to make buttons grayed out for stuff you can’t afford yet.
You could even make a function that tells you what you still need before you can afford it.
string WhatResourcesDoIStillNeedToBuildThisRecipe( Recipe recipe);
If down the line you decide “hey, I want to add or remove a resource,” all the changes are centralized to just a few classes.
1 Like