Skip to content

Scripting

Michael Yarichuk edited this page Oct 18, 2026 · 1 revision

Scripting (optional)

dotnet add package RoguelikeToolkit.Entities.Scripting.Jint

Setup

using RoguelikeToolkit.Entities.Scripting.Jint;

EntityScripting.Register();   // once at startup, BEFORE spawning templates with script members

Without this, spawning a template with a script member throws InvalidCastException naming the script type.

Declare script members

[Component]
public class Behavior
{
    public EntityScript? OnHit { get; set; }
    public EntityComponentScript? Regen { get; set; }
}
Components:
  Behavior:
    OnHit: |
      var h = entity.GetComponent(Health);
      h.Current -= damage;
      entity.SetComponent(Health, h);
    Regen: component.Current += amount;

Execute

using var host = new ScriptHost(new ScriptHostOptions { ReferenceTypes = new[] { typeof(Health) } });

var b = goblin.Get<Behavior>();
b.OnHit!.ExecuteOn(host, ref goblin, ("damage", 4));
b.Regen!.TryExecuteOn<Health>(host, goblin, ("amount", 2));

Things to know

  • Scripts are immutable and shared; keep state in components.
  • One ScriptHost per thread/simulation loop (not thread-safe). Scripts themselves are.
  • Expose component types to scripts via ScriptHostOptions.ReferenceTypes.
  • Value-type components come back as copies: write them back with SetComponent.
  • Defaults: 500 statements, recursion depth 1024. Timeout and MemoryLimitBytes are opt-in (leave memory limit off on IL2CPP).

Clone this wiki locally