- Home /
Start of level Dialogue
Hello,
I'm working on a 2D game and looking for some advice on how to program a small section of dialogue at the start of every level (in game).
I'd like a character portrait, and room for some dialogue next to that. I'd also like the player to be able to choose dialogue occasionally. I understand fairly well what GUI components I need for each part, but am less certain on how to store and set up the changes of the portrait and text. Any advice would be welcome, and thanks for reading!
Answer by Professor Snake · Mar 04, 2013 at 04:14 PM
A relatively complex solution would be to store all the page data in arrays, as well as the buttons in each page and their functions. An important variable to understand is pageChanges. I use that to show which page has what kind of buttons, and which page they lead to.
Each entry should have this format: "currentPage"-"pageToGoTo" (so, 1-3 would mean that we should create a button in page 1 that takes us to page 3. For its name, we are going to use the buttonNames array; for instance, if "1-3" is in position X of the pageChanges array, we are going to use buttonNames[X] to name the button that is in page 1 and takes you to page 3.
We can also detect whether the entry is named "Exit" and leave the dialogue with it.) The arrays buttonNamesForPage and pageChangesForPage should have a length equal to the maximum amount of dialogue available in a page.
They are ultimately unnecessary, but they will simplify things.
var pageChanges:String[];
var pageChangesForPage:int[];
var buttonNames:String[];
var buttonNamesForPage:String[];
var pageToShow:int=0;
var pageTitles:String[];
var pageTexts:String[];
var pageImages:Texture2D[];
function OnGUI(){
//Draw a gui box with the title being pageTitles[pageToShow]
//draw the pageImages[pageToShow] text.
//draw the page text the same way.
//Now we are going to parse the pageChanges data to see how many buttons the page will have
var thisPagePointer:int=0;
for(var i:int=0;i<pageChanges.length;i++){
var textPointer:String[]=pageChanges[i].Split("-"[0]);
if(int.Parse(textPointer[0])==pageToShow){
pageChangesForPage[thisPagePointer]=textPointer[1];
buttonNamesForPage[thisPagePointer]=buttonNames[i];
thisPagePointer++;
}
}
//Now we have assigned all the data we need to our arrays, time to make buttons
GUILayout.BeginVertical(); //you need a GUILayout, it's up to you to customise the coordinates of the GUI.BeginArea that should go in front of it.
for(var j:int=0;j<thisPagePointer;j++){
if(GUILayout.Button(buttonNamesForPage[j])){
if(pageChangesForPage[j]=="Exit")
QuitDialogue();
else
pageToShow=int.Parse(pageChangesForPage);
}
}
}
function QuitDialogue(){
//do things to stop the dialogue and start playing
}
It's a somewhat complex solution but it should get the job done. I'll give you setup examples in a bit.
Examples: Let's assume that in the first dialogue box you want to give the player two choices, a "Hi" choice that takes the user to another page and an "Exit" choice. You also want the second page to be the last and only contain an "exit" choice. This is how the arrays should be set up:
pageChanges: ["0-1","0-Exit","1-Exit"] //Exclude the brackets in all of these
pageChangesForPage: Set length to 2
buttonNames: ["Hi","Leave","Leave"]
buttonNamesForPage: Set length to 2
pageTitles: ["You see a person down the street","You greet the person"]
pageTexts: ["What do you say to the person?","The person greets you as well and continues down the road"]
pageImages: [image of you , image of stranger]
The buttons will be created in the order that the page changers are found in the pageChanges array, so if the array looks like [...,"1-3","0-4","0-2",...] , the button that takes you to page 4 will be shown first in page 0.
Thanks for the assist, Prof. $$anonymous$$anaged to get a basic implementation of in-game dialog in place.
Answer by poncho · Mar 04, 2013 at 03:18 PM
well, you will need gamestates, like, Start, Dialogue, Game, Pause, End, Win, Lose in this Dialog, you could use a GuiBox, a GuiTexture, and a GuiText, a wholetext variable and a textbytime, a timecounter, depending on the timecounter, set a maxtime per character (maxtimeperchar), so when the timecounter reaches maxtimeperchar, you add a letter from the wholetext to the textbytime, that way you will show the letters one by one depending on your maxtimeperchar variable, the portrait would be even easier, you just need a public texture per character(people) and when that char is talking, you just need to change the portrait texture, that would be all you need for that dialogue system, at least is how i made it, good luck and happy coding =D
Thanks, poncho! Got basic in game dialog working. $$anonymous$$uch appreciated!
Your answer
Follow this Question
Related Questions
I am trying to make an RPG style dialog script 2 Answers
How to prevent GUI dialog windows from overlapping? 1 Answer
Editor Input Dialog 2 Answers
Showing Time With GUI.Label 1 Answer