RedCore
Loading...
Searching...
No Matches
Creating a Basic Actor

To demonstrate the registration process, we will create a simple actor that will display a rotating model, and register it to the game.

First, define the class for the actor, inheriting from Actor

#include <actor/Actor.h>
class DemoActor : public Actor {
public:
// Holds registry information
static Profile* sProfile;
DemoActor(const ActorCreateParam& param); // The constructor is only used for setting initial values
~DemoActor() override = default; // The heap is managed, unnecessary to implement beyond default
// Basic lifecycle functions
Result create() override;
bool execute() override;
bool draw() override;
private:
// Add custom data
AnimModel* mModel;
};
virtual bool draw()
virtual bool execute()
virtual Result create()

To learn more about the actor lifecycle, see Actor.

Next, register the actor to the game using the getRegistrar() function we created earlier:

Profile* DemoActor::sProfile = getRegistrar()->newProfile<DemoActor>("demo_actor")
.resources<"star_coin">(ProfileInfo::cResType_Course)
.build();

The builder has additional methods for setting different properties of the profile, see red::ProfileBuilder for more information.

Finally, create the actor's constructor and implement the lifecycle functions:

DemoActor::DemoActor(const ActorCreateParam& param)
: Actor(param)
, mModel(nullptr)
{ }
ActorBase::Result DemoActor::create() {
// Load a model from content/Common/actor/
// SZS/BFRES name, then the model name inside
mModel = AnimModel::create("star_coin", "star_coinA");
return cResult_Success;
}
bool DemoActor::execute() {
mAngle.z() += sead::Mathf::deg2idx(2.0f); // Spin 2 degrees per frame
// Update the model's transformation/animation
mModel->update(mPos, mAngle, mScale);
return true;
}
bool DemoActor::draw() {
mModel->draw();
return true;
}
static AnimModel * create(ModelResource *mdl_res, Model *model, s32 skl_anim_num, s32 tex_anim_num, s32 shu_anim_num, s32 vis_anim_num, s32 sha_anim_num, sead::Heap *heap=nullptr, const sead::PtrArray< ModelResource > *anim_mdl_res_array=nullptr)
static constexpr u32 deg2idx(T a)

Finally, to test the actor, you must inform the level editor that it exists so that it can be placed in a level. See here for an example.