i have separate character files that i want to be able to have an inventory for, and i also need to be able to add things to the inventory as the characters gain items (weapons,armour, potions etc.) Somebody suggested having the items in an array, however can i set the array to have a maximum, for example i want my inventory to have a max amount of spaces so that items cannot be just accumulated indefinitely.
How to create an Inventory using an array?
- Edited
Sure you can.
Let's say you have an array with 50 elements, something like var array[50]
If you pickup an item, iterate over the array to find free slot and put item there. If there are no free slots, don't pick up the item
Pseudocode:
var inventory[50];
function pickup(picked_item):
for i in range(50):
if inventory[i] == None:
inventory[i] = picked_item.id;
Pseudocode: `var inventory[50];
can you please explain what this 50 does, does this set the maximum or is it a placeholder for the array?
for i in range(50):
or is this the part that sets the maximum? and what does the i signify?
if inventory[i] == None: inventory[i] = picked_item.id;`
- Edited
Oh well, it'd be nice if you can read official tutorials: [http://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_advanced.html#arrays](http:// "http://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_advanced.html#arrays") there's info about arrays and loops
@Danicron said:
Pseudocode: `var inventory[50];
can you please explain what this 50 does, does this set the maximum or is it a placeholder for the array? that's how we declare array with 50 elements in most languages, I'm not sure if that's the same with gdscript, but again - pseudocode
another way is to create a size variable, var size = 20
and in a pickup function checking if the current size of inventory array is more than size variable
var inventory = [];
var size = 30;
function pickup(item):
if len(inventory) < size:
inventory.append(item.id)
for i in range(50):
or is this the part that sets the maximum? and what does the i signify?
for
means iterating over a fragment of the code in the quoted example we iterate 50 times