XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 06 Feb 2017 06:07 PM |
| how can metafunctions be used to define methods, i.e: part:Explode() (creates an explosion within said part at that part's location then destroys/deletes the part) |
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 06 Feb 2017 06:08 PM |
| i meant metamethods but u get the idea |
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 06 Feb 2017 06:08 PM |
| please don't give a response like: "__index can be a function or a table", im aware of that much. |
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Feb 2017 06:17 PM |
You don't have to use metatables. If you insist: "__index can be a function or a table"
local t = setmetatable({1, 2, 3}, {__index=table}) print(t:concat(", ")) |
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 06 Feb 2017 06:18 PM |
| how else would you go about it then? i don't want to use a function |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 06 Feb 2017 06:21 PM |
| i know but u said there were other ways besides that |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Feb 2017 06:22 PM |
local a = { x = 1, printX = function(self) print(self.x) end }
a:printX() |
|
|
| Report Abuse |
|
|
|
| 06 Feb 2017 06:22 PM |
xinst = newproxy(true) proxytable = {} print(type(xinst)) --> userdata setmetatable(xinst, {__index = proxytable})
function proxytable:printhi() print("hi from " .. self) end
xinst:printhi()
i don't think i messed up, that should print "hi from userdata: some hexadecimal memory address"
|
|
|
| Report Abuse |
|
|
|
| 06 Feb 2017 06:24 PM |
oh i think you have to do getmetatable(xinst).__index = proxytable instead of using setmetatable
pretty much the same though
|
|
|
| Report Abuse |
|
|
AxonMega
|
  |
| Joined: 29 Aug 2014 |
| Total Posts: 2403 |
|
|
| 06 Feb 2017 06:24 PM |
You don't need metatables to define methods, only metamethods. You can use metatables for that if you want; when you index a table, Lua checks if there is value at the index in the metatable.
And my friggin autocorrect keeps changing metatable to meatball. |
|
|
| Report Abuse |
|
|
|
| 06 Feb 2017 06:24 PM |
@cntkillme
But that does not add methods to an existing instance.
|
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Feb 2017 06:26 PM |
"But that does not add methods to an existing instance." It's impossible to do that. The closest you can get is by wrapping your instance and everything else so it can handle wrapped instances correctly and pretend it's the real instance. |
|
|
| Report Abuse |
|
|
|
| 06 Feb 2017 06:27 PM |
^
Which is the same thing, at least to the caller.
|
|
|
| Report Abuse |
|
|