Skip to content

Commit

Permalink
Lua: Add support for setting properties
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 16, 2024
1 parent 5c281ff commit a88cab3
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,57 @@ sol::object prop_to_object(sol::this_state s, uevr::API::UObject& self, const st
return sol::make_object(s, sol::lua_nil);
}

void set_property(sol::this_state s, uevr::API::UObject& self, const std::wstring& name, sol::object value) {
const auto c = self.get_class();

if (c == nullptr) {
return;
}

const auto desc = c->find_property(name.c_str());

if (desc == nullptr) {
return;
}

const auto propc = desc->get_class();

if (propc == nullptr) {
return;
}

const auto name_hash = utility::hash(propc->get_fname()->to_string());

switch (name_hash) {
case L"BoolProperty"_fnv:
self.set_bool_property(name, value.as<bool>());
return;
case L"FloatProperty"_fnv:
self.get_property<float>(name) = value.as<float>();
return;
case L"DoubleProperty"_fnv:
self.get_property<double>(name) = value.as<double>();
return;
case L"IntProperty"_fnv:
self.get_property<int32_t>(name) = value.as<int32_t>();
return;
case L"UIntProperty"_fnv:
case L"UInt32Property"_fnv:
self.get_property<uint32_t>(name) = value.as<uint32_t>();
return;
case L"NameProperty"_fnv:
//return sol::make_object(s, self.get_property<uevr::API::FName>(name));
throw sol::error("Setting FName properties is not supported (yet)");
case L"ObjectProperty"_fnv:
self.get_property<uevr::API::UObject*>(name) = value.as<uevr::API::UObject*>();
return;
case L"ArrayProperty"_fnv:
throw sol::error("Setting array properties is not supported (yet)");
};

// NONE
}

int ScriptContext::setup_bindings() {
m_lua.registry()["uevr_context"] = this;

Expand Down Expand Up @@ -456,13 +507,22 @@ int ScriptContext::setup_bindings() {
"get_property", [](sol::this_state s, uevr::API::UObject& self, const std::wstring& name) -> sol::object {
return prop_to_object(s, self, name);
},
"set_property", &set_property,
sol::meta_function::index, [](sol::this_state s, uevr::API::UObject& self, sol::object index_obj) -> sol::object {
if (!index_obj.is<std::string>()) {
return sol::make_object(s, sol::lua_nil);
}

const auto name = utility::widen(index_obj.as<std::string>());
return prop_to_object(s, self, name);
},
sol::meta_function::new_index, [](sol::this_state s, uevr::API::UObject& self, sol::object index_obj, sol::object value) {
if (!index_obj.is<std::string>()) {
return;
}

const auto name = utility::widen(index_obj.as<std::string>());
set_property(s, self, name, value);
}
);

Expand Down

0 comments on commit a88cab3

Please sign in to comment.