Building worlds

Logic: scripts and graphs

Make things happen with Rhai scripts or visual logic graphs.

Omnion has two ways to add behaviour, and you can mix them: scripts written in Rhai and visual logic graphs, like Blueprints. Logic graphs turn into Rhai code, and the editor shows you the code it generates.

Scripts

Add one with Add Component → Script, or create a .rhai file with + New → Script and drag it onto an object. A script can define these functions:

FunctionWhen it runs
start()When the game starts
update(dt)Every frame (dt is the time since the last frame, in seconds)
on_hit(other)When this object collides with another
on_overlap(other)When something enters this trigger
on_overlap_end(other)When it leaves

Inside them, this.position, this.rotation, this.scale and this.velocity (arrays [x, y, z]), this.visible, this.name, this.tags and the object’s variables are available. other is the name of the other object.

fn start() {
    this.base_y = this.position[1];
}

fn update(dt) {
    // Spin slowly and float up and down.
    this.rotation[1] += 45.0 * dt;
    this.position[1] = this.base_y + 0.25 * sin(time() * 2.0);
}

fn on_overlap(other) {
    if has_tag(other, "Player") {
        print("Coin collected!");
        destroy(this.name);
    }
}

There are functions for keys, time, positions, vectors, random, tags, other objects’ variables (get_var, set_var), destroy, stop_game, the game UI and print. The full reference is next to the code editor, and errors show up in the Output Log.

The code editor

The Code panel (in the Logic workspace, or Window → Code Editor) has a tab per file, Rhai highlighting, completion for the engine API and for what your script defines (Ctrl+Space), errors marked on their line, go to definition (F12 or Ctrl+click) and find and replace (Ctrl+F / Ctrl+H). Ctrl+S saves, and the change reaches the objects that use the script right away, even while the game is running.

Logic graphs

Logic graphs open in their own tab, with Play, Pause and Stop at the top. While the game runs, nodes light up as they execute, wires that carry something animate and variables show their live values.

  • Events: Start, Update, keys, timer, Hit, Begin/End Overlap, UI button.
  • Control: Branch, If Key Held, If Near.
  • Actions: move, rotate, position, velocity, jump, visible, print, variables, UI text, destroy, stop the game, play sound and animation.
  • Data: variables, position, time, keys, tags, math, comparisons, logic, vectors, distance, random and text.
  • Simple AI: Move Towards and Look At.

White wires carry execution and colored wires carry data. An input without a wire is typed into the node itself. Right-click, or drop a wire in an empty spot, to open the node search (the new node comes already connected). Drag variables, levels and objects from the Outliner into the graph.

Timeline node

A Timeline node holds an editable curve with Update, Finished, Value and Time outputs. With Lerp, Lerp Vector, Set Position, Set Rotation or Set Scale it animates anything.

Levels and global variables

Level Logic (in the toolbar or Tools) runs as a “Level” object with its own variables, like Unreal’s Level Blueprint.

Edit → Project Settings lists your levels in order, which one the game starts with, and global variables that carry over from level to level. Use Open Level, Next Level, Restart Level, Get Level Name and Get/Set Global Variable to move between them.

Variables and tags

  • The Variables component holds numbers, yes/no values, text or vectors, with their starting values.
  • Tags (under the name in Details), like Player or Enemy, let logic recognise objects. The coin in the Content Browser is a complete example: a trigger and a graph that hides it when the player touches it.