Array with boolean variables?

Hello, I am sorry if this is a stupid question, but I am really new to arrays and having a hard time to wrap my head around them.

Anyway, what I wish to do is have an array of 3 items. I have created 3 boolean variables and I wish to somehow be able to call to them from the array. somewhat like so:

var oneVar : boolean;
var twoVar : boolean;
var threeVar : boolean;
var fourVar : boolean;
var myArray : Array[oneVar,twoVar,threeVar];

Then I would want to be able to turn the variables true or false by calling the array, but also be able to switch twoVar with fourVar(for example)…

I am probably going about the wrong way, considering that there is nothing describing what I want( at least of what I could find)…
Any help would be greatly appreciated! :slight_smile:

Thanks in advance

Hi and welcome, CaptainKirby!

So, you are trying to declare an array with three booleans as its elements, called myArray. This is the correct synthax, in unityScript:

var myArray : boolean[] = new boolean[3];

And this is the C# version:

bool[] myArray = new bool[10];

However, I’m not so good with C#, so I’ll continue to explain you the point using Unityscript (that you could probably know as Javascript…)

An array is characterized by its elements. Consider an array as a set of cells, each one containing a particular value: these cells (3, in our case) contain the values (the boolean that you wanna use).

Now, if you wanna retrieve a particular value from the array, you have to access it using the index of its “cell”.

myArray, which we declared above, has 3 elements, and these ones are indexed by indices 0, 1 and 2.

So, if you want to retrieve the second element, you have to read it using myArray[1].

If you want to use it in the future throughout “twoVar” variable, you have to make an assignment: twoVar = myArray[1];

As an end, if you want to exchange the first element with the third, you have to use a temporary variable (as if you have two glasses full of water: to exchange the content of the glasses, you need a third one!)

temp = myArray[0];
myArray[0] = myArray[2];
myArray[2] = temp;

I hope that this simple explanation will be useful to you.

You can also just have all the spells in the array, but where you store the variables for the spell itself, you have a flag for whether the player knows it, and a flag for whether the player has it selected for use.

During the ‘Select spells to use’ function/state, you have an array to use to display and select from. At the start of this phase, you build this second array from the main list, based on the ‘known’ flag’s state. Selecting the spells here does nothing more than toggling the ‘In Use’ flag

Then, when the play game part starts, you have an array of three spell slots that gets populated based on the flags that got set in the selection portion…

So, instead of having to worry about references here and back, and arrays of arrays, you just have three arrays: One main one, one sub-set of that, and another sub-set…

Thank you so much :slight_smile:
However I think my explenation was a little lackluster. I will try again! :slight_smile:

With your aid I have created an array with 3 boolean cells.

I have also made 3 boolean variables.

On function Start I add the 3 variables to the 3 cells of the array.

Later in the code I change the value of one of the cells to true, but it doesn’t change the value of the variable! Is there a way to do this? Or am I just Going at this the wrong way?

This is my code:

public var fireSpell : boolean;
public var frostSpell : boolean;
public var healSpell : boolean;

var eqSpellsArray : boolean[] = new boolean[3];

function Start(){
fireSpell = eqSpellsArray[0];
frostSpell = eqSpellsArray [1];
healSpell = eqSpellsArray[2];
}

function CastSpell(){
eqSpellArray[1] = true;
}