Page 1 of 1

Unable to compare two tables using metatables.

Posted: Wed Jun 14, 2017 4:14 am
by quevigil
Hi,

I'm trying to learn programming by coding a simple puzzle game in LOVE. Right now there are no controls. Anyway, I'm trying to use .png files to create a map by using color codes to denote different parts of the map. While doing so I realized I could use metatables to simplify color equalities by comparing {r,g,b,a} tables at every pixel on the map, which I would later use to draw the appropriate tiles.

I wrote a metamethod for the equality operator which I translated into python to test for validity.(attachment: equalityTesting.txt)

Unfortunately, the equality operator seems to not be working. It never returns true, hence drawing nothing on the screen. (Line 41: maploader.lua; attachment: gameFolder.love)

Comments at the beginning of main.lua may be useful.

Where did I go wrong?

Re: Unable to compare two tables using metatables.

Posted: Wed Jun 14, 2017 7:01 am
by bartbes
You're not applying the metatable to the tables in "color". On line 10 of colors.lua use pairs instead of ipairs and it works.

Re: Unable to compare two tables using metatables.

Posted: Wed Jun 14, 2017 9:32 am
by quevigil
Oh my god. THANK YOU!

On a side note, I had been checking notifications constantly and your reply did not show up. I'm so glad to see that they do not lie about the niceness of this community! Thank you very much once again!

Re: Unable to compare two tables using metatables.

Posted: Wed Jun 14, 2017 10:09 am
by Skeletonxf
For your particular scenario you can actually go even further with memoization

https://www.lua.org/pil/17.1.html

If you have a color factory that remembers the previous color/tables and returns them when asked for the same ones then you can compare colors using == with no meta tables.

Re: Unable to compare two tables using metatables.

Posted: Wed Jun 14, 2017 9:02 pm
by davisdude
quevigil wrote: Wed Jun 14, 2017 9:32 am Oh my god. THANK YOU!

On a side note, I had been checking notifications constantly and your reply did not show up. I'm so glad to see that they do not lie about the niceness of this community! Thank you very much once again!
I believe you only get notifications if you're quoted, or if you're subscribed to the topic (see the wrench next to "Post Reply")

Re: Unable to compare two tables using metatables.

Posted: Thu Jun 15, 2017 1:02 am
by quevigil
davisdude wrote: Wed Jun 14, 2017 9:02 pm I believe you only get notifications if you're quoted, or if you're subscribed to the topic (see the wrench next to "Post Reply")
I understand now. Thank you.