current position:Home>In unity, Lua is object-oriented, and member objects may have residual problems.

In unity, Lua is object-oriented, and member objects may have residual problems.

2022-01-27 00:59:29 zhenmu



TaskItemUI = {
name = nil,
content = nil,
schedule = nil,
schedulebg = nil,
targetObj = nil,
taskData = nil,
curAimId = nil,
equipIcon = nil,
selectImage = nil,
-- Task status change tips
taskChangeTisp = "",
-- Progress effect playing
effecting = false,
        test_table = {}
}


function TaskItemUI:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self;
return o

end

function TaskItemUI:checkAndAdd()
     if #self.test_table == 0 then
         table.insert(self.test_table,"dfafa")
     end
end



function TaskItemUI:OnDestroy()  -- Other places release

    self.test_table = {}

end



Results when switching scenes , After the object is released ,test_table It should be empty .  

But when loading again , function TaskItemUI:checkAndAdd() The length in the judgment is not 0, It indicates that there is residue .   

If you write an initialization function use self Initialize it , Then there is no such problem ...


Why is not clear ...( But the feeling is  setmetatable The way , This self It actually exists 2 Copy object ???, Have time to test again Type the log .)

copyright notice
author[zhenmu],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270059268489.html

Random recommended