How to write C++ gameplay scripts for a JasperEngine project. Served as
jasper://docs/cpp-scripting. The runtime API of a project is the header snapshot in
<project>/.jasperengine/engine-api/ (copied from the editor at open/build): it is the only
source of truth for class and method names. Never carry over names from Unity C#, Unreal or
Godot.
| Kind | Base class | Use it for |
|---|---|---|
| Script class | jasperengine::ScriptBase |
general logic: game controllers, spawners, input |
| GameObject subclass | the GameObject's own class: Mesh, Primitive, Sprite, Model, Camera, GameObject, ... |
calling that class's methods on this (setColor, setPosition, getPosition) |
ScriptBase has getScene(), getEntity(), isActiveAndEnabled(), isEnabled() /
setEnabled(bool) (Unity's enabled: calls onEnable/onDisable), getScript<T>() (the first
C++ script of this GameObject that is a T, enabled or not, else nullptr; Unity's
GetComponent<T>()) and the lifecycle virtuals below; it does NOT have mesh or transform
methods. Wrap other GameObjects explicitly (GameObject(scene, entity), Sprite(scene, entity)).
Scene-wide finds are Lua-only (GameObject.findObjectOfType); in C++ walk
Engine::getSceneLayers().
No reflection or declaration macro is needed (no GENERATED_BODY, GDCLASS, Q_OBJECT,
D_OBJECT): inherit the base class and declare a (jasperengine::Scene*, jasperengine::Entity)
constructor. Prefer editing the skeleton the script creation tool generated.
// Spinner.h
#pragma once
#include "ScriptBase.h"
#include "Engine.h"
#include "ScriptProperty.h"
class Spinner : public jasperengine::ScriptBase {
public:
DPROPERTY("Speed")
float speed = 90.0f;
DPROPERTY("Target") // a GameObject reference set in the Inspector
jasperengine::GameObject* target = nullptr;
DPROPERTY("Bullet") // a Prefab dragged from the Project window
jasperengine::PrefabAsset bullet;
Spinner(jasperengine::Scene* scene, jasperengine::Entity entity);
virtual ~Spinner();
void onUpdate();
};// Spinner.cpp
#include "Spinner.h"
#include "GameObject.h"
#include "Input.h"
#include "Log.h"
using namespace jasperengine;
Spinner::Spinner(Scene* scene, Entity entity): ScriptBase(scene, entity) {
REGISTER_ENGINE_EVENT(onUpdate);
}
Spinner::~Spinner() {
UNREGISTER_ENGINE_EVENT(onUpdate);
}
void Spinner::onUpdate() {
float dt = Engine::getDeltatime();
if (target) {
Vector3 p = target->getPosition();
target->setPosition(p + Vector3(speed * dt, 0, 0));
}
if (Input::isKeyPressed(D_KEY_SPACE) && bullet.isValid()) {
GameObject go = GameObject::instantiate(bullet, Vector3(0, 5, 0), Quaternion());
// false when there is none: the prefab missing, or its init destroyed it at once
if (go) Log::print("spawned %s", go.getName().c_str());
}
}A ScriptBase script overrides the methods it needs; the engine calls only the overridden
ones, with the rules and order of Lua scripts (jasper://docs/lua-api, 1.1):
| Override | When | Unity |
|---|---|---|
void init() override |
once, the first time the GameObject is active in hierarchy, enabled or not; after the DPROPERTY members are bound | Awake |
void onEnable() override |
it becomes active and enabled (after init) |
OnEnable |
void start() override |
once, at the start of the frame after the first onEnable |
Start |
void onDisable() override |
it stops being active and enabled; before onDestroy |
OnDisable |
void onDestroy() override |
once: destroy, scene unload, Play stop / quit (only if init ran); the destructor runs after it |
OnDestroy |
void onApplicationPause(bool paused) override, onApplicationFocus(bool focused), onApplicationQuit() |
app background / focus / quit | OnApplication* |
void onJointBreak2D(jasperengine::Joint2D& joint) override |
a 2D joint of this GameObject (or whose bodyA is it) broke | OnJointBreak2D |
void onParticleSystemStopped() override |
a Particle System of this GameObject with stop action Callback finished | OnParticleSystemStopped |
Overrides may be public, protected or private; do not call the base method: it only notes the
script does not override the message, which is then not sent to it again. The constructor runs
first, before the properties are bound: read DPROPERTY members in
init(), not in the constructor. Events registered with REGISTER_ENGINE_EVENT start after
start() (a script enabled during a frame gets its first onUpdate in the next one).
Migration: a method of an existing script named like one of these is now an override the engine
calls (twice if the script calls it itself too); one with another signature (bool init()) no
longer compiles: rename it. Every Script component entry gets its object, enabled or not
(disabled C++ scripts are compiled too). Exceptions thrown from these methods are logged and
the loop goes on. The GameObject-subclass kind (below) is an object wrapper, not a behaviour:
it gets no lifecycle calls.
void Spinner::init() { if (!target) Log::warn("Spinner: no target"); }
void Spinner::onDisable() { Log::print("paused"); }Persistent objects (Unity's DontDestroyOnLoad, rules in jasper://docs/lua-api 2.8.1):
void AdsManager::init() {
if (persistSingleton<AdsManager>() != this) return; // a duplicate: its GameObject goes
}
void MusicPlayer::init() { dontDestroyOnLoad(); } // the root GameObject survives loadsisPersistent(), SceneManager::dontDestroyOnLoad(scene, root) and
SceneManager::moveGameObjectToScene(from, root, to) do the same from anywhere. The move keeps
the object (its members, pointers to it stay valid); getScene() and getEntity() change with
it, and the wrappers its entity-reference properties made for the moved GameObjects follow.
A GameObject subclass looks the same with public jasperengine::Primitive (or Mesh,
Sprite...) as base, #include "Primitive.h", and Primitive(scene, entity) in the
initializer list; it can then call setColor(...), getPosition(), setPosition(...) directly.
DPROPERTY("Display Name") before a public member exposes it in the Inspector. Supported
member types: bool, int, float, std::string, Vector2, Vector3, Vector4,
PrefabAsset, and pointers to engine wrapper classes or other script classes
(GameObject*, Mesh*, Sprite*, MyOtherScript*) for GameObject references. Colors:
DPROPERTY("Tint", Color4) Vector4 tint = Vector4(1,1,1,1); (or Color3 with a Vector3).
| Macro (in the constructor) | Pair in the destructor | Calls |
|---|---|---|
REGISTER_ENGINE_EVENT(onUpdate) |
UNREGISTER_ENGINE_EVENT(onUpdate) |
every frame |
REGISTER_ENGINE_EVENT(onFixedUpdate) |
UNREGISTER_ENGINE_EVENT(onFixedUpdate) |
every physics step |
REGISTER_UI_EVENT(onClick, method) |
UNREGISTER_UI_EVENT(onClick, method) |
a click: released over the element |
REGISTER_BUTTON_EVENT(onPress, method) |
UNREGISTER_BUTTON_EVENT(onPress, method) |
Button events: onClick is the button's click (released over it, or a Submit while selected); onPress its pressed state; onRelease fires on every release, wherever the pointer is, so it is not a click |
REGISTER_UI_EVENT(event, method), REGISTER_SCROLLBAR_EVENT, REGISTER_PANEL_EVENT |
UNREGISTER_... |
UI component events |
REGISTER_COMPONENT_EVENT(Component, event, method) |
UNREGISTER_COMPONENT_EVENT(...) |
any component's event |
The method name must be the Engine event's name (onUpdate, onFixedUpdate,
onPostUpdate, onKeyDown, ...). Events of a disabled script (inactive GameObject or
disabled entry) are skipped. Registering in the constructor or in init() both work.
onCharInput passes one whole typed character: declare it void onCharInput(char32_t character).
The older form void onCharInput(wchar_t) still compiles but is deprecated: on Windows wchar_t is
16 bits, so a character beyond U+FFFF (an emoji) arrives cut to its low 16 bits there.
Keep the language of the project's existing scripts. A project without scripts gets Lua unless the
user asks for C++: Lua runs in the prebuilt players with no compiler, C++ makes every export a
compiled build (jasper://docs/shipping). Before the first C++ script read get_build_info
cpp_scripts: can_build is the toolchain check Play runs (on Windows, a compiler compatible with
this editor), missing_tools says what is missing. Switch to Lua, and tell the user why, when the
tools are missing, when a build fails for reasons outside your code (CMake does not configure, the
compiler or linker does not run, the editor's compatibility check fails), or when your code still
does not compile after two fixes.
C++ scripts have no coroutines (C++20 co_await is not used: projects build with C++17 too);
the Lua game's coroutines port to timers or to state in onUpdate. A ScriptBase script:
// in a method (the constructor too): once in 2 s, then every 0.5 s from 1 s
invoke([this]() { spawn(); }, 2.0f);
TaskId blink = invokeRepeating([this]() { toggle(); }, 1.0f, 0.5f);
invoke([this]() { resumeMenu(); }, 0.3f, TimeMode::Unscaled); // runs at timeScale 0
cancelInvoke(blink); // false when it is not this script's or already done
cancelAllInvokes();
bool pending = isInvoking(); // or isInvoking(id)The timers belong to the script: they keep running while it is disabled or its GameObject is
inactive and are cancelled when the script is deleted (~ScriptBase: GameObject destroyed,
scene unloaded, Play stop). Callbacks run on the main thread after onUpdate (with the Lua
timers, in creation order, at most once a frame; a late repeat is rescheduled from now). A C++
exception a callback (timer or event) throws is caught: in the editor's Play it stops Play like a
Lua error ("Script crash ... C++ exception : "), in a game it is logged and the game
goes on (jasper://docs/crash-diagnostics, "A callback that fails"); on the Web it ends the game. Code that is not a script: #include "Timer.h",
jasperengine::Timer::after(2.0, fn), Timer::every(0.5, fn, TimeMode::Scaled, owner, count),
Timer::cancel(id) (or ScriptScheduler::after/every from "ScriptScheduler.h"; an owner
pointer makes ScriptScheduler::cancelOwner(owner) end them, unowned ones end at Play stop).
Time under Unity's names: #include "EngineTime.h", jasperengine::Time::getDeltaTime() (the
fixed step inside onFixedUpdate), getUnscaledDeltaTime(), getFixedDeltaTime(), getTime(),
getUnscaledTime(), getRealtimeSinceStartup(), getTimeSinceLevelLoad(), getTimeScale()...;
or Engine::getDeltatime(), Engine::getUnscaledDeltatime(), Engine::getUpdateTime() (the
fixed step; Engine::getFixedDeltatime() is a fixed frame delta, Unity's captureDeltaTime).
- PlayerPrefs:
jasperengine::UserSettings("io/UserSettings.h", flat include"UserSettings.h"):getIntegerForKey(k, default),setIntegerForKey(k, v), the float, double, long, bool, string andDataversions,hasKey,deleteKey,deleteAll,save. The same keys as LuaPlayerPrefs/UserSettings(jasper://docs/lua-api, 2.4). - Files:
jasperengine::FileUtils("FileUtils.h"):readText(path, out, error),writeText(path, text, error)(atomic),appendText,exists,remove,makeDirs,list, with the LuaFile.*path rules (data://writable;asset://,lua://read-only). - JSON: nlohmann json 3.11 is on the include path (
#include "json.hpp",nlohmann::json);nlohmann::json::parse(text)andj.dump(). To hand JSON to Lua or take it from Lua usejasperengine::LuaJson("LuaJson.h":toJson,push,encode,decode, the same rules as the LuaJsonmodule).
jasperengine::ShaderGlobals ("shader/ShaderGlobals.h", flat "ShaderGlobals.h"), the Lua
Shader.setGlobal* (jasper://docs/shaders#global-values), main thread only:
int id = ShaderGlobals::propertyToID("FogTint") once, then ShaderGlobals::setColor(id, Vector4(1, 0.5f, 0, 1)) (sRGB in), setFloat, setVector, setMatrix, setFloatArray(id, values, count), setVectorArray, setTexture(id, Texture("textures/noise.png")),
clearTexture, get(id, value) (a ShaderGlobalValue: type, linear values, texture),
exists, getNames(), resetToDefaults(). A renderer's parameter follows a global with
setShaderGlobalBinding(member, global) (Image, Text, Polygon, Points, LineRenderer,
TrailRenderer, Skybox; a mesh: Material::shaderGlobalBindings with
ShaderGlobals::setBinding, then setMaterial).
The subscriptions belong to the script (this): when the script goes away they are removed even
without the UNREGISTER_* (and its timers cancelled). Timers: #include "util/Scheduler.h",
Scheduler::game().after(2.0, [this]() { ... }, TimeDomain::Game, {.ownerKey = ownerKeyOf(this)}),
or keep the handles in a TimerGroup member. Logging with a category and the call's file:line:
JE_LOG_WARN(logCategoryScript(), "hp %d", hp) ("log/Logger.h").
- Includes are quoted and resolve against the engine API snapshot:
"Engine.h","Input.h","Log.h","GameObject.h","Mesh.h","component/ScriptComponent.h". Never#include <core/...>. - Logging:
Log::print("pos %f %f", p.x, p.y),Log::warn,Log::error(printf format). Neverprintf,std::coutorEngine::log(it does not exist). - Key codes are
D_KEY_*macros (D_KEY_SPACE,D_KEY_LEFT); Lua usesInput.KEY_*. - Input as in Lua (jasper://docs/input):
Input::getKeyDown(D_KEY_SPACE),Input::getTouch(0), and the input actions of Project Settings > Input with#include "input/InputActions.h":InputActions::getAxis("Horizontal"),InputActions::getButtonDown("Jump"). A source the script feeds (InputActions::setSourceValue(reinterpret_cast<uintptr_t>(this), "Horizontal", 1.0f, InputSourceKind::Script)) is cleared when the script is deleted. - Angles are in DEGREES by default (
Engine::setUseDegrees(false)switches):Quaternion(angle, axis)takes the angle first;Quaternion::fromEulerAngles,Camera::rotateViewall go throughAngle::defaultToRad.std::sin/coswant radians: convert withAngle::defaultToRad,Angle::degToDefault,Angle::radToDefault. - Physics: continuous forces in
onFixedUpdate, not multiplied by deltatime; teleport withRigidbody::setPosition/setRotationorRigidbody2D::setPosition/setAngle, never by writing the GameObject's transform fromonFixedUpdate. Seejasper://docs/physics. Rigidbody::setMassonly writes the live Jolt body and is lost on reload: author density.- Box2D and Jolt directly, where the engine's classes stop:
#include "box2d/box2d.h", and<Jolt/Jolt.h>before any other Jolt header. The handles:Rigidbody2D::getBox2DBody(),getBox2DShape(i), the scene'sPhysicsSystem::getWorld2D(),Rigidbody::getJoltBody(). Every public Box2D and Jolt function links, in Play and in builds (the editor's engine library exports all of both); the generatedCMakeLists.txtpasses the macros Jolt's headers need.
- Script files (
.h/.cpp) can live anywhere in the project outside.jasperengine/. The editor discovers the classes attached to GameObjects and compiles them into the project library (editor Play) or the game (export). - Script directories (Project Settings > Directories,
scriptDirsin project.yaml) are compiled whole and are include roots:<root>/Player/Thing.his included as"Player/Thing.h". Shared helper classes belong there. A.cppneither under a root nor included by an attached script is not compiled at all. Discovery skips hidden directories and directories namedbuild. - The editor runs a C++ build only when a C++ script (enabled or not) is attached to some GameObject: in a Lua-only project helper sources are first compiled by an export.
- Never edit the generated
CMakeLists.txt(regenerated every build). It can also build the project as a standalone executable against the editor's engine library (cmake -S <project>;-DJASPERENGINE_LIB_DIR=<the editor's folder>names another editor's): it always compiles with that library's layouts and stops when the folder holds a static engine build. Custom settings go inProjectBuild.cmakeat the project root, attached to${JASPERENGINE_TARGET}with paths from${JASPERENGINE_SCRIPTS_DIR}:target_include_directories(${JASPERENGINE_TARGET} PRIVATE ${JASPERENGINE_SCRIPTS_DIR}/Source/Public) target_link_libraries(${JASPERENGINE_TARGET} PRIVATE mylib)
- The C++ standard is a project setting (
cxxStandard: 20, the default, or 23); do not setCMAKE_CXX_STANDARDyourself. The engine and its API headers need C++20. The compilers that build it: GCC 11, Clang 16, Apple Clang 15, MSVC 19.30 (Visual Studio 2022) or newer; the generatedCMakeLists.txtstops with a message naming the compiler below those. C++23 exports to Xcode need Xcode 16 or newer. - Code that must build with a physics backend disabled in the export guards on
JASPERENGINE_PHYSICS_2D/JASPERENGINE_PHYSICS_3D. - Verify:
compile_scriptsbuilds the scripts without playing and returns the compiler errors asfile:line:column: message(project-relative): fix, compile again, then play and read the Console for runtime failures ("Script crash in scene ..." entries). Play builds too (incrementally after a compile_scripts), and a compile error then shows in the Console.
A project saved with cxxStandard: 17 opens as C++20: the editor logs one Console warning
(Project C++ standard 17 is no longer supported; the project now builds with C++20 ...) and the
next Play or compile_scripts builds at C++20. Its compile errors show the usual way. Most
C++17 code compiles unchanged; what C++20 changed:
u8"..."literals areconst char8_t[]: they no longer convert toconst char*orstd::string. Drop theu8prefix (sources are UTF-8) or copy the characters over.std::filesystem::path::u8string()andgeneric_u8string()returnstd::u8string, andstd::filesystem::u8path()is deprecated. Convert withstd::string(s.begin(), s.end()), and build paths from UTF-8 withstd::filesystem::path(std::u8string(text.begin(), text.end())).[=]in a member function capturingthisimplicitly is deprecated: write[=, this](or[this], or[*this]for a copy).- Compound assignment and
++/--on avolatilevariable are deprecated: writex = x + y. - A class with a user-declared constructor (even
= defaultor= delete) is no longer an aggregate:T{1, 2}needs a matching constructor, or remove the declared ones. - Arithmetic and bitwise operators between two different enumeration types, or between an enumeration and a floating-point value, are deprecated: cast to the underlying type first.
char8_t,concept,requires,consteval,constinit,co_await,co_yieldandco_returnare keywords and can no longer name things.a == balso triesb == a: a non-constmemberoperator==can become ambiguous. Make itconst, or a free function with symmetric parameters.
Every export builds with the oldest standard libraries the engine supports (GCC 11's, and
libc++ for macOS 10.15 and iOS 13), so scripts that ship to every platform stay off <format>,
std::jthread and stop tokens, <semaphore>/<latch>/<barrier> and std::atomic waits,
<memory_resource>, floating-point std::to_chars/from_chars, <syncstream>, <chrono>
calendars and time zones, coroutines and modules.
create_script(kindcpp_script= ScriptBase subclass,cpp_subclass= subclass of the GameObject's class, needsentity) writes the editor's template (or yourheader_content/source_content) and can attach it.attach_scriptattaches an existing.h/.cpppair,update_script_entryrepoints / reorders / enables,detach_scriptremoves it.set_script_propertiessets DPROPERTY values on a GameObject ({"speed": 300});list_scriptsshows them. A new DPROPERTY appears once the saved header is re-parsed: before your next tool call (listed in itsexternal_changes).- Edit sources with your own file tools (clangd reads the generated
.clangd, which points it at.jasperengine/clangd/compile_commands.json; engine headers are in.jasperengine/engine-api/). Thencompile_scripts, orjasperengine-editor checkfrom a shell.write_file/read_file/search_filesare for clients without file access.