[Solved] How do I delete a text that was added to an array?

Hello. Texts are added to an array when items are collected in my prototype inventory:

I want to be able to delete them by this text, not by their index. This is because a player will collect items in an arbitrary order. Zero index could be a book, banana, crowbar - anything. I need to be able to target “banana”, for example, specifically, and remove it from the array. Is that possible? I can’t find an action for it and could only find ‘remove variable at index’.

I am also a little fuzzy on what happens when a text is added to an array. Is it like the below, where texts are added to form a list, all being values of the array? Or perhaps these texts count as new children of the array? (they seem to be counted as children when I iterate over them in another event, but the ‘Remove child’ action doesn’t work to remove them).

array = [“banana”, “book”, “cola”, “dino”, “crowbar”]

Many thanks.

You can use something like this:

There is also an array tools extension.

This goes through the list array with the variable i When you remove an element, everything below it moves up a spot. So, when you delete something then you need to check the same index the next time, so you need to subtract 1 from i so it’s the same index after the next event adds one.

If you open the debugger, you can see how the variable is stored.
image

arrays are automatically numbered while structures are not. Remove child is for structures. They’re very similar except arrays use numbers and are ordered while structures use text and aren’t numbered.

image

.
https://wiki.gdevelop.io/gdevelop5/interface/debugger/#game-debugger-and-profiler

2 Likes

Very helpful again Keith. Thank you. This week you and MrMen have helped me fill key knowledge gaps that were preventing me doing the things I wanted to do.

1 Like