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

Is OOP possible on instances(parts) in Roblox?

Asked by 9 years ago
Cube = something...

function up(self, y)
    self.Position = self.Position + Vector3.new(0, y, 0)
end

Cube:up(1)
1
In what way is this OOP? BlueTaslem 18071 — 9y
0
Tell me if my solution is what you meant, as I think you were trying along the lines of that DigitalVeer 1473 — 9y
1
Yes CodeSponge 125 — 9y
0
Because the part is acting like a class DewnOracle 115 — 9y
View all comments (2 more)
0
What is OOP? yumtaste 476 — 9y
0
Object Oriented Programming. http://docs.oracle.com/javase/tutorial/java/concepts/ DigitalVeer 1473 — 9y

3 answers

Log in to vote
1
Answered by 9 years ago

Yes! It is possible!

You also don't have to use metatables to achieve the task.

You can set up a little system such as the following:

Cube = workspace.BasePlate


function instanceHandler(cube)
local object = { }
local Cube = cube

function object:up(y)
     cube.Position = cube.Position + Vector3.new(0, y, 0)
     return cube.Position
end     

return object
end

--'main method'

local newcube = instanceHandler(Cube)
newcube:up(5)

print("Cubes position:")
print(Cube.Position)
print("Position After Up:")
print(newcube:up(10))



If you think about it, this works the same way that some other things you may see also work, just like when you see str:lower() and str:sub()

That is a form of OOP right there as it has built-in methods that work like the example given above.

Ad
Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You can define methods in Lua.

If t has a field called a, then t:a(x) will call t.a(t, x).

While you can't add new fields to ROBLOX objects, you could make wrappers for them.

It would in general be better to just call up yourself, though,

moveUp(workspace.Brick, 5)

(It's best practice to make all methods / functions named as verbs because it makes it clearer what it actually does.)

function moveUp(thing, by)
    thing.CFrame = thing.CFrame + Vector3.new(0, by, 0)
end

function wrap(part)
    local myobj = { MoveUp = moveUp }
    setmetatable(part, {__index = part, __newindex = part})
end

local part = wrap(workspace.Part)
part:MoveUp( 5 )

-- But it would be clearer and simpler to just say

moveUp( workspace.Part, 5 )

Log in to vote
1
Answered by 9 years ago

If you want proper inheritance, metamethods can accomplish this (with some limitations).

  1. When you're creating a new instance of your class, also create an instance of the object you're inheriting from

  2. Use __index and __newindex to search through functions/properties of the immediate class and then any base-classes, until you get to the one that inherits from the Roblox object -- after that, try to access the property/function via that object you created in step 1. If you're in __newindex and the property doesn't exist (ie an error occurred when trying to use it [you wrap that part in a pcall]), create the variable in the instance that triggered the __newindex.

Limitations:

These inherited objects cannot interface properly with Roblox's hierarchy. If you have an instance "specialPart" created from class "SpecialPart" which inherits from Roblox's "Part", you can use metamethods to allow specialPart.Parent = workspace, but - since you can't change Roblox's metatables, you cannot do Instance.new("Part").Parent = specialPart, since specialPart is a table. You can work around this by specifying "specialPart.BaseObject" (or whatever you called the variable from step 1).

Similarly, this code can't work:

specialPart = SpecialPart.new()
specialPart.SpecialValue = 5
specialPart.Parent = workspace --this can work with metamethods specifically checking for assignment to "Parent" (occurs in __newindex); it can redirect the assignment to "specialPart.BaseObject.Parent = workspace"
specialPart.Name = "SpecialPart"
--elsewhere
specialPart = workspace.SpecialPart --this will only return a Part, not the SpecialPart created above
print(specialPart.SpecialValue) --thus, this will error

There are ways to work around this, but true inheritance would mean that you don't have these limitations in the first place.

Answer this question