What's the purpose of this and what exactly does it do?
I heard it's to get the original instance and manipulate it through a table but I think that's too vague and might be wrong.
What is wrapping?
All objects have an interface (a way of interacting with it, invoking behavior, changing properties, etc...). However, sometimes the interface doesn't include something(s) that the programmer may find particularly useful. Unless you're the one designing the object, you can't just add or change it's preset behavior. You can, however, create your own interface for this object, which does the new interaction for you. This is called wrapping.
Creating a wrapper
In Lua, you can emulate the wrapping of an object by using tables (for the object) and metatables (for the wrapper). Say for example, you wanted to wrap a property to a Part
instance, which represents when the part was created (or when it was wrapped). We'll call this property BirthTime
. First, let's create a function that will handle this for us (you should know what tables and metatables are before reading):
-- Create part first local part = Instance.new("Part", workspace) local function WrapPart(part) local interface = {} local behavior = {} -- Object interface -- interface.BirthTime = os.time() -- Create property -- Interface behavior -- behavior.__index = part -- Set reference to this object to part function behavior:__newindex(key, value) part[key] = value -- Set new index of this object to new index of part end -- Return new interface return setmetatable(interface, behavior) end -- Pass part to wrapper local wrappedPart = WrapPart(part) -- As you can see, we can access both our new property, and all the part's other properties. Giving us the illusion we added something to this object. print(wrappedPart.BirthTime) -- > Timestamp print(wrappedPart.Name) -- > "Part"
Now, this is a very weak example compared to a wrapper's full potential, but this is only meant to give you the idea. After all, we're just returning the table we created with only two metamethods to redirect reference. There's nothing that handles calling a function as a method on the object once wrapped, parenting something to it, etc. Creating more complex examples would probably just lead to more confusion at this stage. Hope this gave you a better idea of what wrapping is, if you have any questions, just let me know.