How to remove an item at index from an array in Verse?

Sorry for the noob question, but I can’t wrap my head around the way Verse works, I can’t even do the simplest things.

I want to remove an item from an array of item spawners at index 0. I tried this:
set ItemSpawners = array{ItemSpawners[1]..ItemSpawners[Length-1]}

I also tried it with a temporary array, copying values one by one in a for loop.
I couldn’t get it working. As far as I know there’s no remove or delete functions.

3 Likes

Managed to get it work like this:

var TempArray : []item_spawner_device = array{}
for(i := 1..ItemSpawners.Length-1):
    if(e := ItemSpawners[i]):
        set TempArray = TempArray + array{e}
set ItemSpawners = TempArray

You can remove an item from an array using (InputArray:[]t).RemoveElementAt(IndexToRemove:int):[]t. This returns a new copy of the array with the element removed.

Here is an example usage:

var MyDeviceArray:[]creative_device = array{}

if (NewArray := MyDeviceArray.RemoveElement[0]):
    set MyDeviceArray = NewArray
6 Likes

Thank you. Somehow this escaped my attention.

1 Like

The functions are implemented as extension methods on []t which I’ve found aren’t discoverable as part of our API documentation. I’ve flagged this to the documentation team so we can make improvements here in the future.

4 Likes

Well, It’s not accesible now. Is there any other link to check the extension methods?

they are in the Verse module:

seems like the list of functions on the page is actually incomplete, but the list in the sidebar is more complete

This is super helpful and I was having a hard time finding these thank you!

Link loser…