Skip to content

Entity cast

Julien SOYSOUVANH edited this page Oct 24, 2021 · 2 revisions

Sometimes one might want to get the concrete type of an entity to access more specific data. It is possible to do that by checking the kind of an entity, and then downcast it:

#include <Refureku/Refureku.h>

void doSomethingIfClass(rfk::Entity const* entity)
{
    if (entity != nullptr && entity->getKind() == rfk::EEntityKind::Class)
    {
        rfk::Class const* c = reinterpret_cast<rfk::Class const*>(entity);
        //Do something with c
    }
}

However, this way of doing things is very verbose, so Refureku has cast functions for each kind of entity. The above code becomes:

#include <Refureku/Refureku.h>

void doSomethingIfClass(rfk::Entity const* entity)
{
    if (rfk::Class const* c = rfk::classCast(entity))
    {
        //Do something with c
    }
}