asqbtcupid / unreal.lua

lua solution for UnrealEngine4
MIT License
300 stars 98 forks source link

UTableUtil::GetTempIns just can keep 10 ins, #17

Closed mirchd closed 6 years ago

mirchd commented 6 years ago

UTableUtil::GetTempIns just can keep 10 ins, If lua ref C++ object, and in single functin or single class, it is very easy to over 10, I need to new lua and copy ref C++ object to this new lua to avoid it, can any other way?

mirchd commented 6 years ago

self.posArr[1] = {FVector2D.New(0.0, -92.0)} self.posArr[2] = {FVector2D.New(-103.0, 21.0), FVector2D.New(103.0, 21.0)} self.posArr[3] = {FVector2D.New(0.0, -92.0), FVector2D.New(-103.0, 21.0), FVector2D.New(103.0, 21.0)} self.posArr[4] = {FVector2D.New(-53.0, -72.0), FVector2D.New(53.0, -72.0), FVector2D.New(-103.0, 21.0), FVector2D.New(103.0, 21.0)} self.posArr[5] = {FVector2D.New(0.0, -100.0), FVector2D.New(-92.0, -52.0), FVector2D.New(92.0, -52.0), FVector2D.New(-103.0, 32.0), FVector2D.New(103.0, 32.0)} self.posArr[6] = {FVector2D.New(-35.0, -92.0), FVector2D.New(35.0, -92.0), FVector2D.New(-98.0, -44.0), FVector2D.New(98.0, -44.0), FVector2D.New(-103.0, 32.0), FVector2D.New(103.0, 32.0)}

dump(self.posArr)

the data of array is not the same as above

mirchd commented 6 years ago

FVector2D.New = ULuautils.FVector2D_New

asqbtcupid commented 6 years ago

It's an opmization for lua garbagecollection.

When you get c++ struct property, or call a function which return struct.

What you get is a temp ins,The memory will be reused.As you mention,the lenth of the temp buffer was limited to 10.

The original FVector2D.New allocate memory in heap.But can't pass value for construction.

So i have to write

local v = FVector.New()
v.X = 10, v.Y = 20

for convinient, I replace it with ULuautils.FVector2D_New which cause such bug.

for each struct, there is a function named "Copy",It will create a same instance in heap.you can write

self.posArr[2] = {FVector2D.New(-103.0, 21.0):Copy(), FVector2D.New(103.0, 21.0):Copy()}

finally,for performance consideration, you should avoid cross lua and c++ and create gc object.

asqbtcupid commented 6 years ago

and you can increase the length of the buffer.

https://github.com/asqbtcupid/unreal.lua/blob/0be0ffee77e9b27acf84cecd77e80149df1b7601/Plugins/LuaPlugin/Source/LuaPluginRuntime/TableUtil.h#L1022

Remenber,don't keep the temp ins for later use.if you want. call Copy() to create the same one managed by lua garbage system.

mirchd commented 6 years ago

ok, thank you for quickly reply