-1

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?

piet.t
  • 11,718
  • 21
  • 43
  • 52
The_berrichon
  • 11
  • 1
  • 3

2 Answers2

4

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.

Chris H
  • 790
  • 9
  • 19
  • You got the answer in just as I was typing - my apologies! – brianolive Aug 01 '18 at 12:35
  • 1
    @Brian happens often enough, no need to apologise! – Chris H Aug 01 '18 at 12:40
  • the program runs now but i can't move and the two "characters" spawn at the same place – The_berrichon Aug 01 '18 at 13:03
  • @The_berrichon That sounds like an unrelated problem, even if it's in the same program as the thing you originally asked about. StackOverflow comments are designed for clarification or suggesting improvements, and aren't convenient for follow-up questions and additional answers. You should open a new question devoted to the new problem you're having. – Chris H Aug 01 '18 at 13:08
0

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
brianolive
  • 1,573
  • 2
  • 9
  • 19