DialogueTree
Backend code to process dialogue trees, with built-in functionality for sessions and memory. Uses signals to communicate with the front-end.
Functions
new
Creates a new DialogueTree instance with the given dialogue data.
Example:
local Praxis = require(game.ReplicatedStorage.Praxis)
return Praxis.DialogueTree.new({
CharacterName = "Joe",
DialogueTreeData = {
Text = function(self, player : Player)
if self:Recall(player, "HasMet") then
return "Hello again! What do you need?"
else
return "Oh, hello there, nice to meet you! How can I help?"
end
end,
Options = function(self, player : Player)
if self:Recall(player, "HasMet") then
return {
{
Input = "Nothing",
},
{
Input = "Can you tell me more?",
Response = {
Text = {"Yes!", "I am the color blue!", "And not green!"}
}
}
}
else
return {
{
Input = "Tell me about yourself",
Response = {
Text = {"Sure!", "I am awesome, swag, and cool", "And that's all!"}
}
}
}
end
end,
}
})
Converse
DialogueTree.Converse() → Signal.Signal<string,Options>Starts a new dialogue session for the given player. If a session already exists for the player, a warning will be issued and no new session will be created.
Example:
local TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new
local SessionSignal = TestDialogue:Converse(player)
if SessionSignal then
SessionSignal:Connect(function(text : string, options : Options)
print(`Showing text: {text}`) -- Text that shows in dialogue box
for index, option in options do
print(`Option {index}: {option.Input}`)
end
end)
end
End
Ends the dialogue session for the given player. If no session exists for the player, a warning will be issued and no action will be taken.
Respond
Responds to the current dialogue session for the given player with the selected option index. If no session exists for the player, a warning will be issued and no action will be taken.
If pickedOptionIndex is nil, the session will be ended. This is the most common way to end dialogue sessions, as it allows the front-end to handle the end of the session gracefully.
Example:
local TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new
-- Run code elsewhere to begin conversation
local function OnOptionPicked(player : Player, pickedOptionIndex : number?) -- Mock function for UI button that picks option
TestDialogue:Respond(player, pickedOptionIndex)
end
Remember
Remembers a value in the session's memory, using the original memory table inserted with DialogueTree:Converse. If no session exists for the player, a warning will be issued and no action will be taken.
Example:
local TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new
-- Run code elsewhere to begin conversation
TestDialogue:Remember(player, "HasMet", true)
Forget
Forgets a value in the session's memory, using the original memory table inserted with DialogueTree:Converse. If no session exists for the player, a warning will be issued and no action will be taken.
Example:
local TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new
-- Run code elsewhere to begin conversation
TestDialogue:Forget(player, "HasMet")
Recall
Recalls a value in the session's memory, using the original memory table inserted with DialogueTree:Converse. If no session exists for the player, a warning will be issued and nil will be returned.
Example:
local TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new
-- Run code elsewhere to begin conversation
local HasMet = TestDialogue:Recall(player, "HasMet")
ClearSessions
Clears all sessions for all players. It's recommended to call this when dialogue source is cleaned up.