Change, add, delete property, method class

Well I need to add some properties and methods for Intance: Part
This is the part I want to change:

local part= Instance.new("Part")

part.Jet=0 -- new

local bPosition= part.Position

local aPosition

while true

do

wait(1)

aPosition= part.Position

part.Velocity= aPosition- bPosition-- change

bPosition= aPosition

end

Since all parts must have it, it would be really easy not to have to add it as a script

Your question is very unclear, I am assuming you want to be able to set a custom property of the part, just make a table and use the part as an index then its value is a table containing “Jet” which you can change.

1 Like

I sense opportunity for an object-oriented approach:

local brick = {}
brick.__index = brick

function brick.new(part) --//Create new brick class instance, passing the actual brick as a parameter
local this = {}

this.part = part --//Store the actual part in the new object
this.Jet = 0 --//Declare custom fields here

setmetatable(this, brick) --//Set metamethods to the new object

return this --//Return the new object to where this function was called
end

local myBrick = brick.new(workspace.Baseplate) --//Pass in the actual brick as a parameter
3 Likes

You still need to keep in mind that you wouldn’t be able to call the base methods, no?

That’s why I included the __index metamethod. When you attempt to index a nil field in this, it’ll point the index to the brick object, rather than the this, or the new instance of brick object.

1 Like

Yes, I understand that, I presume I was being ambiguous in my speech. I meant that you wouldn’t be able to be able to call brick:Destroy(), as having to index part from the constructed object would be somewhat counterintuitive for general use.

1 Like

Oh, I understand.

Well, yeah I guess. I don’t think there’s a better way for wanting to write new fields for RBX classes though, like Part

local myBrick = brick.new(workspace.Baseplate)
myBrick.part:Destroy()

I also think it’s impossible but it would be very easy to write a class and inherit from Part.

Wrapping instances.

local function wrap(instance)
    return setmetatable({ this = instance }, { __index = instance })
end

From there you can write functions that affect this. Though you’re going to have to make sure to clean the part up when it’s destroyed to prevent memory leaks.

Can you take a small example? I am still not familiar with the lua structure.

Sure.

local part = Instance.new("Part")
part.Parent = workspace
local wrapped = wrap(part)

function wrapped:GoCrazy()
    for i = 1, 50 do
        self.this.BrickColor = BrickColor.Random()
        wait(0.1)
    end
end

wrapped:GoCrazy()

This would make the part go crazy by changing its color to a random one 50 times.

Well, how about creating an event? I just need the same thing.
function wrapped:GoCrazy()

You can use a bindable event.

local bindable = Instance.new("BindableEvent")
wrapped.WentCrazy = bindable.Event

Then in the GoCrazy method

bindable:Fire()

To listen for it

wrapped.WentCrazy:Connect(function()
    print("Ok this is epic")
end)