I made a custom class called DialogBallon that contains a bunch of variables:
class DialogBalloon {
var id : int;
var nextId = new Array(); //possible answers
var actor : Transform;
var listener : Transform;
var title : String;
var text : String;
var waitingText : String;
var isPlayerQuestion : boolean; //If its a Question that the player can ask(needs to be unlocked to show)
var isActorQuestion : boolean; //If its a question that a Npc can ask.
var isLastLine : boolean; //Last line of a dialog.
var isUnlocked : boolean; //once unlocked the player can use it as option.
var wasDisplayed : boolean; //if it has already been said
}
and now i have several of these inside an Array:
var allDialogs = new Array(); //Array that contains all the game dialogs
//adding the dialogs
allDialogs.Add(exitDialogLine);
allDialogs.Add(politeEndLine);
allDialogs.Add(beingIgnored);
etc...
During my game I do a filter of the ones of a certain type to generate the buttons that allow the player to use those dialogs and it works well:
for(var possibleQuestion : DialogBalloon in dialogManager.allDialogs){
if(
possibleQuestion.isPlayerQuestion &&
possibleQuestion.isUnlocked &&
possibleQuestion.wasDisplayed == false
){
var width : Vector2 = GUI.skin.GetStyle("keywords").CalcSize(GUIContent(possibleQuestion.title));
if( GUI.Button (Rect (0, yOffset, width.x, 30), GUIContent (possibleQuestion.title))){
//do stuff
}
yOffset += 25;
totalYOffset = yOffset;
}
}
Using this for example will show 3 buttons for the 3 only questions out of around 30 that fit the if() requirements but I would also like a variable that tells me the total. If I do a dialogManager.allDialogs.Length inside the ‘for’ loop I always get the total and I can’t figure out what I should write to get the correct results.
Any help very appreciated!
UPDATE - RESOLVED.
The proposed solution didn’t quite work (it would just increase forever) but somehow I got to work by adding a second variable (possibleQuestionTotal) outside the function on the top of the script. Maybe because its a function? Would love to understand why it had to be this way.
function QuestionsGUI(){
var yOffset = 0;
var AreaWidth = 500;
var possibleQuestionSize = 0;
GUILayout.BeginArea(Rect ((Screen.width * 0.6) - (AreaWidth * 0.5),(Screen.height * 0.83), AreaWidth,500));
GUILayout.BeginVertical();
for(var possibleQuestion : DialogBalloon in dialogManager.allDialogs){
if(
possibleQuestion.isPlayerQuestion &&
possibleQuestion.isUnlocked &&
possibleQuestion.wasDisplayed == false
){
var width : Vector2 = GUI.skin.GetStyle("keywords").CalcSize(GUIContent(possibleQuestion.title));
if( GUI.Button (Rect (0, yOffset, width.x, 30), GUIContent (possibleQuestion.title))){
dialogManager.currentDialogDisplay = possibleQuestion;
possibleQuestion.wasDisplayed = true;
showDialogLine = true;
showQuestions = false;
showAnswers = false;
showWaitingLine = false;
waitingTimer = 0.0;
ResetSayDialogLine ();
}
yOffset += 25;
totalYOffset = yOffset;
possibleQuestionSize ++;
possibleQuestionTotal = possibleQuestionSize;
}
}
GUILayout.EndVertical();
GUILayout.EndArea();
}