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);
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.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?
Sorry for the delay. GameMaker Studio 1.4.9 does not recognize some things from the code you shared. I will try other things and wait for other answers. ThanksI edited my post with an example.
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: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);
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: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
it seems a backwards to me?
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.
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 stylesBasically 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.
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 helpI'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);
//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);
}
// 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);
}
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.When I ran the code returned this error: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
if (my_list[| j] == value) {