` I was trying to create a simple inventory and item selection program for the player, and I represented the “inventory slots” using buttons. I have it set so that the slots which are not in use are hidden and disabled, so the player should only be able to use buttons or “inventory slots” which are currently holding items.
I wanted it to be that when the player presses one of the buttons while in a specific location, that they’ll be able to use the item in that button.
I hadn’t gotten very far besides trying to set the “selected item” variable to the item held in the button. This is because every time I tested it, I would get an error where it would say that the array I was picking from was empty.
This didn’t make any sense because I made it so that you can’t pull up the item selection without the required item anyways. I did a little testing and found out that the array would always have items in it right before I ran the code, but once I pressed the button the array would always show up empty. I tried it with a few arrays I had for this same script, and all of them showed up as empty.
I checked and doubled checked the documentation on arrays, so I’m not sure there’s an error in my notation.
Could someone let me know a possible solution? Or if there is an error in my code? I’ll also say that the script processing the button commands is a global script, I don’t know if that changes anything.`
I’m not sure where’s going wrong, you need to post more code about how you initialize and use it, but I’ll show you this array example for now.
# An array can store a sequence of values
var array = [] # This is empty
var fruits = ["orange", "berry", "melon"] # This array has 3 values
array.push_back("Hello") # array is now ["Hello"]
array.push_back("World") # it is now ["Hello", "World"]
fruits[2] # This will give you the third item, "melon" (count from zero)
array[100] # Error! This array only contains 2 items
Okay, I start off with this script attached to a “door”. The “player_near” variable is turned on when they’re in the door’s Area2D. The “current” is an array with the current collected items. It references another script, Inventory, which is a global/autoload script.
func _unhandled_input(event: InputEvent) → void:
if event.is_action_pressed(“interact”) && player_near == true && name == “House2Base”:
if Inventory.current.has(“Key”) == true:
Inventory.start_item_selection(“Locked Door”)
This part of the script seems to work okay.
Then going to the Inventory script, the function that’s been called is this one:
The PauseManager is another global, in this case it’s calling a function that stops the physics processing of the player. That part works okay. The next line emits a signal which is received by the item slots, which are all buttons with the same code.
Their code reads it and does this:
func item_select_mode():
modulate.a = 1
if not item_type == null:
set_disabled(false)
Changing the opacity to make them more opaque, and then (if they have an item “in” them) making them not disabled so that the player can press on the separate slots. Each slot is has a unique icon that it takes depending on the item it’s carrying.
From there it just goes back to the button pressed or “slot_pressed” code, which seems to be the issue. I only have around 5 buttons, so that’s why it’s labeled “slot_0” for the first one.
Your initial question was about the issue with the array being empty - I assume you’re talking about the slot_types array that you mentioned in your first post, correct? Share us all the code that initiates and modifies this slot_types array then - this is the most important part of the puzzle.
And please use preformatted text functionality with ``` when sharing code snippets, not quotes.
I have this at the beginning of the code: var slot_types = []
And then this later on:
func update_slots():
slot_types.clear()
var open_slots = 0 #keeps track of slots accounted for
for n in len(current): #will repeat for as many items are in the inventory
slot_number = "Slot" + str(open_slots)
item = current[n]
if counted.has(item) && (n+1) != len(current): # skips duplicate items, inventory slots script accounts for multiples
n +=1
elif counted.has(item) == false: #NEW item type
slot_types.append(item) # used in item selection
counted.append(item) # counts new items entered into slots
open_slots +=1
update_inv.emit(item, slot_number)
counted.clear() #clears this array so we don't double count items
item = null #resets counted item
This function updates slot_types to include all of the items held in the current array, so it shouldn’t be empty. Even when I check the script right before I hit one of the buttons, slot_types usually has items in it like I expect. It’s only when I hit the button that it seems like it empties itself, slot_types and even every other array in the same script.
When I click the button “Slot0”, it leads to a function that gives an error because it reads the array “slot_types” as empty. But only after I press the button.
Can you paste the piece of code that handles the click?
Edit: also a second thought - could it be that you’re loading the same Node twice? Firstly as part of your main scene, and secondly as an autoload? You can add some print statements to see if these are printed once or twice.