How can I fix this lua runtime error in pico-8?

378 views Asked by At

I'm following your roguelike tutorial and have encountered a problem I do not know how to solve. This is my first-time coding with Lua

If r.nospawn then return 0

--Attempt to index local "R" (a nil value)

I asked the PICO-8 discord server, they tried to help me, but I still don't fully understand, and I did not want to pester them further with the issue. The name of my file on PICO-8 is called Rogue - if that has anything to do with the issue.
Here's a picture of the error, the discord comment I received, and a link to the full list of code on GitHub.

Error in PICO-8
1

Discord Comment
2

Github Code

1

There are 1 answers

0
Martin On

I think you are missing a simple failure case. i.e. what happens when all the entries in rpot have been removed. Then local r=getrnd(rpot) should return null. That might be an error in it's own right i.e. there should always be something to allocate from there.

However getrnd will fail.

function getrnd(arr)
 return arr[1+flr(rnd(#arr))]
end

In the case when arr is empty you will try and return element 1, which will be out of bounds. I don't know lua but it might return null for you which leads to the next problem. But that has a simple fix:

repeat
  local r=getrnd(rpot)
  if r
    placed+=infestroom(r)
    del(rpot,r)
  end
 until #rpot==0 or placed>maxmons[floor]