Difference between revisions of "Creating a Behavior"
(Created page with "===Functions=== void Update(int dt) void QueuedPathfind(array<vec2>@ path) void QueuedFetchActors(array<UnitPtr>@ actors) void Collide(UnitPtr unit, vec2 pos,...") |
|||
Line 1: | Line 1: | ||
− | ===Functions== | + | Behaviors are classes that control a [[Unit]]. For example, health items have the Pickup behavior. This pickup behavior responds to the <code>Collide</code> function, so that it can perform an [[Effect]] when a [[Player]] touches the item. |
− | + | ||
− | + | __TOC__ | |
− | + | ||
− | + | ==Minimal behavior== | |
− | + | ||
− | + | The most basic behavior is one that does nothing and simply stores the [[UnitPtr]]: | |
− | + | ||
− | + | <pre> | |
− | + | class MyBehavior | |
− | + | { | |
− | + | UnitPtr m_unit; | |
− | + | ||
+ | MyBehavior(UnitPtr unit, SValue& params) | ||
+ | { | ||
+ | m_unit = unit; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==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: | ||
+ | |||
+ | <pre> | ||
+ | <behavior class="MyBehavior"> | ||
+ | <int name="move-time">1000</int> | ||
+ | </behavior> | ||
+ | </pre> | ||
+ | |||
+ | Then, to get the value of <code>move-time</code> in your script, you can do the following: | ||
+ | |||
+ | <pre> | ||
+ | int m_moveTime; | ||
+ | |||
+ | MyBehavior(UnitPtr unit, SValue& params) | ||
+ | { | ||
+ | m_unit = unit; | ||
+ | m_moveTime = GetParamInt(unit, params, "move-time"); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | To make the value optional instead of required, add the 4th optional argument <code>false</code>, followed by an optional default value. For example, if you would like <code>move-time</code> in the above example to be optional and default to 1000, you would do this: | ||
+ | |||
+ | <pre> | ||
+ | m_moveTime = GetParamInt(unit, params, "move-time", false, 1000); | ||
+ | </pre> | ||
+ | |||
+ | ==Functions== | ||
+ | Further, you can implement any function you'd like from the below list in your class, and they will be called as events. | ||
+ | |||
+ | <pre> | ||
+ | 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) | ||
+ | </pre> |
Revision as of 12:38, 21 April 2017
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)