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 10 years ago
1Cube = something...
2 
3function up(self, y)
4    self.Position = self.Position + Vector3.new(0, y, 0)
5end
6 
7Cube:up(1)
1
In what way is this OOP? BlueTaslem 18071 — 10y
0
Tell me if my solution is what you meant, as I think you were trying along the lines of that DigitalVeer 1473 — 10y
1
Yes CodeSponge 125 — 10y
0
Because the part is acting like a class DewnOracle 115 — 10y
View all comments (2 more)
0
What is OOP? yumtaste 476 — 10y
0
Object Oriented Programming. http://docs.oracle.com/javase/tutorial/java/concepts/ DigitalVeer 1473 — 10y

3 answers

Log in to vote
1
Answered by 10 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:

01Cube = workspace.BasePlate
02 
03 
04function instanceHandler(cube)
05local object = { }
06local Cube = cube
07 
08function object:up(y)
09     cube.Position = cube.Position + Vector3.new(0, y, 0)
10     return cube.Position
11end    
12 
13return object
14end
15 
View all 24 lines...

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
10 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,

1moveUp(workspace.Brick, 5)

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

01function moveUp(thing, by)
02    thing.CFrame = thing.CFrame + Vector3.new(0, by, 0)
03end
04 
05function wrap(part)
06    local myobj = { MoveUp = moveUp }
07    setmetatable(part, {__index = part, __newindex = part})
08end
09 
10local part = wrap(workspace.Part)
11part:MoveUp( 5 )
12 
13-- But it would be clearer and simpler to just say
14 
15moveUp( workspace.Part, 5 )
Log in to vote
1
Answered by 10 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:

1specialPart = SpecialPart.new()
2specialPart.SpecialValue = 5
3specialPart.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"
4specialPart.Name = "SpecialPart"
5--elsewhere
6specialPart = workspace.SpecialPart --this will only return a Part, not the SpecialPart created above
7print(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