trouble with metatables

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble with metatables

Matt Eisan
Hello, I'm trying to create 3D vector object to try to get the hang of how metatables and pseudo-classes work. I figured it would be simple enough. However, I cannot figure out why this code is not working, (I'm probably doing something really silly wrong, but hey, that's how you learn.) 

Thanks in advance for any help.
Reply | Threaded
Open this post in threaded view
|

Re: trouble with metatables

Choonster TheMage
On 6 July 2013 12:49, Matt Eisan <[hidden email]> wrote:
>
> Hello, I'm trying to create 3D vector object to try to get the hang of how metatables and pseudo-classes work. I figured it would be simple enough. However, I cannot figure out why this code is not working, (I'm probably doing something really silly wrong, but hey, that's how you learn.)
>
> Thanks in advance for any help.

You put the `new` and `setX` methods in the VECTOR table, but the
vector __index metamethod (which you've set as a table of default x, y
and z values) completely ignores this table.

Either put the methods in the same table as the defaults and set that
as the __index metamethod or use a function to return the appropriate
method or default value.

I've attached a working version of your script.

vector.lua (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: trouble with metatables

Matt Eisan
Well I knew it was something silly. Thank you, I've been trying to figure this out for hours. 


On Fri, Jul 5, 2013 at 11:09 PM, Choonster TheMage <[hidden email]> wrote:
On 6 July 2013 12:49, Matt Eisan <[hidden email]> wrote:
>
> Hello, I'm trying to create 3D vector object to try to get the hang of how metatables and pseudo-classes work. I figured it would be simple enough. However, I cannot figure out why this code is not working, (I'm probably doing something really silly wrong, but hey, that's how you learn.)
>
> Thanks in advance for any help.

You put the `new` and `setX` methods in the VECTOR table, but the
vector __index metamethod (which you've set as a table of default x, y
and z values) completely ignores this table.

Either put the methods in the same table as the defaults and set that
as the __index metamethod or use a function to return the appropriate
method or default value.

I've attached a working version of your script.