• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Removing one value from array and deleting the cleared spot

B

Bonemarrow

Guest
Quick question.
So I have an array called Items. And this array holds big amount of items. If I wanted to remove one value from it, and then cleaning the spot, how would I do it?
Example:
Items: 1,2,3,4,5,6, to Items: 1,2,3,5,6,
What I don't want: 1,2,3,0,5,6,

Hope you understood.
In advance-thank you.
 
It would be better to use a list to do this, then iterate through the list to find the item you want to remove, then remove it from the list.
The list will automatically shrink or grow depending on if you are removing or adding items.
ds_list documentation: http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds lists/

However, if you want to use a array of fixed size, what you will need to do is have a counter which keeps track of the last index in the array. This counter will also be used to determine the array size.
So, let's say when we initialize our array we do:
Code:
ItemSize = 6;
Items[ItemSize+1] = 0; //initializing indices 0-6 as zero
Then, using a script that would be called from whatever object initialized the array, we can add items by:
Code:
///scr_items_add(obj)
var _obj = argument0;

if(ItemSize >= array_length_1d(Items)){
  return false; //cannot add anything, array is full
}

//else add to end of array which we know is open
ItemSize += 1;
Items[ItemSize] = _obj;
return true;
And for removing we would have to remove from the index, then shift everything over, and decrement the array. I will update this shortly with how to do that.

Edit:
Here is the remove script (assuming we know the index we want to remove from) :
Code:
///scr_items_remove_index(index)
for(var i = argument0; i < ItemSize-1; i++){
  Items[i] = Items[i+1];
}
ItemSize -= 1;
If we need a script to find that index:
Code:
///scr_items_find_index(obj)
for(var i = 0; i < ItemSize; i++){
  if(Items[i] == argument0){
    return i;
  }
}
Now, with those two scripts we can make a script to remove an object:
Code:
///scr_items_remove_obj(obj)
scr_items_remove_index(scr_items_find_index(argument0));
 
Last edited:

ZeDuval

Member
This is not really possible, you'd need to always create the array anew and fill it with the values minus the deleted ones. Your case is a perfect example for the "array or ds_list?" question, where the answer is ds_list.

Will the data change, take a list is the data static, take an array.
 
D

DariusWolfe

Guest
There are a couple ways.

First, don't use an array. ds_lists handle stuff like this for you, and are faster than arrays. They do require more complex commands to access and modify, but they're a superior option to arrays in most cases; If you delete a node in the list, it automatically fills the gap for you.

Secondly, if you must use an array, you'll need to do the clean up yourself.

Basically, if you delete position 3 in an array, you'll need to assign the value in position 4 to position 3, the value in position 5 to position 4, and so on; Don't forget to reduce whatever variable you're using to keep track of the length of the array.
 
I

izanbf1803

Guest
Pseudocode:

for (var i = 1; i < length(array); i++){
if array[ i ] != elementToRemove {
append(newArray, array[ i ]);
}
}
 
Last edited by a moderator:
B

Bonemarrow

Guest
Thank all of you for your replies.
Online handle, I used your method but had to modify it a bit, howewer now it works! Thanks! Final code:

///scr_items_find_index(obj)
for(var i = 0; i < inventoryItemAmount; i++){
if(inventoryItemListTemp == argument0){
return i;
}
}

///scr_items_remove_index(index)
for(var i = 0; i < argument0; i++){
inventoryItemListTempFinal = inventoryItemListTemp;
}
for(var i = argument0+1; i < inventoryItemAmount; i++){
inventoryItemListTempFinal[i-1] = inventoryItemListTemp;
}
inventoryItemAmount -= 1;
 
Top