Skip to main content

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

DialogueTree.new(DialogueDialogue) → DialogueTree

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(
selfDialogueTree,
playerPlayer,
memoryDialogueMemory?
) → 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

DialogueTree.End(
selfDialogueTree,
playerPlayer
) → ()

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

DialogueTree.Respond(
selfDialogueTree,
playerPlayer,
pickedOptionIndexnumber?
) → ()

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

DialogueTree.Remember(
selfDialogueTree,
playerPlayer,
indexany,
valueany
) → ()

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

DialogueTree.Forget(
selfDialogueTree,
playerPlayer,
indexany
) → ()

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

DialogueTree.Recall(
selfDialogueTree,
playerPlayer,
indexany
) → any

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

DialogueTree.ClearSessions(selfDialogueTree) → ()

Clears all sessions for all players. It's recommended to call this when dialogue source is cleaned up.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Creates a new DialogueTree instance with the given dialogue data.\n\nExample:\n\n```lua\nlocal Praxis = require(game.ReplicatedStorage.Praxis)\n\nreturn Praxis.DialogueTree.new({\n    CharacterName = \"Joe\",\n    \n    DialogueTreeData = {\n        Text = function(self, player : Player)\n            if self:Recall(player, \"HasMet\") then\n                return \"Hello again! What do you need?\"\n            else\n                return \"Oh, hello there, nice to meet you! How can I help?\"\n            end\n        end,\n        \n        Options = function(self, player : Player)\n            if self:Recall(player, \"HasMet\") then\n                return {\n                    {\n                        Input = \"Nothing\",\n                    },\n                    \n                    {\n                        Input = \"Can you tell me more?\",\n                        \n                        Response = {\n                            Text = {\"Yes!\", \"I am the color blue!\", \"And not green!\"}\n                        }\n                    }\n                }\n            else\n                return {\n                    {\n                        Input = \"Tell me about yourself\",\n                        \n                        Response = {\n                            Text = {\"Sure!\", \"I am awesome, swag, and cool\", \"And that's all!\"}\n                        }\n                    }\n                }\n            end\n        end,\n    }\n})\n```",
            "params": [
                {
                    "name": "Dialogue",
                    "desc": "",
                    "lua_type": "Dialogue"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DialogueTree"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 175,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "Converse",
            "desc": "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.\n\nExample:\n\n```lua\nlocal TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new\n\nlocal SessionSignal = TestDialogue:Converse(player)\n\nif SessionSignal then\n    SessionSignal:Connect(function(text : string, options : Options)\n        print(`Showing text: {text}`) -- Text that shows in dialogue box\n\n        for index, option in options do\n            print(`Option {index}: {option.Input}`)\n        end\n    end)\nend\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "memory",
                    "desc": "",
                    "lua_type": "DialogueMemory?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal.Signal<string, Options>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 210,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "End",
            "desc": "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.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 237,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "Respond",
            "desc": "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.\n\nIf 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.\n\nExample:\n\n```lua\nlocal TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new\n\n-- Run code elsewhere to begin conversation\n\nlocal function OnOptionPicked(player : Player, pickedOptionIndex : number?) -- Mock function for UI button that picks option\n    TestDialogue:Respond(player, pickedOptionIndex)\nend\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "pickedOptionIndex",
                    "desc": "",
                    "lua_type": "number?"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 272,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "Remember",
            "desc": "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.\n\nExample:\n\n```lua\nlocal TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new\n\n-- Run code elsewhere to begin conversation\n\nTestDialogue:Remember(player, \"HasMet\", true)\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "any"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 301,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "Forget",
            "desc": "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.\n\nExample:\n\n```lua\nlocal TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new\n\n-- Run code elsewhere to begin conversation\n\nTestDialogue:Forget(player, \"HasMet\")\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 327,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "Recall",
            "desc": "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.\n\nExample:\n\n```lua\nlocal TestDialogue = require(path.to.dialogue) -- Using our example from DialogueTree.new\n\n-- Run code elsewhere to begin conversation\n\nlocal HasMet = TestDialogue:Recall(player, \"HasMet\")\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                },
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 353,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        },
        {
            "name": "ClearSessions",
            "desc": "Clears all sessions for all players. It's recommended to call this when dialogue source is cleaned up.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "DialogueTree"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 366,
                "path": "src/Packages/DialogueTree/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "DialogueTree",
    "desc": "Backend code to process dialogue trees, with built-in functionality for sessions and memory. Uses signals to communicate with the front-end.",
    "source": {
        "line": 6,
        "path": "src/Packages/DialogueTree/init.luau"
    }
}