1

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

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • There's too much code to analyze but brettski has a lead there. I guess using oldschool [printf debugging](https://pico-8.fandom.com/wiki/Printh) (start in spawnmobs) will sooner or later let you find an answer. – wonko realtime Apr 01 '22 at 09:14

1 Answers1

0

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]
Martin
  • 2,316
  • 1
  • 28
  • 33