Skip to content
Modar Nasser edited this page Jun 19, 2024 · 17 revisions

In file LDtkLoader/Entity.hpp

Class : ldtk::Entity

Represents an Entity instance.

Tip

To get the correct sprite data of the Entity as it is shown in the editor, you should use hasSprite to check if the Entity has a sprite, and then getTexturePath and getTextureRect to get the texture data.

Fields

layer

const Layer* const layer

Pointer to the Layer object that contains the Entity.

iid

const std::string ldtk::Entity::iid

Unique instance ID of the Entity.

🔝

Methods

getName

ldtk::Entity::getName() const -> const std::string&

Returns the name of the Entity.

🔝

getSize

ldtk::Entity::getSize() const -> const ldtk::IntPoint&

Returns the size in pixels of the Entity.

🔝

getColor

ldtk::Entity::getColor() const -> const ldtk::Color&

Returns the color of the Entity.

🔝

getPosition

ldtk::Entity::getPosition() const -> const ldtk::IntPoint&

Returns the position in pixels of the Entity relative to the parent Layer.

If the layer has an offset different than (0, 0), the layer's total offset should be added to get the position in pixels relative to the parent Level :

auto entity_level_pos = entity.getPosition() + entity.layer->getOffset();

🔝

getGridPosition

ldtk::Entity::getGridPosition() const -> const ldtk::IntPoint&

Returns the position in grid coordinates of the Entity.

🔝

getWorldPosition

ldtk::Entity::getWorldPosition() const -> const ldtk::IntPoint&

Returns the computed position in pixels of the Entity relative to the World.

🔝

getPivot

ldtk::Entity::getPivot() const -> const ldtk::FloatPoint&

Returns the pivot of the Entity, a point from (0.f, 0.f) to (1.f, 1.f).

🔝

hasSprite

ldtk::Entity::hasSprite() const -> bool

Returns true if the Entity has a sprite associated to it, returns false otherwise.

🔝

getTexturePath

ldtk::Entity::getTexturePath() const -> const std::string&

Returns the path to the texture of the sprite. Returns an empty string if the Entity has no sprite.

🔝

getTextureRect

ldtk::Entity::getTextureRect() const -> const ldtk::IntRect&

Returns the texture rectangle of this Entity's sprite.

🔝

hasNineSlice

ldtk::Entity::hasNineSlice() const -> bool

Returns true if the Entity's sprite has a 9-slices scaling, returns false otherwise.

🔝

getNineSliceBorders

ldtk::Entity::getNineSliceBorders() const -> const ldtk::NineSliceBorders&

Returns the Entity's 9-slices borders. See NineSliceBorders.

🔝

hasTag

ldtk::Entity::hasTag(const std::string&) const -> bool

Returns true if the Entity has the given tag, returns false otherwise.

🔝

allTags

ldtk::Entity::allTags() const -> const std::vector<std::string>&

Returns a vector containing all tags of the Entity.

🔝

getField<T>

Inherited from FieldsContainer

template <FieldType T>
ldtk::Entity::getField(const std::string& name) const -> const ldtk::getFieldType<T>&

Returns the field matching the given name and type. Returned field can be null.

T must be one of the values of the FieldType enum.

This overload allows to get either single value fields or array fields:

// get a single value field
const auto& field = entity.getField<ldtk::FieldType::Color>("hair_color");

if (!field.is_null()) {
    // get the field value
    const auto& hair_color = field.value();
}
// get an array field
const auto& array_field = level.getField<ldtk::FieldType::ArrayPoint>("spawns");

// iterate on the array field
for (const auto& field : array_field) {
    if (!field.is_null()) {
        // get the field value
        const auto& point = field.value();
    }
}

template <typename T>
ldtk::Entity::getField(const std::string& name) const -> const ldtk::Field<T>&

Returns the field matching the given name and type. Returned field can be null.

T must be one of the following types : int, float, bool, std::string, ldtk::Color, ldtk::IntPoint, ldtk::Enum, ldtk::FilePath.

For example, if your Entity has a field of type Color named "color", you can write :

const Entity& object = ...; // get the Entity

// get the field
const auto& field = object.getField<ldtk::Color>("color");

if (!field.is_null()) {
    // get the field value
    const auto& color = field.value();
}

🔝

getArrayField<T>

Inherited from FieldsContainer

template <typename T>
ldtk::Entity::getArrayField(const std::string& name) const -> const ldtk::ArrayField<T>&

Returns the array field matching the given name and type.

ldtk::ArrayField<T> is equivalent to std::vector<ldtk::Field<T>> and can be iterated over like a normal vector. Fields can be null.

T must be one of the following types : int, float, bool, std::string, ldtk::Color, ldtk::IntPoint, ldtk::Enum, ldtk::FilePath.

For example, if your Entity has a field of type ArrayPoint named "spawns", you can write :

const Entity& object = ...; // get the Entity

// get the field
const auto& array_field = object.getArrayField<ldtk::IntPoint>("spawns");

// iterate on the array field
for (const auto& field : array_field) {
    if (!field.is_null()) {
        // get the field value
        const auto& point = field.value();
    }
}

🔝

Clone this wiki locally