• 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!

Legacy GM Save Array to DS list

itsmecsf

Member
Is it possible to save matrix values in ds list? I would like to save the array values to the list, but would like the value to be added if it does not exist, and if it does, it will not be added to the list. I would like an example of how this can be done. Thanks.
 
Last edited:

FoxyOfJungle

Kazan Games
Why do you want to add the values of an array to a ds_list if you can just use arrays that work like lists?

However...
GML:
list = ds_list_create();
ds_list_add(list, 128);
ds_list_add(list, 500);
ds_list_add(list, "AAAA");
ds_list_add(list, true);

array = ["AAAA"];

//show_debug_message(array);

for (var i = 0; i < ds_list_size(list); ++i) {
    var can_add = true;
    for (var j = 0; j < array_length(array); ++j) {
        if (array[j] == list[| i]) can_add = false;
    }
    if (can_add) array_push(array, list[| i]);
}

show_debug_message(array);
 
Last edited:

itsmecsf

Member
Why do you want to add the values of an array to a ds_list if you can just use arrays that work like lists?
Because I find it a little easier to work with a list. I'm new to the world of programming and GameMaker. My idea is to do a small inventory using ds list, but I can't change the amount of items, I thought that storing an array would be easier to change the amount.
 

FoxyOfJungle

Kazan Games
I'm not very familiar with GMS 1.4 anymore, but I think you just need to change that:

GML:
list = ds_list_create();
ds_list_add(list, 128);
ds_list_add(list, 500);
ds_list_add(list, "AAAA");
ds_list_add(list, true);

array[0] = "AAAA";

//show_debug_message(array);

for (var i = 0; i < ds_list_size(list); ++i) {
    var can_add = true;
    for (var j = 0; j < array_length_1d(array); ++j) {
        if (array[j] == list[| i]) can_add = false;
    }
    if (can_add) array[array_length_1d(array)] = list[| i];
}

show_debug_message(array);
 
I'm not very familiar with GMS 1.4 anymore, but I think you just need to change that:

GML:
list = ds_list_create();
ds_list_add(list, 128);
ds_list_add(list, 500);
ds_list_add(list, "AAAA");
ds_list_add(list, true);

array[0] = "AAAA";

//show_debug_message(array);

for (var i = 0; i < ds_list_size(list); ++i) {
    var can_add = true;
    for (var j = 0; j < array_length_1d(array); ++j) {
        if (array[j] == list[| i]) can_add = false;
    }
    if (can_add) array[array_length_1d(array)] = list[| i];
}

show_debug_message(array);
I'm a little unsure what you're doing here and it seems a backwards to me? Hahaha, I'm not too sure, I'd have to test it to see what it's doing. I would do it this way personally:
GML:
var my_array;
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 2;
my_array[3] = 4;

var my_list = ds_list_create();

var array_size = array_length_1d(my_array); // Declaring the array size outside of the for loop is always a good micro-optimisation, otherwise the size of the array is retrieved every single cycle of the loop, rather than only once

for (var i=0;i<array_size;i++) {
   var value = my_array[i];
   var list_size = ds_list_size(my_list);
   var exists = false;
   for (var j=0;j<list_size;j++) { // Loop through the list and compare the current array value to the values in the list, if it is found, we set exists to true and break out of the loop
      if (my_list[j] == value) {
         exists = true;
         break;
      }
   }
   if (!exists) { // If no value has been found in the above loop, then exists will remain false, so we know we can push the array value into the list
      ds_list_add(my_list,my_array[i]);
   }
}
// my_list = 1, 2, 4 now
 
Last edited:

FoxyOfJungle

Kazan Games
I'm a little unsure what you're doing here and it seems a backwards to me? Hahaha, I'm not too sure, I'd have to test it to see what it's doing. I would do it this way personally:
Code:
var my_array;
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 2;
my_array[3] = 4;

var my_list = ds_list_create();

var array_size = array_length_1d(my_array); // Declaring the array size outside of the for loop is always a good micro-optimisation, otherwise the size of the array is retrieved every single cycle of the loop, rather than only once

for (var i=0;i<array_size;i++) {
   var value = my_array[i];
   var list_size = ds_list_size(my_list);
   var exists = false;
   for (var j=0;j<list_size;j++) { // Loop through the list and compare the current array value to the values in the list, if it is found, we set exists to true and break out of the loop
      if (my_list[j] == value) {
         exists = true;
         break;
      }
   }
   if (!exists) { // If no value has been found in the above loop, then exists will remain false, so we know we can push the array value into the list
      ds_list_add(my_list,my_array[i]);
   }
}
// my_list = 1, 2, 4 now
Basically the OP wants to add the items from a array to an ds_list, with the proviso that the existing items are not overwritten, so the code I wrote does just that:

