Mod Utilities
Version History
Build Version | Change Date | Change Type | Description |
---|---|---|---|
676042 | 2025-06-21 | stable | Current version |
Overview
The modutil
module provides essential utility functions for mod development, including error handling, environment setup, configuration management, and integration with the game's various systems. It serves as the foundation for mod development in Don't Starve Together.
Usage Example
-- Error handling in mods
modassert(condition, "This must be true")
moderror("Something went wrong")
modprint("Debug information")
-- Get mod configuration
local config_value = GetModConfigData("option_name")
-- Character management
AddModCharacter("mycharacter", "MALE", {"forest", "cave"})
RemoveDefaultCharacter("wilson")
Error Handling Functions
moderror(message, level)
Status: stable
Description: Reports mod errors with proper context and mod identification. Behavior depends on whether mod debug printing is enabled - will assert if enabled, otherwise prints a warning.
Parameters:
message
(string): Error message to displaylevel
(number): Optional stack level for error reporting (default: 1)
Example:
moderror("Invalid configuration value")
moderror("Function called with wrong parameters", 2)
modassert(test, message)
Status: stable
Description: Assert function for mods that uses moderror for reporting. Only triggers if the test condition fails.
Parameters:
test
(any): Condition to test (truthy/falsy)message
(string): Error message if assertion fails
Returns:
- (any): The test value if successful
Example:
local value = modassert(some_value, "Value cannot be nil")
modassert(#items > 0, "Items list must not be empty")
modprint(...)
Status: stable
Description: Debug print function for mods. Only outputs if mod debug printing is enabled in the mod index settings.
Parameters:
...
(any): Values to print
Example:
modprint("Debug info:", player_count, world_state)
modprint("Mod initialized successfully")
Release ID System
CurrentRelease.GreaterOrEqualTo(release_id)
Status: stable
Description: Checks if the current game version supports a specific release ID, allowing mods to conditionally use features based on game version.
Parameters:
release_id
(string): Release ID to check
Returns:
- (boolean): True if current version supports the release
Example:
if CurrentRelease.GreaterOrEqualTo("R35_SANITYTROUBLES") then
-- Use sanity trouble features
inst.components.sanity:AddSanityPenalty("darkness", -5)
end
CurrentRelease.PrintID()
Status: stable
Description: Prints the current release ID to console for debugging purposes.
Example:
CurrentRelease.PrintID()
-- Output: "Current Release ID: ReleaseID.R37_LUNAR_CAGE"
Configuration Management
GetModConfigData(optionname, modname, get_local_config)
Status: stable
Description: Retrieves configuration values for mod settings. This is an internal function - mods should use the environment version provided during initialization.
Parameters:
optionname
(string): Name of the configuration optionmodname
(string): Mod identifier (required when calling outside modmain)get_local_config
(boolean): Whether to get client-local config (optional)
Returns:
- (any): Configuration value, or nil if not found
Example:
-- Within mod environment (modname automatically provided)
local difficulty = GetModConfigData("difficulty_level")
local show_debug = GetModConfigData("debug_mode")
-- Manual call with modname
local value = GetModConfigData("option", "workshop-12345", false)
Character Management
AddModCharacter(name, gender, modes)
Status: stable
Description: Registers a new mod character with the game, including gender assignment and custom skin modes for animation states.
Parameters:
name
(string): Character prefab namegender
(string): Character gender ("MALE", "FEMALE", "ROBOT", "NEUTRAL", "PLURAL")modes
(table): Optional skin mode definitions for character animation states
Example:
-- Basic character registration
AddModCharacter("mycharacter", "FEMALE")
-- Character with custom skin modes
AddModCharacter("mycharacter", "FEMALE", {
{ type = "normal_skin", play_emotes = true },
{ type = "ghost_skin", anim_bank = "ghost", idle_anim = "idle", scale = 0.75, offset = { 0, -25 } },
{ type = "powered_skin", anim_bank = "mycharacter_powered", scale = 1.1 }
})
RemoveDefaultCharacter(name)
Status: stable
Description: Removes a default game character from the character selection list.
Parameters:
name
(string): Character name to remove
Example:
RemoveDefaultCharacter("wilson")
RemoveDefaultCharacter("willow")
Environment Setup Functions
The modutil system provides numerous environment setup functions that are automatically added to mod environments. These functions enable mods to integrate with various game systems:
World Generation
AddLevel(level)
: Add custom world levelAddTask(task)
: Add world generation taskAddTaskSet(taskset)
: Add task set for level generationAddRoom(room)
: Add custom room typeAddStartLocation(location)
: Add spawn location option
Customization and UI
AddRecipeFilter(filter)
: Add crafting recipe filterAddIngredientValues(values)
: Add ingredient nutritional valuesAddCharacterSelectPostInit(fn)
: Add character select screen modificationsAddMainPostInit(fn)
: Add main menu modificationsAddGamePostInit(fn)
: Add post-game initialization
Tiles and Terrain
AddTile(tile)
: Register custom ground tileAddTileData(data)
: Add tile behavior dataAddNoiseFunction(name, fn)
: Add terrain noise function
Post-Initialization Systems
AddPlayerPostInit(fn)
: Add player initialization callbackAddPrefabPostInit(prefab, fn)
: Add prefab modification callbackAddComponentPostInit(component, fn)
: Add component modification callbackAddClassPostConstruct(class, fn)
: Add class constructor callbackAddStategraphPostInit(sg, fn)
: Add stategraph modification callbackAddBrainPostInit(brain, fn)
: Add brain modification callback
Recipe and Crafting
AddRecipe2(name, ingredients, tech, config)
: Add crafting recipe (new system)AddRecipe(name, ingredients, tech, config)
: Add crafting recipe (legacy)AddDeconstructRecipe(prefab, ingredients)
: Add deconstruction recipeAddCookerRecipe(cooker, recipe)
: Add cooking recipe
Actions and Interactions
AddAction(action)
: Register custom actionAddComponentAction(type, component, fn)
: Add component actionAddStategraphAction(action)
: Add stategraph actionAddStategraphActionHandler(sg, action, handler)
: Add action handler
Assets and Resources
RegisterInventoryItemAtlas(atlas, prefab)
: Register inventory atlasRegisterSkinAtlas(atlas, build)
: Register skin atlasLoadPOFile(path, lang)
: Load localization fileModAssets
: Table for mod asset registration
Networking
AddModRPCHandler(namespace, name, fn)
: Add RPC handlerAddShardModRPCHandler(namespace, name, fn)
: Add shard RPC handlerAddUserCommand(command)
: Add user console commandAddVoteCommand(command)
: Add voting command
Cooking and Food
AddCookerRecipe(cooker, recipe)
: Add recipe to specific cookerAddFoodData(name, food_data)
: Add food nutritional dataAddIngredientValues(values)
: Add ingredient cooking values
Localization
STRINGS
: Global strings table for localizationLoadPOFile(path, lang)
: Load translation filesCreatePrefabSkinStrings(prefab)
: Create skin name strings
UI and Interface
AddModScreenPostInit(screen, fn)
: Modify game screensAddOptionScreenPostInit(fn)
: Modify options screenAddFrontEndAssets(assets)
: Add frontend-specific assets
Internal Environment Functions
InsertPostInitFunctions(env, isworldgen, isfrontend)
Status: stable
Description: Internal function that sets up the mod environment with all necessary post-initialization functions and utilities. This creates the sandboxed environment that mods execute within.
Parameters:
env
(table): Environment table to populateisworldgen
(boolean): Whether this is world generation contextisfrontend
(boolean): Whether this is frontend context
Utility Functions
ModInfoname(name)
Status: stable
Description: Returns a formatted mod name string including both the mod ID and display name for better identification in logs and error messages.
Parameters:
name
(string): Mod identifier
Returns:
- (string): Formatted name string
Example:
local formatted = ModInfoname("workshop-12345")
-- Output: "workshop-12345 (Better Farming Mod)"
Class Modification
AddClassPostConstruct(package, postfn)
Status: stable
Description: Adds a post-construction callback to a class loaded from a package, allowing mods to modify class instances after creation.
Parameters:
package
(string): Package path to the classpostfn
(function): Function to call after construction
AddGlobalClassPostConstruct(package, classname, postfn)
Status: stable
Description: Adds a post-construction callback to a global class, useful for modifying game classes.
Parameters:
package
(string): Package path where class is definedclassname
(string): Name of the global classpostfn
(function): Function to call after construction
Environment Variables
Mods receive these environment variables automatically:
modname
: Current mod identifiermodassert
: Mod assertion functionmoderror
: Mod error reporting functionpostinitfns
: Table of post-initialization functionspostinitdata
: Table of post-initialization data
Integration
The mod utilities integrate with:
- Error System: Standardized error reporting and debugging
- Configuration: Mod settings and options management
- Character System: Custom character registration
- Asset Loading: Mod-specific asset integration
- Networking: RPC and communication systems
- UI System: Interface modifications and additions
Related Modules
- Mods: Core mod loading and management system
- Mod Index: Mod registry and information management
- Mod Compatibility: Version upgrade and compatibility handling