Skip to main content

Script Execution System

The ScriptExecutionSystem is a fundamental part of the Zygarde engine’s architecture that orchestrates the execution of scripts associated with game entities. It ensures that each script’s lifecycle methods, such as updates and collision handling, are called in the correct order, enabling dynamic behavior and interaction within the game.

Overview

The ScriptExecutionSystem processes MonoBehaviour scripts that are part of the ScriptPool. It manages their execution by calling the FixedUpdate method on each script and handles collision events, allowing scripts to respond to game physics and interactions. This system is critical for implementing gameplay mechanics and scripted behaviors.

Key Concepts

ConceptDescription
Script PoolA collection of MonoBehaviour scripts that are executed in the context of game entities, managed by the ScriptExecutionSystem.
Scripting ContextAn object that provides the necessary context and state information to scripts during their execution, including references to the entity and delta time.
Collision HandlingThe system processes collision events and invokes the appropriate callbacks in scripts, allowing for interactive gameplay scenarios.

Core Functionality

  1. Script Execution: The ScriptExecutionSystem calls the FixedUpdate method for each script in the pool, allowing scripts to perform updates based on the current game state.
  2. Collision Management: The system checks for collisions and triggers the OnCollisionEnter method in the relevant scripts, enabling entities to react to physical interactions.

Process Flow

  1. Run: The Run method iterates through all registered script pools in the game registry. For each pool, it invokes ProcessScriptPool to handle the execution of the scripts contained within.
  2. ProcessScriptPool: This method creates a ScriptingContext for the current execution and iterates through the scripts in the pool, calling their FixedUpdate methods and handling any collisions.
  3. HandleCollisionCallback: This function checks for collisions related to the current entity and calls the OnCollisionEnter method for scripts when a collision is detected, ensuring that scripts can respond appropriately.

Example Usage

#include "script_execution_system.hpp"
#include "registry.hpp"
#include "utils/timer/timer.hpp"

void exampleUsage() {
// Create a timer for delta time
utils::Timer timer;

// Create an instance of ArchetypeManager
auto archetypeManager = std::make_shared<core::archetypes::ArchetypeManager>();

// Create an instance of ScriptExecutionSystem
zygarde::scripting::systems::ScriptExecutionSystem scriptExecutionSystem(timer.GetDeltaTime(), archetypeManager);

// Create a registry instance
Registry::Ptr registry = std::make_shared<Registry>();

// Create script pools (assuming you have some pools already populated)
zipper<scripting::components::ScriptPool> scriptPools;

// Run the script execution system
scriptExecutionSystem.Run(registry, scriptPools);
}