Skip to main content

Fundamentals Utilities Overview

Build Information

Current documentation based on build version: 676042 Last updated: 2025-06-21

System Purpose

The Fundamentals Utilities category provides essential helper functions and utility systems that serve as the foundation for all DST game mechanics. These utilities offer standardized solutions for common programming tasks including mathematical calculations, string processing, table manipulation, vector operations, performance monitoring, and specialized game-related functionality.

Key Responsibilities

  • Provide mathematical utility functions for calculations and vector operations
  • Handle string manipulation, formatting, and character speech generation
  • Offer table manipulation and data structure utilities
  • Support performance monitoring and profiling capabilities
  • Enable file system operations and persistent data management
  • Facilitate entity finding, position validation, and simulation helpers
  • Supply debugging and development support utilities

System Scope

This category includes foundational utility functions used across all game systems but excludes specific gameplay mechanics (handled by Game Mechanics) and high-level component systems (handled by Core Systems).

Architecture Overview

System Components

The utilities are organized into specialized modules that provide focused functionality while maintaining loose coupling between different utility types. Each module serves specific domains while sharing common patterns for error handling and parameter validation.

Data Flow

Game Systems → Utility Functions → Low-Level Operations → Results
↓ ↓ ↓ ↓
User Input → Helper Functions → Math/String/Table Ops → Game State

Integration Points

  • All Core Systems: Utilities are used throughout the entire codebase
  • Component Systems: Provide foundational operations for component logic
  • Entity Systems: Support entity manipulation and state management
  • Performance Systems: Enable monitoring and optimization capabilities

Recent Changes

BuildDateComponentChange TypeDescription
6760422025-06-21All UtilitiesstableCurrent stable versions

Core Utility Modules

General Utilities

Comprehensive collection of helper functions for common programming tasks.

ModuleStatusDescriptionKey Features
Utility FunctionsstableTable manipulation, string processing, math calculationsData structures, random selection, debugging

Mathematical Utilities

Mathematical functions for calculations, interpolation, and vector operations.

ModuleStatusDescriptionKey Features
Math UtilitiesstableSine waves, interpolation, rounding, distance calculationsWave generation, lerp functions, angle operations

Vector Mathematics

Object-oriented and functional vector operation systems.

ModuleStatusDescriptionKey Features
Vector3stableObject-oriented 3D vector class with operator overloadingVector math, operator support, object methods
Vec3UtilstableHigh-performance 3D vector utility functionsPerformance-optimized, no object creation
VecUtilstable2D vector utilities for XZ plane operations2D calculations, plane operations

String Processing

String manipulation, character speech, and text formatting capabilities.

ModuleStatusDescriptionKey Features
String UtilitiesstableCharacter speech, string formatting, text manipulationCharacter dialogue, special speech effects

Performance Monitoring

Performance analysis, profiling, and diagnostic utilities.

ModuleStatusDescriptionKey Features
Performance UtilitiesstableEntity monitoring, profiling, debugging diagnosticsEntity counting, server statistics, mod analysis

Simulation Helpers

Core utility functions for entity finding, position validation, and game simulation.

ModuleStatusDescriptionKey Features
Simulation UtilitiesstableEntity searching, position validation, vision checkingEntity finding, walkable positions, atlas management

Component Helpers

Utility functions supporting gameplay mechanics and entity management.

ModuleStatusDescriptionKey Features
Component UtilitiesstableEntity state management, world manipulation, specialized systemsBridge construction, inventory management, combat utilities

File Operations

File system utilities for persistent data operations.

ModuleStatusDescriptionKey Features
File UtilitiesstableAsynchronous file operations, existence checkingBulk file deletion, platform-specific optimizations

Common Usage Patterns

Mathematical Operations

-- Interpolation between values
local result = Lerp(start_value, end_value, progress)

-- Sine wave generation for animations
local wave = GetSineVal(frequency, absolute_value, entity_instance)

-- Distance calculations
local distance_sq = distsq(pos1, pos2)

Vector Operations

-- Object-oriented vector math
local direction = (target_pos - start_pos):GetNormalized()
local scaled_vector = direction * speed

-- Performance-optimized vector operations
local x, y, z = Vec3Util_Add(x1, y1, z1, x2, y2, z2)
local norm_x, norm_y, norm_z = Vec3Util_Normalize(x, y, z)

String Processing

-- Character-specific dialogue
local dialogue = GetString(character_inst, "DESCRIBE", "TREE")

-- String formatting and manipulation
local formatted_time = str_seconds(elapsed_time)
local capitalized = FirstToUpper(text_string)

Entity and Simulation Utilities

-- Find entities with specific criteria
local entity = FindEntity(inst, radius, filter_function, must_tags, cant_tags)

-- Position validation
local offset = FindWalkableOffset(position, angle, radius, attempts)

-- Performance monitoring
CountEntities()
local metadata = GetProfilerMetaData()

System Dependencies

