Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions UE4SS/src/LuaType/LuaUObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,9 +1275,72 @@ namespace RC::LuaType
case Operation::GetNonTrivialLocal:
map_to_lua_table(params.lua, params.property, params.data);
return;
case Operation::Set:
lua_table_to_map();
case Operation::Set: {
if (params.lua.is_userdata(params.stored_at_index))
{
// Handle TMap userdata
auto& lua_tmap = params.lua.get_userdata<TMap>(params.stored_at_index);
Unreal::FScriptMap* source_map = lua_tmap.get_remote_cpp_object();

Unreal::FMapProperty* map_property = static_cast<Unreal::FMapProperty*>(params.property);
FScriptMapInfo info(map_property->GetKeyProp(), map_property->GetValueProp());
if (!info.key || !info.value)
{
params.throw_error("push_mapproperty", "Invalid key or value property");
}

auto dest_map = new(params.data) Unreal::FScriptMap{};

// Copy elements from source to destination
Unreal::int32 max_index = source_map->GetMaxIndex();
for (Unreal::int32 i = 0; i < max_index; i++)
{
if (!source_map->IsValidIndex(i))
{
continue;
}

// Get source key/value pair
void* src_pair = source_map->GetData(i, info.layout);

// Add new entry to destination map
Unreal::int32 dest_index = dest_map->AddUninitialized(info.layout);
void* dest_pair = dest_map->GetData(dest_index, info.layout);

// Initialize the destination memory
Unreal::FMemory::Memzero(dest_pair, info.layout.SetLayout.Size);

// Copy key
info.key->CopySingleValueToScriptVM(dest_pair, src_pair);

// Copy value
void* src_value = static_cast<uint8_t*>(src_pair) + info.layout.ValueOffset;
void* dest_value = static_cast<uint8_t*>(dest_pair) + info.layout.ValueOffset;
info.value->CopySingleValueToScriptVM(dest_value, src_value);
}

// Rehash the destination map
dest_map->Rehash(info.layout,
[&](const void* src) -> Unreal::uint32 {
return info.key->GetValueTypeHash(src);
});
}
else if (params.lua.is_table(params.stored_at_index))
{
// TMap as table
lua_table_to_map();
}
else if (params.lua.is_nil(params.stored_at_index))
{
// Empty map
new(params.data) Unreal::FScriptMap{};
}
else
{
params.throw_error("push_mapproperty", "Parameter must be of type 'TMap' or table");
}
return;
}
case Operation::GetParam:
RemoteUnrealParam::construct(params.lua, params.data, params.base, params.property);
return;
Expand Down
6 changes: 6 additions & 0 deletions assets/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Added `TSet` implementation. [UE4SS #883](https://github.com/UE4SS-RE/RE-UE4SS/p

Added `TMap` implementation. [UE4SS #755](https://github.com/UE4SS-RE/RE-UE4SS/issues/755)

Enhanced TMap support to enable round-trip functionality [UE4SS #933](https://github.com/UE4SS-RE/RE-UE4SS/issues/993)
- TMap userdata can now be passed as function parameters
- Improved handling of empty maps and nil values
- Fixed proper key-value pair copying when passing maps between Lua and C++
- Added proper rehashing to ensure map integrity after copying

Added global function `CreateInvalidObject`, which returns an invalid UObject. ([UE4SS #652](https://github.com/UE4SS-RE/RE-UE4SS/issues/652))

Added GenerateLuaTypes function. ([UE4SS #664](https://github.com/UE4SS-RE/RE-UE4SS/pull/664))
Expand Down
Loading