Load and run cppia scripts in hxcpp games. Scripts are compiled Haxe, so they run much faster than interpreted scripting and can use the game's own classes.
- Entry classes: the class named like the file,
MainorScriptis created when the script starts, or the script'smainruns - Calls into every script instance, static calls, fields
- Groups with priorities, stop values, folder loading and hot reload
- Error reporting with the script path, the function and the stack
haxelib run cppiatool to create and build scripts- Header checks in C++ before a file reaches hxcpp, so a broken or wrong file reports an error instead of crashing
- FNV-1a content hashes and native file stamps, so hot reload only fires when a script really changed
- A profiler with calls, total, average and max time per function, timed with a nanosecond clock
- Safe no-ops on targets without cppia support
The haxelib is called hxcppia: a library named cppia would define cppia and make Haxe compile the whole game as a cppia script.
haxelib git hxcppia https://github.com/BrenninhoTools/hxcppia
The game must be built with scriptable. dll_export writes the list of game classes that scripts can use without compiling them again:
--macro cppia.macro.Host.setup('export_classes.info', ['my.game'])
or by hand:
-D scriptable
-D dll_export=export_classes.info
Keep the classes scripts need from being removed by dead code elimination, for example with -dce std or the keep list above.
import cppia.Cppia;
import cppia.CppiaGroup;
Cppia.onError = function(error) showError(Cppia.describe(error));
var scripts = new CppiaGroup();
scripts.hotReload = true;
scripts.loadFolder('mods/scripts');
scripts.call('onCreate');
function update(elapsed:Float)
{
scripts.update();
scripts.call('onUpdate', [elapsed]);
}A single file:
var module = Cppia.load('mods/scripts/Boss.cppia');
module.call('onBeat', [16]);
module.set('speed', 2);
module.callStatic('Boss', 'spawn', []);
module.destroy();Cppia.profiling = true;
scripts.call('onUpdate', [elapsed]);
showText(scripts.profileReport());
scripts.resetStats();Cppia.fileFormat('mods/scripts/Boss.cppia');
Cppia.hashFile('mods/scripts/Boss.cppia');
Cppia.version(bytes);
Cppia.now();haxelib run hxcppia new MyScript scripts/MyScript
class MyScript extends cppia.CppiaScript
{
public function new()
{
super();
}
public function onCreate():Void {}
public function onUpdate(elapsed:Float):Void {}
}Build it with the game's export_classes.info next to it:
haxelib run hxcppia build MyScript -cp scripts/MyScript -o MyScript.cppia --host export_classes.info
Scripts built against an older export_classes.info should be rebuilt when the game's classes change.
MIT