- Home /
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! :)
Thanks in advance
Answer by BiG · Jan 27, 2012 at 04:00 PM
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.
Answer by Tasarran · Jan 28, 2012 at 06:29 PM
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...
Hey Tasarran, thanks for your answer! I am not 100% clear on how to store these flags(boolean variables i assume) with each spell... I am still a bit falky on the ins and outs of array I guess... I know this is probably frowned upon, but could you give an example of how to create an array of these spells which all have this "know" flags? I think it would help greatly in my understanding :)
Thanks!
I need to get a bit more information first: what actually makes the spell?
I mean, somewhere, you have some sort of object or script that has variables for each spell, right?
That would be where I would put the "$$anonymous$$nown" and "In Use" flags.
Or do the spells just exist as items on a list?
Woops added as answer isn$$anonymous$$d of comment -.-
"Well, the reason for creating this question originally was also to sort of ascertain how to set up my spells...
But ideally I think what would fit the mold best would be to simply have each spell be a prefab with a script attatched to them with some variables, defining the damage and some other varying things(spell dependant).
So I would simply put the flags in there and put the...objects into the array?"
Yes, what I had in $$anonymous$$d was some sort of prefab or script for each spell.
Each one would have a boolean variable called "$$anonymous$$nown" and one called "In Use"
Your selection part would just loop through them all and display only the $$anonymous$$nown ones; then when you select from those, it will set the In Use flag.
Then, when you start the game, you just use the three that are tagged.
You could build two arrays from the original array, or you could just loop through and grab them as needed. Unless you have 5000 spells, you won't even notice the time it takes to loop through.
Hey Tasarran, I don't know if you have time to look at my script but if you do, it would be great!
I'm having 2 specific issues:
Items only get registered into the array if they exist in the scene, not as prefabs, is there a way around this?
I wish to arrange each known spell next to each other with, say, 10 pixels width. However with my current scrip, it does it with only with each known spell... if that makes sense?
here's my script:
function Start(){
var allSpellsWithTag : GameObject[];
allSpellsWithTag = GameObject.FindGameObjectsWithTag("Spell");
for(var spells in allSpellsWithTag){
Debug.Log(spells.GetComponent(SpellFlags).inUseSlot);
if(spells.GetComponent(SpellFlags).known == true){
for (var i : int = 0;i < 2; i++) {
Instantiate (spells, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}
//var spellTrain : GameObject = Instantiate(spells,transform.position, transform.rotation);
}
}
}
Answer by CaptainKirby · Jan 27, 2012 at 04:59 PM
Thank you so much :) However I think my explenation was a little lackluster. I will try again! :)
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;
}
Yes, you have just missed a line:
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;
frostSpell = eqSpellArray[1];
}
It's the last line of CastSpell(), in which you assign the element of myArray, indexed by "1", to frostSpell! I think that, beyond the arrays, you know the basics of program$$anonymous$$g, so I let you notice that an array is a variable, exactly as fireSpell, frostSpell and healSpell are variables too! An array is just a special kind of variable, that has to be handled a little differently, not nothing more.
By the way: what are you doing? A RPG? If so, I think that it will become a large game, so my advice is to NOT use the code that you've reported: it will became a mess, and you're on the way to understand anything of your code on the long run! Arrays, in fact, are made to avoid the use of multiple variables, but...You are making just that: using a variable to replace every entry of the array!
...CONTINUE: In your shoes, I will playing around with that code just to understand arrays better. Don't get into a gargantuan game without understand well the logic that you've chosen, or the code will be unmanageable on the long period.
Or, if you have just 3 kind of spells, and you wanna work in your code with significant names (like fire, frost and heal, as you're doing), don't use array at all: just make assignments to these variables.
Thank you so much for your insight! I am making an rpg of sorts yes.
What my goal is:
To have 3 spell buttons which can have any of multple spells that the player has available to them, but only be able to use 3 which the player choses(for example fire, frost and heal).
If I may ask, how would you contruct this scenario? I envisioned having an array for all spells in the game, then one for the spells the player has acces to, and then finally an array for the 3 spells, which can be changed by the player...
Would it be easier(if possible) for me to just have on big array with all the spells and somehow just use 3 of them, that the player selects?
Anyway I guess this a different question completely so I understand if you don't have time to answer it! :)
Thanks!
Watch out. Your idea is a legitimate one (and that's the one that I personally would follow); however you are going to declare arrays of arrays, this way, and you could going crazy (I have an experience on this, trust me!) Considering about using Lists, for this purpose: they are more practicals (just search for them, in the documentation, to have a better explanation).
Beyond that, I'm not feeling so sure about giving you more tips: I don't know many things about your project, such the organization of scenes, the transactions among them...
If you like to have some advices, open a new issue (on the Unity Forum, not here at Unity Answers, because it won't be a "technical" question!) - I think that you'll find people that would be happy in sharing their ideas (and, maybe, that are practical in RPG's creation, unlike me ;) ).
Hehe thanks you very much ! I will definately check out lists. Thanks!