Author Topic: The best way to implement dialogue in games  (Read 2919 times)

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
The best way to implement dialogue in games
« on: Sun, Jan 15, 2012 »
What is the best way to implement dialogue in games? By this, I mean the boxes that show what the characters are saying. Do I put the dialogue in the code or load it from somewhere like a text file for example. What if I want the words to appear one by one? Thanks!
Check out my game journal: http://alternatdev.wordpress.com

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: The best way to implement dialogue in games
« Reply #1 on: Sun, Jan 15, 2012 »
If you have a small amount of dialog lines, maybe up to 100, I find the best way to do it is to create an array and then use a counter to find out where you are up to, and then pull the next line out of the array.

If you have a huge script, you might want to type it all into a text file and parse that file line by line.

With the words appearing one by one, I haven't done that before but I really do like that effect. Alex Kidd in Miracle World did that and it's cool.
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: The best way to implement dialogue in games
« Reply #2 on: Sun, Jan 15, 2012 »
Loading it from a file would be nice, but there's a catch:  if you're planning on selling your game to a sponsor or uploading it to a game site then everything has to be contained in the swf file.  If you go this route then my suggestion would be to embed the file.  Then you can pharse it line by line, word by word, or whatever you design it to do.

The hard-coded / array approach also works well for quick/small tasks.  I do it myself sometimes. Like initials said though, if you're dealing with a lot of text, like character dialog in an RPG or something, then you might be better off with files - they're just easier to maintain if you have to make frequent changes.

As far as the dialog window/HUD goes, there's probably multiple ways to do it.  FlxText supports multiple lines and so does FlxBitmapFont.  If you search around you may be able to find some helpful classes that'll get you close to what you need, but I suspect that you're probably going to have to design something that meets your specific needs.

Placeable

  • Member
  • **
  • Posts: 49
  • Karma: +0/-0
    • View Profile
Re: The best way to implement dialogue in games
« Reply #3 on: Mon, Jan 16, 2012 »
Hello!

You might want to check out FlxDialog: http://flashgamedojo.com/wiki/index.php?title=FlxDialog

It's a very neat class that gives you a simple dialog approach of a linear text.  All it requires is that you insert an array of your dialog where, I believe, each slot in the array contains one page of text.

You can then build upon this, as I have, with a separate class (I simply call Dialog >_>) that reads and encodes .txt files and builds them into an array, I've also added parse tags to create new pages and more dialog in the same file.

To embed .txt files to your project it's no more hazzle than the following line:

Code: [Select]
[Embed(source = "../resources/dialogs/test.txt", mimeType = "application/octet-stream")] public static var testTXT:Class;
Then in my Dialog class (Not FlxDialog) I can insert, in this case, the testTXT object to parse the .txt file.  It looks like this (a cut out version):

Code: [Select]
public function Dialog(dialog:Class, ...)
{
_dialog = new Array();


var MyFileByteArray:ByteArrayAsset = ByteArrayAsset(new dialog());
SetupDialog(MyFileByteArray.readUTFBytes(MyFileByteArray.length));
}

dialog:Class is the testTXT object containing the .txt file.

SetupDialog
Code: [Select]
/**
* Splits the string into several pages
*/
private function SetupDialog(sString:String):void
{
//::  Split up the dialogs
var arr:Array = sString.split("<&>");

for (var i:int; i < arr.length; i++)
{
//::  Split up pages in each dialog
_dialog.push(new Array(arr[i].split("<#>")));
}
}

The parsing here is with the <&> and the <#> tags.  The <&> tag splits up different conversations in the same .txt file and the <#> tag splits up several pages for one conversation.  This might seem confusing but here's a brief example:

I have the following .txt file called test.txt and it contains the following text:

Code: [Select]
Hello my name is TEST<#>And I like cake!<&>Oh!  Its you again!<#>Would you please leave me alone?

So once I talk to the NPC holding this dialog he would say:

"Hello my name is TEST"

NEW PAGE

"And I like cake!"

END DIALOG

Then once you talk to him again he would say (since this is branched of as a new dialog for the NPC):

"Oh!  Its you again!"

NEW PAGE

"Would you please leave me alone?"

END DIALOG

So if you talked to him again he would start there once more (... at "Oh!  Its you again!").

So in the end the array I use in FlxDialog is _dialog (as explained above).

And that's it really.  You could add tons of features to this as it really gets flexible if you are dealing with a lot of dialogues in your game and want different responses with your NPCs.
I've even added branching depending on conditions in my game but we won't get into that as it gets REALLY confusing there >_<
But really if you want to keep it simple then just insert an array of text into the FlxDialog's showDialog(pages:Array) function.

Hope it helps you!
« Last Edit: Mon, Jan 16, 2012 by Placeable »

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: The best way to implement dialogue in games
« Reply #4 on: Mon, Jan 16, 2012 »
Nice!

xyroclast

  • Contributor
  • ****
  • Posts: 389
  • Karma: +0/-0
    • View Profile
Re: The best way to implement dialogue in games
« Reply #5 on: Mon, Jan 30, 2012 »
What happens if you talk to him a third time?

Placeable

  • Member
  • **
  • Posts: 49
  • Karma: +0/-0
    • View Profile
Re: The best way to implement dialogue in games
« Reply #6 on: Tue, Feb 14, 2012 »
Quote
So if you talked to him again he would start there once more (... at "Oh!  Its you again!").

He would just pickup at the same spot.  If you have no added any more pages or dialogs he would continue from the last used one.