- Home /
Add custom declared variables in a Array
So I made a custom class and added several variables to it. My goals is to add all these new variables into a array called 'allDialogs'
class DialogBalloon {
var id : int;
var nextId = new Array(); //next dialog line to go to
var actor : Transform;
var listener : Transform;
var title : String;
var text : String;
var isPlayerQuestion : boolean; //If its a Question that the player can ask(needs to be unlocked to show)
var isStatement : boolean; //just a statement with no answer.
var isUnlocked : boolean; //once unlocked the player can use it as option.
var wasDisplayed : boolean; //if it has already been said
}
var currentDialogDisplay : DialogBalloon;
var welcomeHome00 : DialogBalloon = new DialogBalloon();
var welcomeHome01 : DialogBalloon = new DialogBalloon();
var welcomeHome02 : DialogBalloon = new DialogBalloon();
var welcomeHome03 : DialogBalloon = new DialogBalloon();
var traffic01 : DialogBalloon = new DialogBalloon();
var traffic02 : DialogBalloon = new DialogBalloon();
var dinner01 : DialogBalloon = new DialogBalloon();
var dinner02 : DialogBalloon = new DialogBalloon();
var herDayA01 : DialogBalloon = new DialogBalloon();
var herDayA02 : DialogBalloon = new DialogBalloon();
var herDayB01 : DialogBalloon = new DialogBalloon();
var herDayB02 : DialogBalloon = new DialogBalloon();
var phoneA01 : DialogBalloon = new DialogBalloon();
var phoneA02 : DialogBalloon = new DialogBalloon();
var phoneB01 : DialogBalloon = new DialogBalloon();
var phoneB02 : DialogBalloon = new DialogBalloon();
var allDialogs = new Array(); //Array that contains all the game dialogs
So under Start() I tried to add these to the Array.I found that if I do the long way it works well:
allDialogs.Add(welcomeHome00);
allDialogs.Add(welcomeHome01);
allDialogs.Add(welcomeHome02);
allDialogs.Add(dinner01);
allDialogs.Add(traffic01);
But I would like to get something that is automatic, this is what I tried to write but it just doesn't work...
for (var DialogBalloon in Dialog_Manager){
allDialogs.Add(DialogBalloon);
}
Is it possible to build a loop that will include all these new variables? Hope the question was formulated in a clear way.
Thanks in advance!
//SMALL UPDATE// I forgot to add, all these are being written in a script called 'Dialog_Manager', that is the reason why I added it to the for loop.
And also all these variables have actual values that are declared in the Start() function, here are some of them:
//WelcomeHome
welcomeHome00.id = 1;
welcomeHome00.nextId.Add(welcomeHome01);
welcomeHome00.nextId.Add(welcomeHome02);
welcomeHome00.nextId.Add(welcomeHome03);
welcomeHome00.actor = objManager.actorWife;
welcomeHome00.listener = objManager.actorPlayer;
welcomeHome00.text = "How did your day go?";
welcomeHome01.id = 2;
welcomeHome01.title = "Ok";
welcomeHome01.actor = objManager.actorPlayer;
welcomeHome01.listener = objManager.actorWife;
welcomeHome01.text = "It was ok.";
Do you need to have the vars like herDayA01 and phoneB02 around? or are those just temporary?
you mean the way they were named? They can be called differently, that was just a temporary layout.
Answer by DaveA · Nov 20, 2012 at 11:17 PM
Well, you have 16 of these things, so you could just do:
for (var i = 0; i < 16; i++)
allDialogs.Add (new DialogBalloon());
But you would have to have some constants or something defined to help you know which of those are which, like
allDialogs[Phone02]
Which may be tedious. You could do that with an enum. Probably the most efficient as code goes.
You could also use a HashTable:
var allDialogs : HashTable;
allDialogs = new HashTable();
allDialogs["phone02"] = new DialogBalloon();
That would give you a list you can iterate on, as well as keys you can human-read.
Thanks for the suggestion, going to try this at home this evening! As for the Hashtable, I was actually using a GenericDictionary but realized that for this specific case I don't need it yet (but have a strong feeling I'll need to re-implement it later)
Just realized, the line allDialogs.Add (new DialogBalloon()); wouldnt it just create 16 new emtpy DialogBalloon classes? each one of those variables that I created actually have their own specific values so they are the ones that need to be added. I'm adding the details on the question to make it more clear.
Right. So in this case, yeah, it's a big mess, but you may just need to grunt it out. If it will change a lot, you may consider reading that data from a file. I suppose if there's any wrist-saving, you could do something like:
allDialogs = new Array (welcomeHome00, welcomeHome01, welcomHome02,....
I see. So there is no clean way I could do a for loop in the start() and just pick up all those variables?
Your answer
Follow this Question
Related Questions
Custom Class Array Serialization Problem 0 Answers
C# Adding to an Array 1 Answer
"Null Reference Error" when using a custom class as an array 1 Answer
Error message when accessing class array 1 Answer
class array removeAt 1 Answer