GML [SOLVED]How to remove a value from array?

L

Luc Vergnes

Guest
I'am struggeling with a problem involving arrays. My inventory system uses an array to store all the information about the items in the inventory. I know now that I should have used a ds_list, but it is to late now, because I have written my whole game code around the array. But now I want to put a value from the array al the way to the end (this already works) and then delete it from the array.

I have tried multiple things like:

Code:
array[20] = 0
array[20] = undefined
array[20] = noone
But nothing works...

Maybe you can help me out with this one. I usually don't post these kind of questions, I just do a quick internet seach or try some things out, but I can't find another thread or forum with a good solution that works for me and doesn't use ds_list.

Thanks in advance!
 

Xer0botXer0

Senpai
I don't understand.

You want to reset an array entry ?

If you reset an entry, reset it to the data type it was when you initialized it.

Array[20] = -1; //integer
Array[20] = ""; //string

Infact you can set array[20] = 373837483; and call that a reset. Obviously it's better to not store data in it but the point is a reset depends on what you do with the array.

Please explain further ?

Are you trying to move say Array[3]s value to the last array say Array[50] and then delete it ?
 
you can't remove an empty cell from an array. You could replace an empty value with a value from farther ahead in the array. But I think the simplest thing to do would be to modify your other code so that it will ignore empty values in the array, or to switch to using a list.
 

Bee

Member
I understand what you mean, but what I do is just set the value to -1 then when coding to go through it, I use an if statement in a for loop...
Code:
for (var i=0; i<array_length; i++) {
   if (array[i] != -1) {
      // do something
   }
}
 

jo-thijs

Member
I'am struggeling with a problem involving arrays. My inventory system uses an array to store all the information about the items in the inventory. I know now that I should have used a ds_list, but it is to late now, because I have written my whole game code around the array. But now I want to put a value from the array al the way to the end (this already works) and then delete it from the array.

I have tried multiple things like:

Code:
array[20] = 0
array[20] = undefined
array[20] = noone
But nothing works...

Maybe you can help me out with this one. I usually don't post these kind of questions, I just do a quick internet seach or try some things out, but I can't find another thread or forum with a good solution that works for me and doesn't use ds_list.

Thanks in advance!
Hi and welcome to the GMC!

It's not efficient or elegant, but you can use a script like this:
Code:
///scr_remove_last_element_from_array(array)

var result;

for(var i = array_length_1d(argument0) - 2; i >= 0; --i)
    result[i] = argument0[i];

return result;
and the use it like this:
Code:
array = scr_remove_last_element_from_array(array);
EDIT:
The most efficient approach depends on what you use your array for.
 
L

Luc Vergnes

Guest
Thank you all for your suggestions! I used your advice and now I move the variable all the way to the back of the array and then set its value to -1 like:
Code:
inv_sort() //moves the item to the back (this works fine)
array[inventoryitems-1] = -1
inventoryitems -= 1
Then I just don't use a value if it's -1. For example:
Code:
for(i = 0; i < inventoryitems; i++){
    if array[i] != -1{
        //do stuff
    }
}
 
L

Luc Vergnes

Guest
I understand what you mean, but what I do is just set the value to -1 then when coding to go through it, I use an if statement in a for loop...
Code:
for (var i=0; i<array_length; i++) {
   if (array[i] != -1) {
      // do something
   }
}
I think I'll use this. Thanks!
 
L

Luc Vergnes

Guest
The only thing is that this method will clutter up the array. But I think I'll just save the items in my .ini file with the filtering. And when I load the .ini file, only the items that didn't have a -1 value will be loaded.
 

Xer0botXer0

Senpai
Don't save items equal to -1 in the first place ?

Code:
Ini_open("test");
For (i=0;i<array_length_1d(array);i++)
{
If array[i] != -1//if information exists
{
Ini_read_real(section,key,array[i]);
}
}
Ini_close();
 
L

Luc Vergnes

Guest
Don't save items equal to -1 in the first place ?
That is want I ment to say, hehe. Sorry English is not my first language, and sometimes I don't really know how to express what I want to say. Anyway, thanks for your help and quick replies!
 
L

Lemth

Guest
I know this is already solved, but I could find the solution I like online, so I created my own.
Add this script, call it and supply it with the array and the pos to delete and catch the return:

GML:
/// @desc delete pos in array
/// @arg array
/// @arg pos
/// @ret array

var array;
for(var i=array_length_1d(argument0)-2;i>=0;i--) {
    if(i<argument1) {
        array[i]=argument0[i];
    } else {
        array[i]=argument0[i+1];
    }
}

return(array);
GML:
var array=array_1d_delete(array,[index_to_delete]);
 
Top