How do I fix this PICO-8 error? ')' expected near '='

95 views Asked by At

So I started working with the PICO-8 web version this week, and I wanted to make a simple dungeon crawling game. I started working on the code and got the player sprite moving left and right. The next step was to keep the player on the screen.

So I began by using a boolean variable to keep track of whether the player was touching the edge of the screen: INBOUNDS. Then I tried using an if statement to determine whether the x coordinate was in the limits:

  -- CHECK IF PLAYER IS IN THE SCREEN
  IF (X = 0 OR X = 128)
    INBOUNDS = FALSE
  END

When the program ran, however, I got an error in the console that read:

SYNTAX ERROR LINE 22 (TAB 0)
  IF (X = 0 OR X = 128)
')' EXPECTED NEAR '='

Here is the link to the cartridge file: https://drive.google.com/file/d/1H0snkt8oza1tXiy0__kTVUX7DMYvdJSr/view?usp=sharing

If you need any more info, please ask.

Thanks!

1

There are 1 answers

0
datashaman On

In this line, you are using the assignment operator = instead of equality operator ==:

if (x = 0 or x = 128) inbounds = false

And in these lines, you are using bit-wise & operator instead of logical and:

if (btn(0) & inbounds) x -= 1
if (btn(1) & inbounds) x += 1

Here is a version close to yours that runs correctly:

sprite = 8
x = 1
y = 1
timer = 0.25
inbounds = true

function _update()
  -- clear screen
  cls(1)
  
  -- check if player is in the screen
  if (x == 0 or x == 128) inbounds = false
  
  -- get keypresses
  if (btn(0) and inbounds) x -= 1
  if (btn(1) and inbounds) x += 1
  
  -- draw sprite
  spr(sprite, x, y)
end

You are clearing the screen and rendering the sprite in the _update function which is (I think) not correct.

You should only change state in the _update function, and all graphics code (including cls and spr) should be called in the _draw function.

Here is a slight rewrite which has the correct structure, clamping the player's dimensions to a fixed range using the mid function:

sprite = 8
x = 64
y = 64

function _update()
  -- get keypresses
  if (btn(0)) x -= 1
  if (btn(1)) x += 1
  if (btn(2)) y -= 1
  if (btn(3)) y += 1

  -- clamp dimensions (factor in size of sprite)
  x = mid(0, x, 127-8)
  y = mid(0, y, 127-8)
end

function _draw()
  cls(1)
  spr(sprite, x, y)
end

Here's some more info on the mid function: https://pico-8.fandom.com/wiki/Mid