Native Scripting
Overview
SafiEngine uses a native C++ behavior system for gameplay logic. Instead of external scripting languages, you write behaviors as C++ classes that extend the Behavior base class. Behaviors have full access to the engine, input, physics, audio, and all ECS components.
For comprehensive scripting tutorials, see the Scripting Guide.
Architecture
Creating a Behavior
- Create a
.cppfile inCore/src/behaviors/ - Define a class extending
Behavior - Export a registration function
- Add it to
BehaviorRegistry::RegisterAll()inBehavior.cpp - Add the file to
Core/CMakeLists.txt
Example: Player Controller
Example: Follow Camera (using CameraUtils)
Attaching to an Entity
- Select an entity in the Scene Hierarchy
- Click Add Component > NativeScript
- Select a behavior from the Behavior dropdown
- Press Play to test
The behavior's OnStart is called once when play mode begins. OnUpdate is called every frame with the delta time.
Script Properties
Behaviors can read configurable properties set in the Inspector. Properties are string key-value pairs stored on the NativeScript component:
- Type a property name and click + Add in the Inspector
- Read them in
OnStartviae.get<NativeScript>()->properties - Remove a property by right-clicking it
Properties are saved with the scene and allow you to configure behavior parameters without recompiling.
See Script Properties for details.
Available Engine APIs
Inside OnUpdate, you have full access to:
You can also read and modify any ECS component on the entity:
Editor Integration
The New Behavior button in the NativeScript inspector creates a template .cpp file in your project's assets/scripts/ directory. After writing the behavior logic, add the file to CMake and rebuild.
Integration Notes
- Behaviors are instantiated when play mode starts and destroyed when it stops
- Scene snapshot/restore handles
behaviorNameandproperties; the runtime instance is recreated fresh each play session - Each entity gets its own behavior instance -- state is per-entity
- New behaviors require a rebuild since they are compiled C++