Skip to content

FieldsContainer

Modar Nasser edited this page Jun 18, 2024 · 2 revisions

In file LDtkLoader/containers/FieldsContainer.hpp

Class : ldtk::FieldsContainer

Base class for objects that can have fields.

Fields are pairs of (name, value) where the value can be of one of the following types: int, float, bool, std::string, ldtk::Color , ldtk::IntPoint, ldtk::Enum or ldtk::FilePath.

Methods

getField<T>

template <FieldType T>
ldtk::FieldsContainer::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::FieldsContainer::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 FieldsContainer has a field of type Color named "color", you can write :

const [[Field|Fields#struct--ldtkFieldT]]sContainer& object = ...; // get the [[Field|Fields#struct--ldtkFieldT]]sContainer

// 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>

template <typename T>
ldtk::FieldsContainer::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 FieldsContainer has a field of type ArrayPoint named "spawns", you can write :

const [[Field|Fields#struct--ldtkFieldT]]sContainer& object = ...; // get the [[Field|Fields#struct--ldtkFieldT]]sContainer

// 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