crystal.Behavior

Behaviors are components which manage a Script and automatically add it to their entity’s ScriptRunner.

The recommended usage pattern is to define components that bundle specific logic by inheriting from crystal.Behavior. The example on this page follows this pattern to define a component which makes an entity print a string every second.

Entities with Behavior components should also have a ScriptRunner component. The ECS owning these entities must operate a ScriptSystem.

When a Behavior component is removed from an entity, all its threads are stopped.

Constructor

Like all other components, Behavior components are created by calling Entity:add_component.

When inheriting from crystal.Behavior and writing a constructor for your component class, you must call the super constructor. The super constructor supports one optional argument: a function that contains initial logic for the script to run. Specifying this argument is equivalent to calling self:script():add_thread(f).

Methods

Name Description
script Returns the Script managed by this behavior.

Examples

The two examples below are equivalent. They both define a Behavior which prints "Hello" every second.

local HelloPrinter = Class("HelloPrinter", crystal.Behavior);

HelloPrinter.init = function(self)
  HelloPrinter.super.init(self, function(self)
    while true do
      print("Hello");
      self:wait(1);
    end
  end);
end
local HelloPrinter = Class("HelloPrinter", crystal.Behavior);

HelloPrinter.init = function(self)
  HelloPrinter.super.init(self);
  self:script():add_thread(function(self)
    while true do
      print("Hello");
      self:wait(1);
    end
  end);
end

This site uses Just the Docs, a documentation theme for Jekyll.