Skip to main content

Level

Based on game build 714014 | Last updated: 2026-02-27

Overview

The Level component encapsulates all data and logic for a single playable level in Don't Starve Together's world generation system. It defines how tasks (room layouts) and set pieces (fixed structures) are selected, configured, and applied during world generation. This class is instantiated once per level during worldgen and serves as the central authority for level-specific overrides, task substitutions, and mod integration hooks. It interacts with the tasks and tasksets modules to resolve task and task set definitions.

Usage example

local Level = require("map/level")
local level_data = {
id = "forest_cave",
baseid = "forest_cave",
name = "Forest Cave",
desc = "A cave entrance near the forest",
location = "forest_cave",
hideinfrontend = false,
hideminimap = false,
overrides = {
task_set = "forest_cave_taskset"
},
required_prefabs = { "cave_entrance" },
required_setpieces = { "cave_entrance" },
random_set_pieces = { "random_cave_1", "random_cave_2" },
numrandom_set_pieces = 2
}

local level = Level(level_data)
level:ChooseTasks()
level:ChooseSetPieces()
local tasks = level:GetTasksForLevel()

Dependencies & tags

Components used: None (this is a standalone data/configuration class, not attached to entities). Tags: None identified.

Properties

PropertyTypeDefault ValueDescription
idstringnilUnique identifier for the level; asserted during construction.
baseidstringSame as idBase identifier used for world generation.
namestring""Human-readable name of the level.
descstring""Human-readable description of the level.
override_level_stringbooleanfalseIf true, disables default localization fallbacks.
locationstringnilLocation identifier (e.g., "forest_cave") used by worldgen; asserted.
hideinfrontendbooleannilWhether the level is hidden in the frontend menu.
overridestable{}Dictionary of overrides, especially task_set.
substitutestable{}Task/resource substitution rules.
set_piecestablenilDeprecated; assert prevents usage.
numoptionaltasksnumbernilDeprecated; assert prevents usage.
optionaltaskstablenilDeprecated; assert prevents usage.
valid_start_taskstablenilDeprecated; assert prevents usage.
hideminimapbooleanfalseHides the level from minimap.
min_playlist_positionnumber0Minimum playlist slot position (unused).
max_playlist_positionnumber999Maximum playlist slot position (unused).
ordered_story_setpiecestablenilDeprecated ordered story set pieces (unused).
required_prefabstablenilList of required prefabs for the level.
background_node_rangetablenilBackground node range configuration.
blocker_blank_room_namestringnilName of the blank room used for blockers.
required_setpiecestablenilList of required set pieces.
numrandom_set_piecesnumber0Number of random set pieces to assign.
random_set_piecestablenilPool of random set pieces to draw from.
playstylestringnilPlaystyle tag for level categorization.
chosen_taskstablenilList of selected tasks after ChooseTasks() is called.
versionnumber1Version of the level definition.

Main functions

Level:ApplyModsToTasks(tasklist)

  • Description: Applies mod-defined post-initialization functions to each task in the provided task list, allowing mods to alter task definitions before use.
  • Parameters:
    • tasklist: table — List of task definitions to mod.
  • Returns: nil
  • Error states: None.

Level:GetOverridesForTasks(tasklist)

  • Description: Applies task substitution rules defined in self.substitutes to the task list. Each task is randomly checked against perstory and, if passed, has its corresponding task name replaced with the substitute name (based on pertask chance).
  • Parameters:
    • tasklist: table — List of task definitions to apply overrides to.
  • Returns: table — The modified tasklist with substitution entries added (if any).
  • Error states: None.

Level:GetTasksForLevel()

  • Description: Returns the list of chosen tasks after ChooseTasks() has been called.
  • Parameters: None.
  • Returns: table — The self.chosen_tasks list; nil if ChooseTasks() has not been called.
  • Error states: Returns nil if called before ChooseTasks().

Level:ChooseTasks()

  • Description: Selects and prepares tasks for this level by loading the task set defined in overrides.task_set, applying mod hooks, and processing optional and random tasks. Sets self.chosen_tasks.
  • Parameters: None.
  • Returns: nil
  • Error states:
    • Asserts if overrides.task_set is missing.
    • Asserts if the resolved task_set_data is nil (e.g., if modded preset is used without mods enabled).

Level:GetTasksForLevelSetPieces()

  • Description: Filters self.chosen_tasks to exclude tasks marked with level_set_piece_blocker.
  • Parameters: None.
  • Returns: table — List of tasks suitable for random set piece assignment.
  • Error states: None.

Level:ChooseSetPieces()

  • Description: Assigns required and random set pieces to tasks. If required_setpieces or numrandom_set_pieces are defined, set pieces are randomly assigned to non-blocker tasks. Also processes set_pieces entry to place set pieces on tasks matching choicedata.tasks.
  • Parameters: None.
  • Returns: nil
  • Error states:
    • Asserts if ChooseTasks() has not been called (self.chosen_tasks == nil).
    • Asserts if no valid tasks exist for set_pieces choices.

Level:EnqueueATask(tasklist, taskname)

  • Description: Adds a deep copy of the named task to tasklist using tasks.GetTaskByName.
  • Parameters:
    • tasklist: table — Target list to append the task to.
    • taskname: string — Name of the task to retrieve and copy.
  • Returns: nil
  • Error states: Asserts if taskname is not found.

Level:SetID(id)

  • Description: Sets the id and worldgen_id fields; used internally during construction.
  • Parameters:
    • id: string — Level identifier.
  • Returns: nil
  • Error states: Asserts if id is nil.

Level:SetBaseID(id)

  • Description: Sets the baseid and worldgen_baseid fields; used internally during construction.
  • Parameters:
    • id: string — Base level identifier.
  • Returns: nil

Level:SetNameAndDesc(name, desc)

  • Description: Sets name, desc, worldgen_name, and worldgen_desc.
  • Parameters:
    • name: string? — Level name. May be nil.
    • desc: string? — Level description. May be nil.
  • Returns: nil

Events & listeners

None.