Log In  

I've written some functions based on Dylan Bennet's tutorial "PICO-8 Top-Down Adventure Game Tutorial - Bonus Step - Text". I tried to adapt it to taking gold out of chests (I know the next video in the series partly covers that, but that solution assumes every chest in the game gives the same amount). Ideally they should output a certain number based on an x,y coordinate, which other functions could use.
'''
function set_chests()
chests={}
add_chest(7,3,9)
end

function add_chest(x,y,num)
chests[x+y*128]=num
end

function get_chest(x,y)
return chests[x+y*128]
end
'''

get_chest should return 9 when the coordinates (7,3) are put in, which could then be added to another variable. Instead touching the chest at (7,3) does this:
RUNTIME ERROR LINE 13 TAB 6
return values[x+y*128]
attempt to index global 'values' (a nil value)

I'm not in any rush to fix this, but the fact I have no idea why it's happening bothers me. Advice would be very appreciated.

P#106413 2022-02-06 06:39 ( Edited 2022-02-06 06:50)

The error suggests your code is different from the one you posted. The error talks about the variable "values" but the code you posted talks about the variable "chests"?

P#106417 2022-02-06 07:24

Sorry that was a mistake copying the error into the post. Here is a screenshot of the error after I finished fiddling with it yesterday.

P#106436 2022-02-06 15:03
1

I wonder, are you calling the 'set_chests' function at the beginning of your code? You're initializing the 'chests' table inside of the function, and that means unless you run it before any other functions that use the table, you'll be trying to reference something that doesn't exist yet.

P#106438 2022-02-06 15:38
1

An hour ago I was hit with whiplash from thinking "eureka! I need to use init!" and then "I'm such a dork to have missed that in the first place."

Minutes later I checked this post and someone had already noticed. Slightly embarrassing.

I've added set_chests() to _init() and it works perfectly now. That's what I get for programming when I'm dead tired and not thinking through how everything will work when it's done.

Will I learn from my mistakes? Probably not!

Thank you @Krystman and @JadeLombax

P#106448 2022-02-06 18:15

[Please log in to post a comment]