Adding a Behavior

Adding a Behavior allows you to code and give a specific behavior (code, actions, things to do) to an object in the scene.

After you've created the Behaviour through the use of a game plugin, you will be able to give a Behavior to an object directly within the Maratis Editor.

Behaviors also allow to set a given number of variables to the object which has the Behavior; those variables can be then edited directly within the Maratis Editor or with the use of scripts.

In this example we will make an object rotating with the use of a Behavior.

Game Plugin
First you need to create a game plugin as shown here: How to add your own C++ code (Game Plugin tutorial)

Source code
Now you can create the MyBehavior.h and MyBehavior.cpp files which will contain the code for your Behavior.

MyBehavior.h
///////////////////////////////////////////////////////////////////////////////////////////////////////// // GamePlugin // MyBehavior.h // // Code : Anael Seghezzi /////////////////////////////////////////////////////////////////////////////////////////////////////////


 * 1) ifndef _MY_BEHAVIOR_H
 * 2) define _MY_BEHAVIOR_H


 * 1) include 

class MyBehavior : public MBehavior { public:

// constructors / destructors MyBehavior(MObject3d * parentObject); MyBehavior(MyBehavior & behavior, MObject3d * parentObject); ~MyBehavior(void);

private: // cutom variables float m_rotationSpeed; public:

// destroy void destroy(void);

// get new static MBehavior * getNew(MObject3d * parentObject);

// get copy MBehavior * getCopy(MObject3d * parentObject);

// name static const char * getStaticName(void){ return "MyBehavior"; } const char * getName(void){ return getStaticName; }

// variables unsigned int getVariablesNumber(void); MVariable getVariable(unsigned int id);

// events (virtuals from MBehavior class) void update(void); void runEvent(int param){} };


 * 1) endif

MyBehavior.cpp
///////////////////////////////////////////////////////////////////////////////////////////////////////// // GamePlugin // MyBehavior.cpp // // Code : Anael Seghezzi /////////////////////////////////////////////////////////////////////////////////////////////////////////


 * 1) include "MyBehavior.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////// // Init, this part is always similar, constructor, copy constructor, etc /////////////////////////////////////////////////////////////////////////////////////////////////////////

// constructor MyBehavior::MyBehavior(MObject3d * parentObject): MBehavior(parentObject), m_rotationSpeed(1) {}

// copy constructor MyBehavior::MyBehavior(MyBehavior & behavior, MObject3d * parentObject): MBehavior(parentObject), m_rotationSpeed(behavior.m_rotationSpeed) {}

// destructor MyBehavior::~MyBehavior(void) {}

// destroy function : always similar void MyBehavior::destroy(void) {	delete this; }

// getNew function : always similar MBehavior * MyBehavior::getNew(MObject3d * parentObject) {	return new MyBehavior(parentObject); }

// getCopy function : always similar MBehavior * MyBehavior::getCopy(MObject3d * parentObject) {	return new MyBehavior(*this, parentObject); }

///////////////////////////////////////////////////////////////////////////////////////////////////////// // Variables; allows to access custom variables from script and from Maratis Editor /////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int MyBehavior::getVariablesNumber(void){ return 1; }

MVariable MyBehavior::getVariable(unsigned int id) {	switch(id) {	default: return MVariable("NULL", NULL, M_VARIABLE_NULL); case 0: return MVariable("rotationSpeed", &m_rotationSpeed, M_VARIABLE_FLOAT); } }

///////////////////////////////////////////////////////////////////////////////////////////////////////// // Events /////////////////////////////////////////////////////////////////////////////////////////////////////////

// update function (called by MGame class by default) void MyBehavior::update(void) {	MEngine * engine = MEngine::getInstance; MGame * game = engine->getGame;

// check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior) if(! game->isRunning) return;

// get the associated parent object (who is using this behavior) MObject3d * parent = getParentObject;

// lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed); }

GamePlugin.cpp
Now we have to edit our GamePlugin.cpp file to make it actually add our Behavior to the game, using MBehaviorManager.

//[...] void StartPlugin(void) {	// get engine MEngine * engine = MEngine::getInstance;

// add our behavior MBehaviorManager * behaviorManager = engine->getBehaviorManager; behaviorManager->addBehavior(MyBehavior::getStaticName, M_OBJECT3D, MyBehavior::getNew);

// set game game = new MyGame; engine->setGame(game); } //[...]