Just adding array values to the list:



Do not add if it already exists:



As the OP uses GMS 1.4, I just converted the code to work on it (I actually tested it before submitting the code).
Your code apparently does exactly what mine does, however.

it seems a backwards to me?

Yes, he said himself that he prefers to use lists.
Because I find it a little easier to work with a list. I'm new to the world of programming and GameMaker. My idea is to do a small inventory using ds list, but I can't change the amount of items, I thought that storing an array would be easier to change the amount.
 
Basically the OP wants to add the items from a array to an ds_list, with the proviso that the existing items are not overwritten, so the code I wrote does just that:

Just adding array values to the list:



Do not add if it already exists:



As the OP uses GMS 1.4, I just converted the code to work on it (I actually tested it before submitting the code).
Your code apparently does exactly what mine does, however.




Yes, he said himself that he prefers to use lists.
Ah yeah, I didn't have time to test your code (I felt like it should work, but I wasn't sure). I guess I just got confused because of different coding styles 🤪
 

itsmecsf

Member
I'm not very familiar with GMS 1.4 anymore, but I think you just need to change that:

GML:
list = ds_list_create();
ds_list_add(list, 128);
ds_list_add(list, 500);
ds_list_add(list, "AAAA");
ds_list_add(list, true);

array[0] = "AAAA";

//show_debug_message(array);

for (var i = 0; i < ds_list_size(list); ++i) {
    var can_add = true;
    for (var j = 0; j < array_length_1d(array); ++j) {
        if (array[j] == list[| i]) can_add = false;
    }
    if (can_add) array[array_length_1d(array)] = list[| i];
}

show_debug_message(array);
Before I test your code, I'd like you to see mine, if you can test it and tell me what I'm doing wrong it will be a great help

GML:
//key press - space
var check = "AAA";
value[0] = check;
value[1] = 1;

var find = ds_list_find_index(inv, value);

if find == -1 {
    ds_list_add(inv, value);
}
At first the code works, but if you have the same code in another event, the value I asked to save in the ds list is replaced by the value of the other event

Code:
// key press - enter
var check = "BBB";
value[0] = check;
value[1] = 1;

var find = ds_list_find_index(inv, value);

if find == -1 {
    ds_list_add(inv, value);
}
Now that I've shown my code I'll see yours and the other person who also answered
 
That code doesn't work ever. It might seem like it works, but that's a complete coincidence. You're storing an array in the variable value. value itself holds a reference to that array (which will be some random number). You are then running: var find = ds_list_find_index(inv, value); which is looking for the reference number of the array which is stored in value (again, a random number) inside of the ds_list. It is not looping through the array checking to see if any of the array slots holds a value that is equal to a value in the ds_list.

There is a reason why both mine and @FoxyOfJungle's code loops through both the ds_list and the array. You need to check each value of one against all the values in the other, using a double loop. Foxy's loops through the list and checks each list value against all the values in the array, whereas mine loops through the array and checks each array value against all the values in the list. You need to do one of these two things in order to only add something if it doesn't already exist.
 

itsmecsf

Member
I'm a little unsure what you're doing here and it seems a backwards to me? Hahaha, I'm not too sure, I'd have to test it to see what it's doing. I would do it this way personally:
GML:
var my_array;
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 2;
my_array[3] = 4;

var my_list = ds_list_create();

var array_size = array_length_1d(my_array); // Declaring the array size outside of the for loop is always a good micro-optimisation, otherwise the size of the array is retrieved every single cycle of the loop, rather than only once

for (var i=0;i<array_size;i++) {
   var value = my_array[i];
   var list_size = ds_list_size(my_list);
   var exists = false;
   for (var j=0;j<list_size;j++) { // Loop through the list and compare the current array value to the values in the list, if it is found, we set exists to true and break out of the loop
      if (my_list[j] == value) {
         exists = true;
         break;
      }
   }
   if (!exists) { // If no value has been found in the above loop, then exists will remain false, so we know we can push the array value into the list
      ds_list_add(my_list,my_array[i]);
   }
}
// my_list = 1, 2, 4 now
When I ran the code returned this error:

trying to index a variable which is not an array
at gml_Object_object0_CreateEvent_1 (line 16) - if (my_list[j] == value) {
 
Ah, sorry, I forgot the accessor for the ds_list (haven't used lists for so long now, lol), change that line to this:
GML:
     if (my_list[| j] == value) {
The pipe character | is an accessor for the ds_list, it tells GMS that what you are trying to access is a position in a list instead of an array.
 
Top