generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: RBX.Lua Jury-rigged OOP

Previous Thread :: Next Thread 
Davidii is not online. Davidii
Joined: 17 Jul 2008
Total Posts: 1282
27 Sep 2011 06:57 PM
I've been trying it out just a bit, as it has a lot of potential uses. However, I've been having trouble getting methods to work correctly. Someone who knows what there doing and isn't going to troll me (very rare on this forum, I know), please show me how you do it.
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 06:58 PM
"Jury-rigged"?
Report Abuse
Davidii is not online. Davidii
Joined: 17 Jul 2008
Total Posts: 1282
27 Sep 2011 07:00 PM
Well, it means that you kinda throw it together to get the same result, even though it isn't actually the same as the real thing. Like, making a gun by using a lead pipe and duct tape with a wooden block. That'd be a jury-rigged gun. But OT, somebody show me how you do it.
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 07:05 PM
Well, for object oriented programming, I usually simple use something like this:

local Object = newproxy(true)
getmetatable(Object).__index = {
-- Put content of the object here: methods, attributes, etc
}

Of course, that doesn't support heritance or clonage or all that.

To make a function compatible with being called using ':', just make it this way:

local obj = {}

function obj:add(value) -- the ':' is syntactic sugar to automatically add an argument called 'self' representating the object.
table.insert(self, value)
end

And to call:

obj:add(4) -- the ':' is syntactic sugar to automatically add the value you are calling the method on (in this case, obj) in the argument list, as the first argument.

You could also do it this way, which is exactly the same:

function obj.add(self, value)
table.insert(self, value)
end

And to call:

obj.add(obj, 4)

Oh, and, btw, for clonage, here's a nice way to clone tables that I learned from the lua users wiki:

t = {6, 3, a = 9)

clone = {unpack(t)} -- Now, clone will be a perfect copy of t, except the metatable, but you can clone the metatable easily.
Report Abuse
Davidii is not online. Davidii
Joined: 17 Jul 2008
Total Posts: 1282
27 Sep 2011 07:09 PM
Ooooh, thansk. I didn't know I had to use the "syntactic sugar" as you describe it to give my arguments the special self argument on call.

I was doing mine like this:

function createObject(name, value1, value2)

local object = {}

object.name = name
object.value1 = value1
object.value2 = value2

object.someFunction = function(self, args, args, andmoreargs)

self.value1 = args + args + andmoreargs

end

end

It was unhappy with my "self" call in someFunction.
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 07:14 PM
"I didn't know I had to use the "syntactic sugar" as you describe it to give my arguments the special self argument on call."

You don't HAVE to use it, but since you can use it, why not use it? =)

I suggest you to look there:

lua-users dot org slash wiki slash ObjectOrientedProgramming
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 07:15 PM
I seriously recommend you to make your objects userdatas, using newproxy(true).
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Sep 2011 09:32 PM
Syntactic sugar makes your code taste good.


~ I r unicorn. ujelly? ~
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 09:41 PM
@pwnedu46

That is quite right. Heh, that's the reason they call it "syntactic sugar", anyways.

It's both funny and true. =)
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
27 Sep 2011 09:53 PM
What exactly is the point of making a userdata when it basically has the same features as a table?
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
27 Sep 2011 09:58 PM
"What exactly is the point of making a userdata when it basically has the same features as a table?"

Because it's OOP. We like overcomplicating things. :D


~ I'm a unicorn! ~
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
27 Sep 2011 11:14 PM
Userdatas are different from tables. There are some metamethods that only work for userdatas.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image