'then' expected near ',' but I already place then with Lua

6.6k views Asked by At

As a complete beginner I'm experimenting on a small code on Pico-8 :

function _update()
if p.x, p.y == 64, 45 then
 cls()
    print("dead", 37, 70, 14)
 end
end

And when I try to run the program an error message appear which says :

'then' expected near ','

I've searched a lot but never found an answer. Can somebody help?

2

There are 2 answers

4
Chris H On

You're trying to use an if formatted:

if p.x, p.y == 64, 45 then
  [...]
end

You can assign values to multiple variables with syntax a little like what you've used here. For example,

p.x, p.y = 64,45 would set p.x to 64 and p.y to 45. But you can't check for equality on multiple variables in that way. You need to check each variable separately:

if p.x == 64 and p.y == 45 then
  [...]
end

Your code tries to use p.x, p.y == 64, 45 as a condition for your if branch. This doesn't work because lua doesn't understand this to mean p.x == 64 and p.y == 45. Instead it understands it as a list of unrelated statements (p.x, p.y == 64, and 45), and trips up on the comma between p.x and p.y.

1
brianolive On

Assigning values to variables this way is ok:

a, b = 1, 2

For the if statement though, you'll need to do this:

if a == 1 and b == 2 then
    -- do something
end