asqbtcupid / unreal.lua

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

How to avoid GC objects that created in lua? #33

Closed ghost closed 4 years ago

ghost commented 6 years ago

If i want to create a UObject, In C++, i need to AddRefrenceObject to avoid the GC, But in lua, if i create a UObject, where shound i put the AddRefrenceObject code to? How do you avoid the GC in your lua framework?

nonchip commented 5 years ago

make sure they are not unreferenced, e.g. by using a file like this:

-- nogc.lua
local _nogc_reg={}
return function(object)
  table.insert(_nogc_reg, object)
end

then you can do:

local nogc=require("nogc")

local something={1,2,3}
nogc(something)

the idea is: require caches modules VM wide, so the upvalue table _nogc_reg is referenced through said cache, and contains a reference to something so the GC will leave that alone.