#Help me with unlocking the chest :)

1 messages · Page 1 of 1 (latest)

vast jungle
#

Hey guys, I have problem with locked chest.

I tried to do this myself and my logic was that I will add the key as an item to the chest variables and when player will come to the chest and tries to open it will check it the player has in his inventory the same key as is added to the chest.
If player has the key in his inventory the chest would open.

Unfortunately I am getting an error and I can't figure out how to write this function correctly.

Any help would be appreciated, thanks!

stable cedar
#

first thing:

#

this might not work as you expected

#

mixing or and and

#

maybe add paranthesis to the second part

#

althoug the is_locked can be removed

#

you can just check if is locked or playe has key

#

now th error

#

now let's try to find the real error

#

the find() function returns an int, not a boolean

#

the function you are looking for is has()

vast jungle
#

Thank you i will try to rewrite it

stable cedar
#

do you have any errors in the key resource script?

vast jungle
#

hmmm

#

this is the function now

#

i dont see more errors, just this one

#

when I tested it with if !is_locked or key_required:
and I removed the key from the chest it was locked and when I added key back it opened

#

i think checking if the player has the same item in inventory is kinda whacky

stable cedar
#

humm, what is the array type?

#

is SlotData and ItemDataKey extending the same class?

vast jungle
#

SlotData is a resource

#

and ItemDataKey extends ItemData

#

ItemData is a resource

stable cedar
#

humm, but they cannot be check against each other

#

it seems that is what is happening

#

is the ItemData set to the SlotData?

#

because you are cheking if the ItemDataKeys exists in an Array with SlotData

#

which I assum you static typed to that

#

array:SlotData

vast jungle
stable cedar
#

yeah, that is the problem

#

you check if the key exists in the slots_datas array

#

but that array only has SlotData

vast jungle
#

makes sense

#

so the path for the check needs to go inside SlotData and check there?

stable cedar
#

you are gonna have to loop the array and check the item_data in each entry

#
func has_item(item) -> bool:
    for slot in slot_datas:
        if slot.item_data == item and slot.quantity > 0: return true
    return false
#

you can have a function like this

vast jungle
#

ok progress

#

so now when I pick key from the ground and try to open locked chest it opens ❤️

#

but when I dont have the key in my inventory game crashes with error

#

maybe I wrote this wrongly

#

but it looks allright to me

#

and if has_item returns false it should just make the locked label visible right

#

big thanks for your help thiago I was stuck on this for like two days

stable cedar
#

it looks like you have empty entries in the slots array

#

I recommend either not having empty entries there or check if slot is null before testing for the item

vast jungle
#

this fixed it, it works!

#

thank you very much again for you help and time

#

i learned a lot