Unable to compare two tables using metatables.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
quevigil
Prole
Posts: 7
Joined: Sat Jun 10, 2017 4:00 am

Unable to compare two tables using metatables.

Post 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?
Attachments
gameFolder.love
(5.6 KiB) Downloaded 191 times
equalityTesting.txt
(423 Bytes) Downloaded 192 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Unable to compare two tables using metatables.

Post 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.
quevigil
Prole
Posts: 7
Joined: Sat Jun 10, 2017 4:00 am

Re: Unable to compare two tables using metatables.

Post 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!
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: Unable to compare two tables using metatables.

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Unable to compare two tables using metatables.

Post 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")
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
quevigil
Prole
Posts: 7
Joined: Sat Jun 10, 2017 4:00 am

Re: Unable to compare two tables using metatables.

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 84 guests