Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to program dialogue for a game?

Started by Skywarrior9 Jan 22, 2024 at 8:42 PM 11 replies 14k views
Original Post
Skywarrior9
Skywarrior9

I'm working on a prototype that will have an NPC give dialogue for the player to read and since this is the first time handling dialogue I would like some advice on the programming side of handling dialogue.

Tom Sloper
Tom Sloper

You mean you want to display text in a box on the screen?

-- Tom Sloper    --      sloperama.com
Alberth
Alberth

Make a list of questions with possible answers, and for each answer the next question you want to ask.

Have a variable that tracks the currently displayed question. When the user answers, update the variable to the next question.

Skywarrior9
Skywarrior9

@Alberth

The dialogue will be used to tell a story like how most fantasy games have someone explain your purpose in the game. But your advice will help if I plan to do that

Skywarrior9
Skywarrior9

@Alberth

My apologies I meant role-playing games in my first reply

tcemalpaydin
tcemalpaydin

It depends a lot on the depth and complexity you want the dialogue system to be based on.

I have actually thought on the matter quite a bit and I don't know yet if I want it the way recent AAA titles have been going at.

Most games just have minor branching properties and have dialogues pop based on game stage and similiar factors. So basically you always get a small branching dialogue based on game events. But there are other ways to produce similiar dialogue systems, like some of those used in text based adventures.

rogerdv
rogerdv

I have a json containing the nodes of the dialog. It is quite complex because each node can have conditions (like, is Quest Q active, unassigned, completed? is attribute A greater or lesser than value X?). The conditions determine if the node is displayed or no. Each node points to answers in case of an NPC line, or to next NPC line, in case of player answers. The big problem for me is writing an editor that simplifies the task of creating/editing the json files.

Alberth
Alberth

Did you consider writing a text file with lines of text in a convenient form and parse that?

Eg

invite_walk
condition: quest0=completed, attrA > 7
tell: It is good weather today, care for a walk in the garden?
answer: No, not really I am busy -> end
answer: Hmm, maybe, I was looking for a bar -> invite_bar
answer: Yes please, I'd be delighed -> go_for_walk

With python (or any other language for that matter), you can read the lines of the file, and check text of them.

In my example, first line is the name of the node Other lines have a special word in front like “answer:” so you can recognize them. For each type of line you can decode what the text contains. For the “answer” line, you should look for a “→” sequence, which is after the text to show, and before the name of the next node

For a conditions, you first split on commas, then you have texts of the form “word OP word”, where OP is “=”, “>”, etc.

Editable with any text editor, and text-processing to understand the lines isn't terribly complicated.

RmbRT
RmbRT

@Alberth That sounds good, but may be quite a bit more complex once you want to localise to other languages, depending on how easily you want your localisation work to be. I would rather put all that into compiled code, though, because then you can directly invoke stuff as a result of choosing an option, and it allows you to have more control over dynamically generated dialogues or something. Although even then (or especially then :D), localisation will be a pain.

I would simply have a dialogue class, which has a command to generate a list of options (another class), and then there's a callback on each option that gets triggered when you have selected one (such as on_chosen and on_not_chosen or something, if you have different options which also trigger behaviours if you do not chose them). Not that you have to over-OOP it, but it's a good conceptualisation, I think. Each chosen option can then transform the dialogue into the next phase, either statically or dynamically. The dialogue itself should also contain some kind of context as to which things it's referring to (such as the player and a specific NPC, maybe also some other object of interested that the dialogue is about, etc.). Basically each option needs display() string, on_chosen(Dialogue &), and optionally on_not_chosen(Dialogue &), which modify or destroy the dialogue or affect things referenced by the dialogue. A rejected option can also lead to the creation of another option in the next stage of the dialogue.

Anyway, just brainstorming, not sure what exactly you want to do, anyway. If your requirement is just clicking A until the dialog displayed all its text, obviously, you would not need something like this. But I'm assuming you want the player to make choices and that the dialogue actually affects things.

Walk with God.
Alberth
Alberth

RmbRT said:
That sounds good, but may be quite a bit more complex once you want to localise to other languages

I didn't consider localization, but just because I use a concrete sentence doesn't mean it's the only solution. You can instead use a suggestive name like care_for_a_garden_walk_invite instead of the text. The computer can collect them for you (I tend to be as lazy as possible 🙂 ), and generate files (one for each language) for the actual text like

care_for_a_garden_walk_invite : are you interested in a walk in garden ?
no_i_am_busy : ...

Alternatively, you can put all languages in one file, much alike the conversation

care_for_a_garden_walk_invite
en_us: are you interested in a walk in garden ?
en_uk: are you interested in a walk in garden ?
..

no_i_am_too_busy
en_us: Go away, I am terribly busy
...

And of course, my examples are just about defining the interaction and the actual text, it doesn't say anything about how to implement it. So you can use classes etc.

Typically, you first try a small example, to work out what code you exactly need. Once you know, write Python (or other) code that prints the same pattern, for all dialogs you defined in the text files, in a fraction of a second.

Never do anything the computer can do faster or more precise 🙂

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.