Required Systems

Optional Systems

Performance Considerations

Optimization Strategies

  • Mathematical Operations: Use squared distance calculations to avoid expensive square root operations
  • Vector Operations: Choose between object-oriented Vector3 class and performance-optimized Vec3Util functions based on use case
  • Entity Searching: Utilize tag-based filtering and radius limitations to minimize search overhead
  • String Operations: Cache frequently accessed character dialogue and use efficient string manipulation functions
  • File Operations: Leverage asynchronous file operations and platform-specific optimizations

Memory Management

  • Vector utilities avoid object creation overhead for high-frequency calculations
  • String utilities implement efficient caching mechanisms for character speech
  • Performance utilities use circular buffers and controlled data collection
  • Table utilities provide in-place operations where possible

Scaling Considerations

  • Entity finding functions scale well with proper tag usage and radius constraints
  • Performance monitoring adapts to server size and player count
  • Mathematical utilities maintain consistent performance regardless of game state
  • File operations handle large datasets through asynchronous processing

Development Guidelines

Best Practices

  • Use appropriate utility functions for each domain (math, string, vector, etc.)
  • Choose between object-oriented and functional approaches based on performance requirements
  • Validate inputs to utility functions to ensure robustness
  • Leverage caching mechanisms where available to improve performance
  • Use squared distance calculations for proximity checks when exact distance isn't needed
  • Apply consistent error handling patterns across utility usage

Common Pitfalls

  • Creating unnecessary Vector3 objects in performance-critical loops
  • Using exact string matching instead of utility functions for character speech
  • Ignoring platform-specific optimizations in file operations
  • Performing expensive calculations repeatedly instead of caching results
  • Not validating entity existence before applying utility operations

Testing Strategies

  • Test mathematical utilities with edge cases (zero, negative, infinity)
  • Verify vector operations maintain mathematical correctness
  • Validate string utilities with different character types and special cases
  • Test performance utilities under various load conditions
  • Ensure file utilities handle missing files and permission errors gracefully

Utility Integration Workflows

Mathematical Calculations

  1. Basic Operations: Use mathutil for interpolation, rounding, and wave generation
  2. Vector Math: Choose Vector3 for readability or Vec3Util for performance
  3. Distance Checks: Employ squared distance functions for efficiency
  4. Angle Operations: Apply normalization functions for consistent angle handling

Entity Operations

  1. Entity Finding: Use simutil functions with appropriate tag filtering
  2. Position Validation: Apply walkable position checks for placement operations
  3. State Management: Leverage componentutil for entity state operations
  4. Performance Monitoring: Implement perfutil for debugging and optimization

Data Processing

  1. String Handling: Use stringutil for formatting and character-specific operations
  2. Table Operations: Apply util functions for manipulation and data structures
  3. File Management: Implement fileutil for persistent data operations
  4. Memory Analysis: Use performance utilities for resource monitoring
SystemRelationshipIntegration Points
Fundamentals CoreFoundationEntity system, class framework
ActionsConsumerAction validation and execution
AI SystemsConsumerEntity finding and position validation
Game MechanicsConsumerMathematical calculations and utilities

Troubleshooting

Common Utility Issues

IssueSymptomsSolution
Vector calculations incorrectWrong positions or directionsVerify vector operation order and normalization
Performance degradationSlow entity operationsUse squared distance and tag filtering
String formatting errorsIncorrect character speechCheck character type and string table existence
File operation failuresSave/load errorsVerify file permissions and path resolution

Debugging Utilities

  • Use performance utilities to monitor entity counts and system load
  • Apply debug printing functions to trace utility function calls
  • Leverage vector utilities to validate mathematical operations
  • Employ file utilities to verify persistent data integrity

Migration and Compatibility

Utility Updates

When updating utility functions:

  • Maintain backward compatibility for widely-used functions
  • Provide migration paths for changed APIs
  • Test utility changes across all dependent systems
  • Document performance impact of utility modifications

Best Practices for Updates

  • Test mathematical utilities with known values to verify correctness
  • Validate string utilities with all supported character types
  • Ensure vector utilities maintain mathematical properties
  • Verify file utilities work across all supported platforms

Contributing to Utilities

Adding New Utilities

  1. Determine appropriate module for new functionality
  2. Follow established patterns for parameter validation and error handling
  3. Provide comprehensive examples and usage documentation
  4. Consider performance implications and provide optimized alternatives when needed
  5. Test across different game scenarios and edge cases

Documentation Standards

  • Include clear examples for all utility functions
  • Document performance characteristics and trade-offs
  • Provide migration guidance for deprecated functions
  • Maintain consistency with existing utility documentation patterns

Code Review Checklist

  • Utility functions handle edge cases appropriately
  • Performance characteristics are documented and acceptable
  • Function signatures follow established patterns
  • Error handling is consistent with module standards
  • Examples demonstrate realistic usage scenarios
  • Integration with existing systems is validated