Actors

The Leadwerks Actor class provides a way to easily control your game entities. The Actor class is abstract meaning that it is missing required functions. We create our own custom class derived from the Actor class, which adds our own functions onto the base. This is called inheritence.

By default, a new game will include a sample actor class you can modify called "MyActor". The code for this can be found in the "MyActor.h" and MyActor.cpp" files in the "Source" folder in your game directory. The derived actor class includes several functions you can uncomment to override the default behavior with your own. To enable a function, just uncomment it in both the header and .cpp file and add your own C++ code inside the function brackets.

Actor Functions


The following functions can be added to a derived actor class. These are analagous the object script functions we learned about in Lua.
  • void Attach();
  • void Detach();
  • void Collision(Entity* entity, const Vec3& position, const Vec3& normal, float speed);
  • void UpdateWorld();
  • void UpdatePhysics();
  • void UpdateMatrix();
  • void PostRender(Context* context);
  • void Draw();
  • void DrawEach(Camera* camera);

Attaching an Actor


To attach an actor to an entity, simply call this function:
Actor* actor = new MyActor;
entity->SetActor(actor);
actor->Release();// we're done with this so call Release to decrement the reference count

Thereafter, the actor's class functions will be called at various points in the game and you can access the entity with the actor's entity member:
void MyActor::UpdateWorld()
{
this->entity->Turn(1.0 * Time::GetSpeed(),0,0);
}
}

To remove from an entity, either assign a new actor or set its actor to NULL or nullptr:
entity->SetActor(nullptr);

You do not have to call Detach() yourself or modify any members of the entity, as this is all managed automatically by the engine.

Actors are an easy way to control your entities without having to worry about updating or storing objects in containers. You can assign different actors based on what type of object an entity is and how you want it to behave.

Conclusion


This concludes our series of C++ tutorials for Leadwerks. You now have a solid understand of the core concepts of the C++ programming language and game programming with Leadwerks. You can re-read the material any time you need to brush up on the basics, and feel free to ask questions on the forum any time.