Is there anyway to make a list of prewritten variables? (C#)
Hello, I'm in the process of writing up a script for a mock "Stock Exchange". However, in the game, I need them all to have starting values, so I typed them all out...
public static float Bloxmart_S = 65.63f,KingSnackers_S = 38.27f,HurryNGo_S = 52.14f,FishySmells_S = 12.44f;
public static float McBlox_S = 117.64f,ChickenNFries_S = 68.41f,BlockoTaco_S = 71.89f;
To name a few. I have a function later on that will be used to change the stocks... However, the only way I know how to do this would be literally write out every single stock name by its float variable name to change it, which is both inefficient and I have somewhere close to 80 variables. I couldn't find a way to make a list containing all of these prewitten floats, and I found a few notes on reflection...
float newVar = (float)this.GetType().GetField(StockNames[i]).GetValue(this);
(The StockNames[i] is a list of all the floaters names strings only). However, while that can find the variable, its duplicating it to the new variable, which means the original value stays the same. Is there anyway to actually find the float all contained within this one script by name?
Answer by Masterio · Feb 23, 2016 at 08:44 AM
Read about: Dictionaries, (Arrays and Lists too).
You can define all your data in a Dictionary:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Dictionaries : MonoBehaviour
{
public Dictionary<string, float> list;
void Start()
{
// initialize new dictionary
list = new Dictionary<string, float>();
// add values to the list
//PATTERN: list.Add("key", value);
list.Add("Bloxmart_S", 65.63f);
list.Add("KingSnackers_S", 38.27f);
list.Add("HurryNGo_S", 52.14f);
///...
// get value from the list by key
float value;
list.TryGetValue("HurryNGo_S",out value);
// or
value = list["HurryNGo_S"];
// edit value by key name
list["HurryNGo_S"] = 99f;
// check if key exists
if(list.ContainsKey("Bloxmart_S"))
Debug.Log("Bloxmart_S exists.");
else
Debug.Log("Bloxmart_S not exists.");
// check if value exists
if(list.ContainsValue(65.63f))
Debug.Log("65.63f exists.");
else
Debug.Log("65.63f not exists.");
// remove key and value
list.Remove("KingSnackers_S");
// iterate over the list
foreach(float v in list.Values)
{
Debug.Log(v);
}
// if you need a key and value
foreach(KeyValuePair<string, float> p in list)
{
Debug.Log(p.Key + " - " + p.Value);
}
}
}
Answer by Q-ro · Feb 23, 2016 at 01:14 PM
why not use a dictionary ?
//Add this to the top of your script
using System.Collections.Generic;
//then where you need to create your list (perhaps the player preferences) you define the dictionary as follows:
var stocks = new Dictionary<string, string>();
stocks["stock_name1"] = "value1";
stocks["stock_name2"] = "value2";
Your answer
Follow this Question
Related Questions
Updating a List from a Custom Inspector 0 Answers
Firebase List Users 1 Answer
Get the lowest value from a Gameobject List 0 Answers
How Do I Create a List of Two Strings? C# 1 Answer
List keeps losing items. 0 Answers