Object-Oriented Programming: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
Pasted in from another page. Needs tidying up.
>JulienDethurens
(It's the lua-users wiki, not the Lua-users wiki.)
>NXTBoy
(Pasted in from another page. Needs tidying up.)
Line 124: Line 124:
Account(80) Account(240)
Account(80) Account(240)
}}
}}
==Another example==
A custom object is something that you create that has [[Properties|properties]] like the other ROBLOX objects, such as game.Workspace.Brick.Transparency
===Declaring an object===
Your data type here will be a "Vector2".
<code lua>Vector2 = {} --this declares the class "Vector2"</code>
<code lua>
function Vector2.new(one, two) --the function that we will use to create new objects of this class
newTB = {x, y} --this makes our new object with the properties, which you set as objects in the  table
newTB.x = one --set the value to the one called in the argument
newTB.y = two
return newTB
end
</code>
Now, we can call a new object of the class as such:
<code lua>
a = Vector2.new(7, 8)
print(a.x) --> 7
print(a.y) --> 8
a.x = 10
print(a.x) --> 10
</code>
===Adding member functions to your object===
<code lua>
function Vector2.new(one, two)
newTB = {x, y}
newTB.x = one
newTB.y = two
function newTB.reset()
newTB.x = 0
newTB.y = 0
end
return newTB
end
a = Vector2.new(5, 8)
print(a.x) --> 5
print(a.y) --> 8
a:reset()
print(a.x) --> 0
print(a.y) --> 0
</code>


== See also ==
== See also ==
Anonymous user

Navigation menu