Creating an exact copy of an object

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Kopelat
Prole
Posts: 1
Joined: Tue Nov 21, 2023 5:07 pm

Creating an exact copy of an object

Post by Kopelat »

Is there a way to clone an object and possibly modify properties of it in lua?
In Roblox Lua it's just a function :Clone() but i don't know if there is one for normal lua :cry:

Do i need to make that function myself?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Creating an exact copy of an object

Post by MrFariator »

There are clone functions for a couple of specific types of things in löve, like Source:clone for audio sources. However, based on what I can surmise from Roblox documentation, you're asking for a method to create copies of lua objects. There is no readily available built-in functionality for this, in either lua or löve.

What sort of cloning were you thinking of? Maybe you are simply looking for object orientation (ie. implement some way to define classes, and easily create instances of said classes), which can be implemented with libraries like classic and middleclass.

However, if you want an exact copy, then you could use the following function:

Code: Select all

local function copy (o, seen)
  seen = seen or {}
  if o == nil then return nil end
  if seen[o] then return seen[o] end

  local no
  if type(o) == 'table' then
    no = {}
    seen[o] = no

    for k, v in next, o, nil do
      no[copy(k, seen)] = copy(v, seen)
    end
    setmetatable(no, copy(getmetatable(o), seen))
  else -- number, string, boolean, etc
    no = o
  end
  return no
end
When you pass a table to this function, it will return a copy of the table. Works on most simple tables. Wouldn't recommend this for creating instances of objects (like what Instance:clone is used for in roblox), but it can work when you need to copy or juggle some data around, without modifying the original table.
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Creating an exact copy of an object

Post by dusoft »

Check the manual, deep copies are possible:
http://lua-users.org/wiki/CopyTable
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Creating an exact copy of an object

Post by MrFariator »

The function I already posted handles deep copies, but it's not the only implementation to do so. The main caveat is that it may not work on tables that have any sort of special metatables going on, or extremely deep tables.

However, based on the roblox docs, I believe Kopelat might just need to look into simple object orientation, or maybe even ECS, since these deep table copy functions rely on recursion. That doesn't necessarily scale the best, for any sufficiently complex class.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Creating an exact copy of an object

Post by darkfrei »

Maybe it will be nice to have a entity prototype, that contents the same values in the entity, that have values that are unique for each entity.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 60 guests