- Home /
How to store children of a class inside that class
So I can't figure out the logical way of doing this.
In my project when a character talks to another one he starts a dialog. (class actorDialog).
At the end of that dialog I load the "children"as possible answers and display them for the player to chose. Then it plays that answer and so on.
Now my problem is how to actually write this. This is the class I created for it:
class actorDialog {
var title : String; //GUI title to select it
var isAvailable : boolean; //Is this dialog unlocked?
var script : List.<ScriptStep>; //Text for the dialog
var total : int; //How many types this dialog was selected
var children : List.<actorDialog>; //Possible replies to the dialog
}
Now my problem is the actorDialog.children, since its a List of items of the same class inside the class itself. It makes an eternal loop that slows down Unity and I imagine it just doesn't make sense to have it that way.
Any other suggestions on how to go about it? This seems like the most logical way for me but probably I'm wrong...
Any help very appreciated!
There shouldn't be an infinite loop there. It's a tree structure, so depending on your code, it will run until you hit all leaves(nodes without children) and then stop. How deep is your tree?
That's what I would like, exactly like a tree structure! But where do I define how deep is supposed to go? (I'm imagining maybe 5 levels). I also realized that once I wrote this class, Unity became very slow doing Undo/Redo (taking about 10 secs) and the file size for the scene.unity increased about 4 times making me thing I shouldn't even be doing this or maybe it's supposed to be written in a different way?
You don't set how deep the tree is. It just depends on how deep you build it.
It sounds like you only need the current children at any given moment. Where is your code to fill the children list?
This is what I would write as a code for it:
This would be the full class:
class actorDialog {
var title : String; //GUI title to select it
var isAvailable : boolean; //Is this dialog unlocked?
var script : List.<ScriptStep>; //Text for the dialog
var total : int; //How many types this dialog was selected
var children : List.<actorDialog>; //Possible replies to the dialog
function actorDialog (script : List.<ScriptStep>, actorDialogList : List.<actorDialog>){
this.title = script[0].textLine;
this.script = script;
actorDialogList.Add (this);
}
}
And then on Start() I would write this:
startDialogWife = new actorDialog (obj$$anonymous$$anager.actorPlayer, dialog$$anonymous$$anager.startDialogWife);
startDialogWife.children.Add(askHowSheIsFeeling);
startDialogWife.children.Add(askAboutWatchWife);
startDialogWife.children.Add(askAboutHerPast);
and maybe some of them would have sub children.
But what I mean is, even before writing any of these extras, JUST writing the class makes Unity slowdown a lot.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem with arrays in a list 1 Answer
How to obtain a gameObject from an List with the name? 1 Answer
Looping through multiple lists 0 Answers