Creating a Behavior
Behaviors are classes that control a Unit. For example, health items have the Pickup behavior. This pickup behavior responds to the Collide
function, so that it can perform an Effect when a Player touches the item.
Contents
Minimal behavior
The most basic behavior is one that does nothing and simply stores the UnitPtr:
class MyBehavior { UnitPtr m_unit; MyBehavior(UnitPtr unit, SValue& params) { m_unit = unit; } }
Parameters
You can define parameters in your Unit file, and then fetch them in your Behavior script. Take for example the following integer in the Behavior XML:
<behavior class="MyBehavior"> <int name="move-time">1000</int> </behavior>
Then, to get the value of move-time
in your script, you can do the following:
int m_moveTime; MyBehavior(UnitPtr unit, SValue& params) { m_unit = unit; m_moveTime = GetParamInt(unit, params, "move-time"); }
To make the value optional instead of required, add the 4th optional argument false
, followed by an optional default value. For example, if you would like move-time
in the above example to be optional and default to 1000, you would do this:
m_moveTime = GetParamInt(unit, params, "move-time", false, 1000);
Functions
Further, you can implement any function you'd like from the below list in your class, and they will be called as events.
void Update(int dt) void QueuedPathfind(array<vec2>@ path) void QueuedFetchActors(array<UnitPtr>@ actors) void Collide(UnitPtr unit, vec2 pos, vec2 normal) void Collide(UnitPtr unit, vec2 pos, vec2 normal, Fixture@ fxSelf, Fixture@ fxOther) void EndCollision(UnitPtr unit) void EndCollision(UnitPtr unit, Fixture@ fxSelf, Fixture@ fxOther) void Destroyed() vec4 GetOverlayColor() SValue@ Save() void Load(SValue@ save) void PostLoad(SValue@ save)