View previous topic :: View next topic |
Author |
Message |
kiddietron Site Admin
Joined: 06 Feb 2008 Posts: 797 Location: On Roblox
|
Posted: Sun May 11, 2008 1:32 am Post subject: Making Custom Objects by xLegox |
|
|
Ever wonder how you can make object with a set of properties like the Roblox ones? Well here's how...
Your data type here will be a "Vector2"
Vector2 = {} --this declares an object of the class "Vector2" (yes, it is a table as well)
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, wich 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
--now we can call a new object of the class as such...
a = Vector2.new(7, 9)
print(a.x) --> 7
print(a.y) --> 9
a.x = 10
print(a.x) --> 10
------------------------------------------
Quite simple isnt it?
How can you add member (methods, deemed properly) functions to your object though?
-------------------------------------------
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, 7)
print(a.x) --> 5
print(a.y) --> 7
a:reset()
print(a.x) --> 0
print(a.y) --> 0
---------------------------------------------------------
Qute simple again eh?
I haven' yet figured out how to build these into the hierarchy of objects that roblox uses though, so you can't set an objectvalue's value to one of these objects, or make its parent something.
The main purpose of this is to easily work with data in large scripts. _________________ �Kiddietron
�Phailing at making a signature
Free Money here just sign up and click away.
http://bux.to/?r=kiddietron
Spam is for noobs.
Last edited by kiddietron on Sun May 11, 2008 1:39 am; edited 1 time in total |
|
Back to top |
|
|
jtm110972 Beginner Poster
Joined: 06 Feb 2008 Posts: 33 Location: Sitting infront of a laptop
|
Posted: Sun May 11, 2008 1:35 am Post subject: |
|
|
Twice, a smiley messed it up...... _________________ ->jtm110972<-
<a><img></a>
|
|
Back to top |
|
|
kiddietron Site Admin
Joined: 06 Feb 2008 Posts: 797 Location: On Roblox
|
Posted: Sun May 11, 2008 1:39 am Post subject: |
|
|
Thanks I fixed it now. _________________ �Kiddietron
�Phailing at making a signature
Free Money here just sign up and click away.
http://bux.to/?r=kiddietron
Spam is for noobs.
|
|
Back to top |
|
|
SirGelatina Site Admin
Joined: 01 Mar 2008 Posts: 116 Location: Behind you
|
Posted: Sun May 11, 2008 3:40 am Post subject: |
|
|
I used this concept in WitchCraft. The problem of this concept is that #Vector2 return 0, not 2 (Because of x and y properties) |
|
Back to top |
|
|
CyberDonkey Post-A-Holic
Joined: 06 Feb 2008 Posts: 680 Location: Location: Location:
|
Posted: Sun May 11, 2008 10:26 am Post subject: |
|
|
im not good at scripting
but neither am i bad.
though, i haven't learn "TB" _________________ ~CyberDonkey~
~Forever Lame~ |
|
Back to top |
|
|
chess123mate Beginner Poster
Joined: 06 Feb 2008 Posts: 40 Location: Canada
|
Posted: Sun Jul 13, 2008 12:53 am Post subject: |
|
|
Um... it doesn't work.
It prints out:
7
nil
0
0
Instead of what you said it should:
5
7
0
0
Any explanations? |
|
Back to top |
|
|
DingDong272 Avid Poster
Joined: 26 Jun 2008 Posts: 129 Location: United States
|
Posted: Sun Jul 13, 2008 1:43 pm Post subject: |
|
|
The arguments get shifted a position. Do this:
print(nil, a.x)
print(nil, a.y) _________________ .:�ing�ong272:.
.:�elta Corp Avid Poster:. |
|
Back to top |
|
|
chess123mate Beginner Poster
Joined: 06 Feb 2008 Posts: 40 Location: Canada
|
Posted: Sun Jul 13, 2008 3:10 pm Post subject: |
|
|
Then how can this concept be useful at all? You wouldn't know when an argument has been shifted and when it hasn't. |
|
Back to top |
|
|
Anaminus Beginner Poster
Joined: 08 Feb 2008 Posts: 17 Location: Location: Location: Location: Location: Location:
|
Posted: Sun Jul 13, 2008 7:19 pm Post subject: |
|
|
This works better:
Code: | Vector2 = {}
function Vector2.new(x,y)
local object = {x = x, y = y}
function object:reset()
self.x = 0
self.y = 0
end
return object
end |
?-Anaminus-? _________________ ?-Anaminus-?
^^You're probably seeing this twice because I typed in my signature >_< |
|
Back to top |
|
|
DingDong272 Avid Poster
Joined: 26 Jun 2008 Posts: 129 Location: United States
|
Posted: Sun Jul 13, 2008 7:32 pm Post subject: |
|
|
I don't see any declaration of, "self," in there.
How is it going to work?
.:�ing�ong272:. _________________ .:�ing�ong272:.
.:�elta Corp Avid Poster:. |
|
Back to top |
|
|
chess123mate Beginner Poster
Joined: 06 Feb 2008 Posts: 40 Location: Canada
|
Posted: Sun Jul 13, 2008 9:36 pm Post subject: |
|
|
"self" must be a special variable in lua. It works
Thanks! |
|
Back to top |
|
|
Anaminus Beginner Poster
Joined: 08 Feb 2008 Posts: 17 Location: Location: Location: Location: Location: Location:
|
Posted: Mon Jul 14, 2008 2:09 am Post subject: |
|
|
self automatically refers to the parent of the member function.
?-Anaminus-? _________________ ?-Anaminus-?
^^You're probably seeing this twice because I typed in my signature >_< |
|
Back to top |
|
|
DingDong272 Avid Poster
Joined: 26 Jun 2008 Posts: 129 Location: United States
|
Posted: Mon Jul 14, 2008 1:59 pm Post subject: |
|
|
Really? That's pretty cool. _________________ .:�ing�ong272:.
.:�elta Corp Avid Poster:. |
|
Back to top |
|
|
chess123mate Beginner Poster
Joined: 06 Feb 2008 Posts: 40 Location: Canada
|
Posted: Mon Jul 14, 2008 9:44 pm Post subject: |
|
|
Ah! The main changes necessary were
function name.action()
rather than
function name:action()
And vice-versa for properties; ":" instead of "." for internal functions.
And the use of "self"
Now I can use these things! XD
Thanks again, Anaminus! |
|
Back to top |
|
|
DingDong272 Avid Poster
Joined: 26 Jun 2008 Posts: 129 Location: United States
|
Posted: Tue Jul 15, 2008 2:24 am Post subject: |
|
|
When deeming a member function, you always want to use a period ( . ), not a colon ( : ), because using a period to call the function might not, "behave," correctly. _________________ .:�ing�ong272:.
.:�elta Corp Avid Poster:. |
|
Back to top |
|
|
|