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 |
|
|
| |
|
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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 27 Sep 2011 07:15 PM |
| I seriously recommend you to make your objects userdatas, using newproxy(true). |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 27 Sep 2011 11:14 PM |
| Userdatas are different from tables. There are some metamethods that only work for userdatas. |
|
|
| Report Abuse |
|
|