Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Wrapping instances with Roblox's lua?

Asked by 8 years ago

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.

0
What exactly do you mean with wrapping, do you mean TextWrap, Coroutine.wrap? RubenKan 3615 — 8y
0
@RubenKan Wrapping is an abstract term for proving a "different" interface for the same object. It's not being used in another context. ScriptGuider 5640 — 8y
0
No, not that type of wrapping. By wrapping instances, I mean wrapping as such: https://forum.roblox.com/Forum/ShowPost.aspx?PostID=158772115 that there is a reference to the original object. But I want to see why it's useful as a functionality. AbstractCode 16 — 8y
0
Yes, that's the same concept. ScriptGuider 5640 — 8y

1 answer

Log in to vote
4
Answered by 8 years ago
Edited 8 years ago

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.

0
To give a *why* to this what: people make wrappers for ROBLOX objects as a sort-of OOP extension. For instance: giving Parts new methods for your specific game. adark 5487 — 8y
0
Basically, TL;DR: It's just a way to add additional properties to an already existing instance and manipulate it while still retaining the instance's old properties. AbstractCode 16 — 8y
Ad

Answer this question