Scripting
The two main ways of writing game logic is either in custom C++ modules or through scripting. C++ is more suitable for computationally heavy tasks while scripting is an easy way of iterating quickly.
To add a script asset to an entity in your scene, see the: Assets documentation
Hot reload
As with all assets, changes saved to script files are hot reloaded. Any change to a script, even when live testing on your phone using the built-in QR code hosting, will be instantly reflected.
Language support
Currently, Hiber3D supports JavaScript as the primary scripting language. Additionally, Lua scripting is available as an experimental option.
Script template
The easiest way to begin scripting is to, from inside the editor, create a new .js script file.
This is done by right clicking inside the assets panel, then pressing New File...
, and naming the new file with the .js
extension.
This new file will automatically include both a template of the default script functions and some brief documentation on the basic scripting API.
Default script functions
When creating a new .js
file, you will get the following default script structure. Note that scripts only run while in play mode.
({
onCreate() {
},
update(deltaTime) {
},
onEvent(event, payload) {
}
});
onCreate()
This function runs only once, when the script is initialized.
update(deltaTime)
This functions runs once every tick.
The deltatime
number is the time in seconds elapsed during the previous tick.
onEvent(event, payload)
This function runs once for each subscribed to event.
The event
string is the name of the event that just fired.
payload
is an object containing the members of the event.
See: “Event management” further down this page and C++ Modules - C++ registration
Hiber3D API
Here are some commonly used Hiber3D functions:
Print to console
hiber3d.print(...) // use JSON.stringify for object
Entity management
const newEntity = hiber3d.createEntity()
hiber3d.destroyEntity(entity)
Script management
hiber3d.addScript(entity, "path/to/script.js")
hiber3d.removeScript(entity, "path/to/script.js")
hiber3d.hasScript(entity, "path/to/script.js")
const script = hiber3d.getScript(entity, "path/to/script.js")
script.someField = 2;
Component management
hiber3d.addComponent(this.entity, "Hiber3D::Transform")
hiber3d.removeComponent(this.entity, "Hiber3D::Transform")
hiber3d.hasComponents(this.entity, "Hiber3D::Transform", "Hiber3D::Renderable")
hiber3d.findEntitiesWithComponent("Hiber3D::Transform")
hiber3d.setValue(this.entity, "Hiber3D::Transform", "position", "x", 1)
const transform = hiber3d.getValue(this.entity, "Hiber3D::Transform")
Singleton management
hiber3d.setValue("Hiber3D::ActiveCamera", "position", {x:1, y:2, z:3})
const position = hiber3d.getValue("Hiber3D::ActiveCamera", "position")
Call C++ functions
hiber3d.call("myFunction", 1)
See: C++ Modules - C++ registration
Input management
hiber3d.call("keyIsPressed", 1)
hiber3d.call("keyJustPressed", 1)
hiber3d.call("keyJustReleased", 1)
Note: Find key codes in \<Hiber3D/Core/KeyEvent.hpp\>
Event management
hiber3d.addEventListener(this.entity, "EventName") // done in onCreate()
hiber3d.writeEvent("EventName", {});