Astrid
Astrid

Reputation: 1312

Lua tables check if any variable matches any value

In Lua (Codea on ipad) i have made a program where there are four pairs of X-Y coordinates , and these are put in tables under the same id (count = count + 1) When i first tested the code using only one pair, to detect when the X-Y coordinates touch one of the coordinates in the table (where the coords have already been). I did this using this bit of code:

if (math.abs(xposplayer - posx) < 10) and (math.abs(yposplayer - posy) < 10) and id < (count - 10) then

This bit of code is being played in this loop:

for id,posx in pairs(tableposx) do
posy = tableposy[id]

This worked just like i want it to!

But now i have is with 8 tables (tableposx1 tableposy1, ...) And i would like to check if any the the current coordinates touches any of the coordinates in any of the tables (ever) so i tried:

for id,posx1 in pairs(tableposx1) do
posy1 = tableposy1[id]
posy2 = tableposy2[id]
posx2 = tableposx2[id]
posy3 = tableposy3[id]
posx3 = tableposx3[id]
posy4 = tableposy4[id]
posx4 = tableposx4[id]

And this bit four times (for the four current coordinates)

if ((math.abs(xposplayer1 - posx1) < 10) and (math.abs(yposplayer1 - posy1) < 10))
or ((math.abs(xposplayer1 - posx2) < 10) and (math.abs(yposplayer1 - posy2) < 10))
or ((math.abs(xposplayer1 - posx3) < 10) and (math.abs(yposplayer1 - posy3) < 10))
or ((math.abs(xposplayer1 - posx4) < 10) and (math.abs(yposplayer1 - posy4) < 10))
and (id < (count - 10))

But this always (almost) goes true. And becaues sometimes the the values in the table are NIL it will throw me an error saying it cant compare something to a nil value.

Thanks in advance, Laurent

Upvotes: 1

Views: 550

Answers (2)

Mike Corcoran
Mike Corcoran

Reputation: 14565

You should probably be organizing these values using tables. Use a table for positions, that holds a series of 'coordinate' tables. this way you can iterate over all the coordinates with a for loop, and be sure that each item in the table represents coordinate pairs that you can write somewhat generic functions to test validity for.

function GetNewCoords(x_, y_)
    x_ = x_ or 0
    y_ = y_ or 0
    return { x = x_, y = y_}
end

function CoordsAreValid(coords)
    if (coords == nil) return false
    return coords.x ~= 0 or coords.y ~= 0
end

local positions = {}
table.insert(positions, GetNewCoords(5, 10))
table.insert(positions, GetNewCoords(-1, 26))
table.insert(positions, GetNewCoords())
table.insert(positions, GetNewCoords(19, -10))

for _, coords in pairs(positions) do
    if (CoordsAreValid(coords)) then
        print(coords.x, coords.y)
    end
end

Upvotes: 1

vertti
vertti

Reputation: 7889

Start by removing the copy paste code. Use something like posy[n] instead of posy1, posy2... And same for the other: tableposy[n][id] instead of tableposy1[id]..

After that you can use loops to do the comparisons in one line. And you can refactor the comparison into a function where you do nil check before the comparison.

Upvotes: 2

Related Questions