I’m a beginner dev working on a game/simulation where there are a lot of NPCs that will roam around and do things around the map. I already have the dialogue system itself somewhat working, they know when to start conversations with each other, the problem is that I don’t know how to write it so that they can different conversations depending on who they’re talking to. I don’t want to give my entire script away, but all that needs to be said is that I have a function in a module that gets 2 characters, a starting character and a replying character, then what I plan it to do is to look through more modules within itself to find a module named after the starting character, check if it has valid dialogue for the replying character, and if it does, it should return the dialogue. The problem is, I want each character to have more than 1 line of dialogue with each other, and I’m not sure how to do it. Can anyone give suggestions on how to implement this?
1 Like
The fix is pretty simple honestly, instead of storing one string per character pair, store a table of lines:
return {
Bob = {
"Hey Bob, seen anything weird today?",
"You always show up at the worst times.",
"...do you ever just stop talking?",
},
Carl = {
"Carl. Long time.",
"Still working at the docks?",
},
}
Then in your dialogue module you just track which line each pair is on:
local Progress = {}
local function GetDialogue(Starter: string, Replier: string): string?
local Lines = require(script[Starter])[Replier]
if not Lines then return nil end
local Key = Starter .. "_" .. Replier
local Index = Progress[Key] or 1
Progress[Key] = (Index % #Lines) + 1
return Lines[Index]
end
Each pair cycles through their lines in order, swap the index for math.random(1, #Lines) if you want it random instead.
Adding on to this, You could have a sort of ‘ChatID’ system for the NPCs to interact. Here’s a rough plan:
- 1 - When NPCs are next to each other, they trigger a handshake (as in, they notice each other). During the handshake, a random value is used to determine who should speak first.
- 2 - Once the handshake is finalised, and someone is selected to speak (say, Bob), A random dictionary of messages is chosen from. Each starting message is paired with an ID. ie,
local BobToCarlStart = {
[1] = "Hey, Carl. How's it goin'?",
[2] = "Did ya see the new person?"
}
- 3 - Once the message is sent, a payload, containing the chatID, (in this case, 2) and interactionID (in this case, ‘a’. (I’d use 1, but ‘a’ is easier to visualise)) is sent to the other NPC, in this case, Carl. Carl can then choose from a predefined list, using the ChatID of 2 and interaction ID of a from another dictionary as such:
local CarlToBob = {
[1] = {
['a'] = "Oh, Bob... Not bad.",
['b'] = "Bob, thank the blox you're here, something terrible happened."
},
[2] = {
['a'] = "Yeah, they look... interesting.",
['b'] = "New person? What do you mean?"
}
}
- 4 - The message is displayed, and the payload response is sent to Bob again. Bob receives the payload,
chatID = 2, interactionID = 'a'and selects from a dictionary again as such:
local BobToCarl = {
-- *
[1] = {
['a'] = {
"Good t' hear.",
"That's nice t' know."
},
['b'] = {
"Woah, Hold ya horses, what's happenin'?",
"What's th' problem, Carl?"
}
},
[2] = {
['a'] = {
"Interesting? What d'ya mean?",
"Huh. Why's that?"
},
['b'] = {
"You haven't heard?",
"Oh wow, d'ya not see them?"
},
}
}
(Eventually you can keep sending payloads and interactions like a tree, but I’ve kept it at 2 branches for simplicity. Feel free to also add a chatCount or such to help guide the NPCs through a more tree-like system, by making another dictionary at the *, so it encloses everything.)
- 5 - When you want to end a conversation, you could just add a 0 in place of a comment, and when the 0 is selected, we can disconnect the NPCs and they can get on with their day.
Hope that helps, @The_Ultinate !
1 Like