get_field_value

Lookup for an attribute or property with the given name in the entity and return back its Value or return an error if it is not found

Arguments

  • name - The name of the attribute or property to lookup

Returns

  • Some(Value) - If the attribute or property is present in the entity
  • None - If the attribute or property is not present in the entity

Signature

---@param field_name string @Name of the field to lookup
---@return Value|nil error
function Entity:get_field_value(name)

Example of Usage

-- bind the exposed entity UserData to a local variable
local entityAPI = alacrity.Entity

-- create a new random unique user ID
local id = alacrity.uuid.new_v4()

-- create a new Entity instance
local e = entityAPI.new({
    id = id,
    name = "My new Ad-Hoc entity",
    description = "This is an Ad-Hoc example test entity",
    attributes = {
        { name = "str", value = 10 },
        { name = "dex", value = 5 },
        { name = "int", value = 3 },
        { name = "wis", value = 1 },
        { name = "cha", value = 4 },
    },
    properties = {
        { name = "level", value = 1, hide_from_conditions = false },
        { name = "class", value = "knight ", hide_from_conditions = false },
        { name = "active_quest", value = 1, hide_from_conditions = true },
    }
})

-- this assertions pass
assert(e:get_field_value("level").Value, 1)
assert(e:get_field_value("str").Value, 10)
assert(e:get_field_value("active_quest").Value, 1)
assert(e:get_field_value("wis").Value, 1)

ok, err = pcall(function()
    local v = e:get_field_value("none")
    return v
end)
assert(ok, false)

assert(e:get_field_value("none") == nil, true)