Difference between revisions of "Creating a Behavior"

From Serious Sam's Bogus Detour
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
The most basic behavior is one that does nothing and simply stores the [[UnitPtr]]:
 
The most basic behavior is one that does nothing and simply stores the [[UnitPtr]]:
  
<pre>
+
<pre class="prettyprint">
 
class MyBehavior
 
class MyBehavior
 
{
 
{
Line 23: Line 23:
 
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:
 
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>
+
<pre class="prettyprint">
 
<behavior class="MyBehavior">
 
<behavior class="MyBehavior">
 
   <int name="move-time">1000</int>
 
   <int name="move-time">1000</int>
Line 31: Line 31:
 
Then, to get the value of <code>move-time</code> in your script, you can do the following:
 
Then, to get the value of <code>move-time</code> in your script, you can do the following:
  
<pre>
+
<pre class="prettyprint">
 
int m_moveTime;
 
int m_moveTime;
  
Line 43: Line 43:
 
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:
 
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>
+
<pre class="prettyprint">
 
m_moveTime = GetParamInt(unit, params, "move-time", false, 1000);
 
m_moveTime = GetParamInt(unit, params, "move-time", false, 1000);
 
</pre>
 
</pre>
Line 65: Line 65:
 
</pre>
 
</pre>
  
[[Category:Guides]]
+
[[Category:Tutorials]]

Latest revision as of 12:34, 31 July 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.

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)