- Home /
Cast Static variables as generic ones?
I am making a game with upgrades, I have class call "GlobalStats" which contains the static variables I need to reference.
I have made another class "MenuUpgrade" that needs to reference these variables, but I want to define which variables are referenced in the editor and not in code, this is so I don't have to duplicate everything 50+ times, and hence the class has to be use generic variables without references to particular static variables.
How can I cast a generic variable in my non-static class as a static variable?
public class GlobalStats : MonoBehaviour
{
// Static Variable
public static int maxSpeedLevel = 0;
public static float playerMaxSpeed = 12.5f;
}
public class MenuUpgrade : MonoBehaviour
{
// Generic Variables
public string upgradeLevel;
public string upgradeStat;
}
often times especially if your newer it's better to say what your trying to accomplish broadly ins$$anonymous$$d of what you have now for example.
i'm not even entirely sure what your trying to do but and i'm not trying to rude, i'm almost sure what you think is the correct way to solve the problem through static variables and such is simply the wrong way from a simplicity point of view to solve it.
if for example you say I want when I increase upgrade level for it to increase a bunch of different variables but i'm not sure which and not sure by how much right now so i'd like that to be dynamic or i'd like to define that in the editor. (is this what you want?)
that is your problem, you might suggest how you think you'd do it but I wouldn't settle on your solution as the definitive best one and ins$$anonymous$$d describe what behavior you want to end up with and then ask for input on how to achieve it.
I'm not sure what you're asking, generic and static variables are 2 unrelated things:
Static variables: https://msdn.microsoft.com/en-us//library/98f28cdx.aspx
Generics: https://msdn.microsoft.com/en-us/library/512aeb7t.aspx
Then, "cast" also means something not related to your question: https://msdn.microsoft.com/en-us/library/ms173105.aspx
You can cast something to change it's "static" nature, you cast the value.
You're using the wrong words to make your question, it makes no sense as it is right now, try to use the actual words for the things you have and the things you want to do.
Sorry for the poorly phrased question, but I did find a solution, although it was more of a hack than the original solution I intended to find.
Answer by MrDizzle26 · Mar 01, 2015 at 11:54 PM
To get around this problem I created new variables in which to store the value of the static ones.
int iUpgradeLevel = 0;
float iUpgradeStat = 0;
int iUpgradePrice = 0;
Rather than try and reference the "GlobalStats" static variables as strings in my code, I entered a boolean check so that I could toggle in the inspector which set of statics to reference.
public bool bMaxSpeed;
public bool bAcceleration;
public bool bBoostSpeed;
Depending on which bool is checked determines when values are passed into my upgrade menu slot, and which values will be altered by the function.
if(bMaxSpeed == true)
{
iUpgradeLevel = GlobalStats.maxSpeedLevel;
iUpgradeStat = GlobalStats.maxSpeed;
iUpgradePrice = GlobalStats.maxSpeedPrice;
}
if(bAcceleration == true)
{
iUpgradeLevel = GlobalStats.accelerationLevel;
iUpgradeStat = GlobalStats.acceleration;
iUpgradePrice = GlobalStats.accelerationPrice;
}
if(bBoostSpeed == true)
{
iUpgradeLevel = GlobalStats.boostSpeedLevel;
iUpgradeStat = GlobalStats.boostSpeed;
iUpgradePrice = GlobalStats.boostSpeedPrice;
}
These values can then be used as part of a generic upgrade system.
// Deny Upgrade!
if(GlobalStats.cash < iUpgradePrice || iUpgradeLevel == 5)
{
audio.PlayOneShot(deny, 1f);
}
// Buy Upgrade!
if(GlobalStats.cash >= iUpgradePrice && iUpgradeLevel < 5)
{
iUpgradeStat += statAmount;
GlobalStats.cash -= iUpgradePrice;
iUpgradePrice *= 2;
iUpgradeLevel += 1;
bSwitchSprite = true;
audio.PlayOneShot(click, 1f);
audio.PlayOneShot(drill, 1f);
}
if(iUpgradeLevel < 5)
{
currentText.text = currentString + ": " + iUpgradeStat.ToString();
costText.text = iUpgradePrice.ToString();
}
if(iUpgradeLevel >= 5)
{
costText.text = "MAX";
currentText.text = currentString + ": " + iUpgradeStat.ToString();
descriptionText.text = "Fully Upgraded";
}
Once an upgrade has been purchased I then make sure to change the value of the original static to represent there new values.
if(bMaxSpeed == true)
{
GlobalStats.maxSpeedLevel = iUpgradeLevel;
GlobalStats.maxSpeed = iUpgradeStat;
GlobalStats.maxSpeedPrice = iUpgradePrice;
statAmount = 1.25f;
}
if(bAcceleration == true)
{
GlobalStats.accelerationLevel = iUpgradeLevel;
GlobalStats.acceleration = iUpgradeStat;
GlobalStats.accelerationPrice = iUpgradePrice;
statAmount = 0.005f;
}
if(bBoostSpeed == true)
{
GlobalStats.boostSpeedLevel = iUpgradeLevel;
GlobalStats.boostSpeed = iUpgradeStat;
GlobalStats.boostSpeedPrice = iUpgradePrice;
statAmount = 5;
}
This provided me with the solution I was looking for, but require me to use these boolean checks rather than pass in the name of the static variable to use. I'm sorry for the poor phrased question but I hope this helps make sense of what I was asking.
Your answer
Follow this Question
Related Questions
C# dynamic typing 3 Answers
C# Casting Compile Error 1 Answer
Accessing properties from variable 1 Answer
NullReference Problem with pragma strict 1 Answer
Classes and type casting? 2 Answers