[Solved] Array Methods not working

So, basically, I’m making an inventory system for my game. The items are represented by text objects. The inventory itself is a global array. The text modifies itself to match the elements of the array when I add the manually, but when I do it with the “Append string” method, it does not change the text or add the element to the array. Please help!
Thanks!

Hi PandaGaming, it’s unlikely I can help, but you increase your chances of someone else being able to help if you post a screenshot of your events.

ok
here’s my code:

and here’s what happens after pressing a:


see? nothing happens! it seems like it should work, but it doesn’t

update: it works now, but it appends it to inventory slot 10 instead of 9! the only way i can think of to fix this is for gdevelop to add an “insert string/number at index” method. unless there’s another way? please help!

Your screen shot doesn’t include the initial setting of the array values. So have you already filled it in the editor? How many entries in the array?


Why would it be at slot 11? From your code, I assume you have slots 0-9 filled? So the next one will be 10. Remember the array is 0 based. so the 11th element is array[10].


Also, as an aside, having a separate text object for each inventory item isn’t the best way. If you want to change something for that text, then you have to do it for each slot (font size, colour, font type etc)

I’d suggest the following :

  1. Create one text object, say called ‘txt_InventoryItem’. Give this object a variable named ‘id’ defaulting to -1.
  2. Create 10 instances of txt_InventoryItem, each with an id value 1 more than the last one created starting with 0 (so you should have 10 txt_InventoryItem objects with id going form 0 to 10)
  3. Populate all the txt_InventoryItem objects with this one event :

i’ll try that, thanks.
also the slot 10 was an error lol
i meant iit inserted at slot 10 instead of 9

ok! it’s sort of working. the only problem is i don’t know how to make the object variables unique to each instance. little help?
btw you’re a miracle worker thanks so much

oh wait, nvm, i know what to do!
thanks so much, but i can take it from here :slightly_smiling_face:
you were a huge help!

1 Like

ok! it’s working!
thanks again!