XavierEmeralds
XavierEmeralds

Reputation: 1

Find first empty slot in an array in Godot 3.2.3

I have an inventory array that I want to find the first empty slot in the said array and put the Resource the player picks up into that slot. I'm a little familiar already with C programming and am pretty sure that a for loop would work nicely, I just can't seem to get it to work for me.

Upvotes: 0

Views: 1453

Answers (1)

James Westman
James Westman

Reputation: 2690

You can use Array.find to find the index of first element of your array that is null (assuming you're using null to indicate empty inventory slots).

It will return the index of the first empty slot, in which case you'd do inventory_array[index] = picked_up_item. Or, if the inventory is full (i.e. there is no null element), it will return -1, in which case you should do something reasonable (like not pick the item up).

Upvotes: 1

Related Questions