Discord
Login
Community
DARK THEME

Cloning Objects

If you do obj_copy = obj then it just copies the internal pointer, Is there a way to copy an object's data, not just the internal pointer?

can you explain why you want to make a copy. knowing what you need it to do makes it a lot easier to help

Sorry, I'm making a save system for a game where it stores all relevant objects,variables and lists in a save file object, then adds the save file object to a list and then store the list in storage. The issue is that because it saves the pointer not the data, if say the player jumps the actual y changes but so does y in the save file, so when you load the save file it does nothing. Does that help?

for that you need to make a deep copy. you can use this code to make a deep copy

deepCopy = function(obj) if typeof obj != "object" or obj == 0 then return obj end

local result = (obj.keys.length > 0 and typeof obj.keys[0] == "number") ? [] : {}

for key, value in obj if typeof value == "object" and value != 0 then result[key] = deepCopy(value) else result[key] = value end end

return result end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community