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

Problem with arrays and ds list

F

Federico

Guest
Hi Guys!
I am working on a software that can take some word, transform it into code, and can organize the word in similar code. For example:
  • I have the word "hello". The programm transform the word into a special code.
  • We pretend the code is "61001101".
  • The software have a main ds_list called "elenco", where save all the code (61001101) and assign a value at the variable "numero" according to its position in the ds_list.
Example. The first word is "hello" and its code is "61001101". So che code was added in the ds_list
("elenco") and attributes at the variable "numero" the value 0 (which is the position in the list).
Later I add a new word "dog", which code is "8101101". The software add the code in the list and assign the value 1 at the variable "numero" (position in the ds_list "elenco").


Up to here is all okay.

Now, I want that for each code added to the ds_list ("elenco"), was created another ds_list which contain the original word. For example:
  • Word "hello".
  • Transform to code "61001101".
  • Add the code to the ds_list "elenco":
  • assign a value at the variable "numero" based on the position of the code in the ds_list "elenco".
  • create a ds_list called my_name for each code in the list "elenco".
  • save the original word ("hello") in the ds_list my_name.
  • I use an array with the ds_list my_name, so it is called my_name[numero]
All work if I use only one ds_list (my_name[numero]. If I create another list, and add a now word in it, the word was added in all the ds_list "my_name[numero]" exsisting. It is at if the array ("numero") in the ds_list (my_name) didn't work. Someone can help me? I leave you the code...
SINCODE IS THE CODE (for example "61001101).

Code:
///create_list
if (create_list = false) exit;  
 
//covert sincode to ordinal number  
var posizione = ds_list_find_index(elenco, sincode);
if (posizione = -1)
{
    ds_list_add(elenco, sincode);
    numero = real(ds_list_find_index(elenco, sincode));
}
else
{
    numero = real(ds_list_find_index(elenco, sincode));
}

//----------------------//
my_name[numero] = true;
var word = string(part);
//---------------------//


if !(ds_exists(my_name[numero], ds_type_list))
{
    my_name[numero] = ds_list_create();
    ds_list_clear(my_name[numero]);
    ds_list_add(my_name[numero], word);
    test = true;
}
else if (ds_exists(my_name[numero], ds_type_list))
{
    var pos = ds_list_find_index(my_name[numero], word);
    if (pos = -1)
    {
        ds_list_add(my_name[numero], word);
    }
    else if (pos != -1)
    {
        already_exists = true;
    }
}
   
var posi = ds_list_find_index(my_name[numero], word);  
if (posi = -1)
{
    status = "error";  
}
else
{
    status = "complete";
}

complete = true;
create_list = false;
word = "";
 
F

Federico

Guest
looks to me you should be using a ds_map
Yes, that is a good idea. I haven't know ds_map since this morning. Now I check in the wiki and I think is good for what I need. Now I try and let you know.
 
F

Federico

Guest
looks to me you should be using a ds_map
There is a problem... The wiki said that "There is also no way to hold two keys that are the same, nor can you assign one key two values."
In my case the Keys of the map are the SINCODE variables (for example 61001101), and the values are the word (for Example "hello").
But for every SINCODE I have more than 1 word. Example in the code 61001101 I have the word "hello" and the word "dog", and some other.
 
F

Federico

Guest
looks to me you should be using a ds_map
Can I create a in a ds_map?
Code:
elenco = ds_map_create(); //create the map
ds_list_sincode = ds_list_create(); //create the list (value)
ds_map_add(elenco, 0, ds_list_code); //add list in the map
ds_list_add(ds_list_sincode, word); //add word in the list
But I think I'll have the same problem: I have to index the ds_list with an array. and it doesn't work now...
 
I

icuurd12b42

Guest
how about simply doing
ds_map_add(elenco,"61001101", "hello");
ds_map_add(elenco,"61001102", "dog");

not sure why you need a list in this whole concept
 
F

Federico

Guest
how about simply doing
ds_map_add(elenco,"61001101", "hello");
ds_map_add(elenco,"61001102", "dog");

not sure why you need a list in this whole concept
Becouse for the key "61001101" I have more than 1 word. Example: "hello" and "dog" have the same code "61001101". For every key (sincode) I need more then 1 value...
 
I

icuurd12b42

Guest
you will need to step back and explain what your concept is in one sentence, and my that I dont mean "I want to have one item hold many names..."

what's the use, what factors determined you need something like that...
 
G

GeminiEngine

Guest
How about a map of lists?
Code:
var list = ds_list_create();
ds_list_add(list,"hello","dog");
ds_map_add(elenco,"61001101", list);
Treat every value in the map as a list, even if that list has only one entry. Each item then has a "x" and a y component, using your example, "hello" would have coordinates ("61001101",0).
 
I

icuurd12b42

Guest
ds_map_add_list is the function you need when you add list to a map, it give ownership to the container and allows saving the whole structure
 

GMWolf

aka fel666
Far better yet: a map of arrays:
Code:
ds_map_add(elenco, 61001101, ["hello", "dog"]);
But this all seems quite convoluted. What is the desired result?
 
I

icuurd12b42

Guest
^ I don't do that anymore since there is little speed advantage and there's conflict to saving it (to json) and then reading it back in... the arrays morph a ds_lists... so might as well use ds_lists from the get go

Oh, gms2? not sure if this issue is resolved
 

GMWolf

aka fel666
^ I don't do that anymore since there is little speed advantage and there's conflict to saving it (to json) and then reading it back in... the arrays morph a ds_lists... so might as well use ds_lists from the get go

Oh, gms2? not sure if this issue is resolved
I do it because of garbage collection, and no ambiguities between tyoes. (Arrays are array, lists are indexed)
 
I

icuurd12b42

Guest
I do it because of garbage collection, and no ambiguities between tyoes. (Arrays are array, lists are indexed)
If you add_list to the map, destroying the map destroys the list... though replacing a list already in the map you would need to get the id from the map first and destroy it yourself before adding a new list (aka data)...

The array seems to alleviate this for sure. but there was issues with the garbage collector not freeing arrays properly unless you set the variable holding it to 0 yourself... and so I'm a little wary of it working properly with arrays in data structures...
 

GMWolf

aka fel666
If you add_list to the map, destroying the map destroys the list... though replacing a list already in the map you would need to get the id from the map first and destroy it yourself before adding a new list (aka data)...

The array seems to alleviate this for sure. but there was issues with the garbage collector not freeing arrays properly unless you set the variable holding it to 0 yourself... and so I'm a little wary of it working properly with arrays in data structures...
I think this problem was fixed.
What hasn't been fixed are cyclical references but I have been told this was in the works.

The problem with ds_map_add_lost, is that despite numerous bug reports, we still don't have "ds_map_is_list".
This means there is no way to differentiate your lists and numbers.

Not a problem if you already know you only have lists, but not great in other situations.
 
I

icuurd12b42

Guest
Yeah, I've been begging for the is_list/is_map for 2 years
 
Top