I have variables in a list like so…
bool bool1;
bool bool2;
list boolvariables = new list();
start()
{
boolvariables.add(bool1);
boolvariables.add(bool2);
}
i need to iterate through the list (to make the code efficient and shorter) but how do i assign a new value to them? I believe they are called references in lists so thats why it’s not possible. I was thinking linq might have some useful function that would allow me to do this but I cant find no answer to this on the web. Any idea guys?
for (int i=0;i<boolvariables.Count;i++) {
boolvariables[i] = false;
}
thanks for your reply… that is what I tried but it did not work, I read that variables in a list are called references so I assume they do not update the variable automatically. Am i wrong?
It depends on the type of the variable.
For a list of primitives (bool, int, float, etc) or structs (e.g. Vector3), a list stores values. So if you do this:
Vector3 foo=new Vector3(1,2,3);
someList[2] = foo;
someList[2].y = 3f;
The value of foo is never changed - it’s still (1,2,3). When you assigned someList[2], it made a copy of it.
For a list of class instances (nearly anything else), it will store references. That means that the array and your variable will be pointing to the same spot in memory. If you have a list of bools and want them to be stored as references - so changing the thing it came from will be reflected when you look in the array - you can wrap in up in a class:
class WrappedBool {
public WrappedBool(bool newValue) {
val = newValue;
}
public bool val;
}
....
WrappedBool foo = new WrappedBool(true);
someList.Add(foo);
someList[0].val = false;
Debug.Log("foo is now "+foo.val); //outputs False
OK, I just reread your original post and now I more fully understand what you were going for. This is one of those problems where the kind of thing you’re trying to do suggests that you probably have a problematic design in the first place. Can you give a little more context as to what you’re trying to accomplish?
public bool poleInHand = false;
public bool floorInHand = false;
public bool wallInHand = false;
List<bool> toggles = new List<bool>();
List<KeyCode> keyCodes = new List<KeyCode>();
// Use this for initialization
void Start ()
{
toggles.Add(floorInHand);
toggles.Add(poleInHand);
toggles.Add(wallInHand);
keyCodes.Add(KeyCode.Alpha1);
keyCodes.Add(KeyCode.Alpha2);
keyCodes.Add(KeyCode.Alpha3);
}
// Update is called once per frame
void Update()
{
foreach (KeyCode keycode in keyCodes)
{
if (Input.GetKeyDown(keycode))
{
for (int i = 0; i < toggles.Count; i++)
{
if (toggles[i] && toggles[i] != (toggles[keyCodes.IndexOf(keycode)]))
{
toggles[i] = false;
}
}
toggles[keyCodes.IndexOf(keycode)] = !(toggles[keyCodes.IndexOf(keycode)]);
}
}
floorInHand = toggles[0];
poleInHand = toggles[1];
wallInHand = toggles[2];
}
}
the last three lines of code is what I want to get rid of… I will be adding more toggles so this will just become a bit silly.
I might have 10 toggles in the future so I would like to update the variables with a for loop or something that will make me not have to write ten lines of the same code.
OK, in addition to wanting to use references in array (as opposed to values), you also have two parallel arrays. 99% of the time, if you have parallel arrays, you want to merge them into one array with a class tying your data together instead.
[System.Serializable]
public class KeyToggle {
public KeyCode key;
public bool value;
public void UpdateValue() {
if (Input.GetKeyDown(key) ) {
value = !value;
}
}
}
public List<KeyToggle> toggles;
for (int k=0;k<toggles.Count;k++) {
toggles[k].UpdateValue();
}
(I’m not 100% sure what your code in your loop is supposed to be doing, but I think this is all that’s needed)
You’ll be able to edit this list in the inspector with dropdowns.
If you add a “name” variable to the class, you could (in Start) create a Dictionary<string, KeyToggle>, and add each of these to the dictionary using the name as the key. Then, to access them arbitrarily, you could use toggleDict[“poleInHand”].value, for example. (You wouldn’t have to modify the sample code above; the List and the Dictionary will be pointing to the same KeyToggle objects by reference)
The only reason it’s a List up there and not a Dictionary is to be able to edit it in the inspector. If you’d rather do them all through code, you could skip the List and add a constructor to your class to make it handy to add them in Start:
// in Keytoggle
public KeyToggle(KeyCode key) {
this.key = key;
}
Dictionary<string, KeyToggle> toggles;
// in Start
toggles = new Dictionary<string, KeyToggle>();
toggles.Add("poleInHand", new KeyToggle(KeyCode.Alpha1) );
//etc
//in Update
foreach (var toggleKVP in toggles) {
toggleKVP.Value.UpdateValue();
}
if (toggles["poleInHand"].value) {
//do pole in hand stuff
}