- Home /
Best way to assign a bunch of variables?
I'm making a game with a lot of abilities and the player can assign each one to a direction (up, down, left right). So in my abilities script I have variables for each ability's cool down time and effect. So instead of just assigning each of these manually like this:
if (equippedTopAbility == "lightning")
{
topAbilityCooldownTime = lightningCooldownTime;
topReadyEffect = lightningReadyEffect;
}
if (equippedRightAbility == "lightning")
{
rightAbilityCooldownTime = lightningCooldownTime;
rightReadyEffect = lightningReadyEffect;
}
if (equippedBottomAbility == "lightning")
{
bottomAbilityCooldownTime = lightningCooldownTime;
bottomReadyEffect = lightningReadyEffect;
}
if (equippedLeftAbility == "lightning")
{
leftAbilityCooldownTime = lightningCooldownTime;
leftReadyEffect = lightningReadyEffect;
}
I would like to create a function with variables that I determine earlier. So for example the function looks like: public void AssignVariables(string direction, float CooldownTime, GameObject ReadyEffect)
and I want to assign topAbilityCooldownTime and topReadyEffect.
So my question is whether or not there is a way to assign variables like the following
direction + "AbilityCooldownTime" = CooldownTime;
direction + "ReadyEffect" = ReadyEffect;
I know that this doesn't work but was wondering if there was an easy way to do this. (The variables I want to assign are topAbilityCoolDownTime and topReadyEffect)
Edit: Thanks for the help everybody! I got it all figured out!
You could use structs in combination with the variables you need. For example enum, float, int, bool, Gameobjects, Vector3 etc. That way, it's easier to sort variables for specific needs.
public struct myStruct {
float myFloat;
Int myInt;
myEnum;
bool myBool;
} ;
enum myEnum {
first Name, second Name.... million Name etc
}
myStruct = new myStruct() ;
Answer by Roger_0123 · Sep 11, 2020 at 07:52 AM
Hi @Cryno1000, you may use a C# Dictionary:
public Dictionary<string, float> cooldownTimes = new Dictionary<string, float>();
public Dictionary<string, GameObject> readyEffects = new Dictionary<string, GameObject>();
//....
public void AssignVariables(string direction, float cooldownTime, GameObject readyEffect){
this.cooldownTimes[direction + "AbilityCooldownTime"] = cooldownTime;
this.readyEffects[direction + "ReadyEffect"] = readyEffect;
}
Check the C# Documentation for info on how to use Dictionaries
Hope this helps! (PS: sorry for code formatting)
I fixed the formatting for you, it's not that difficult.
Scroll to the "Posting Questions, Answers and Comments" code formatting section.
Answer by ShadyProductions · Sep 11, 2020 at 10:49 AM
Like few have mentioned, you could use enums and dictionaries to do it.
I suggest keeping your ability stuff in a nice seperate class.
public enum Direction
{
Left,
Right,
Up,
Down
}
public class Abilities
{
public readonly Dictionary<Direction, float> Cooldowns;
public Abilities()
{
Cooldowns = new Dictionary<Direction, float>();
foreach (var direction in (Direction[])Enum.GetValues(typeof(Direction)))
Cooldowns.Add(direction, 0f);
}
}
When defined you can do something like
Abilities abilities = new Abilities();
abilities.Cooldowns[Direction.Left] = 5f;
var cooldown = abilities.Cooldowns[Direction.Left];
Your answer
Follow this Question
Related Questions
I'm unable to clear a variable that is used in multiple scripts. 0 Answers
Statics variables in structs for jobs system 0 Answers
Float randomly being set to 0 spontaneously 1 Answer
AudioClip stuttering while loop is OFF and not using function Update? 1 Answer
How to READ a variable VALUE from other Object Script? 1 Answer