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

[Solved] Issue with looping through a list; must be something simple?

W

whale_cancer

Guest
As part of my game's data structure I have a global list (global.markers) that holds a series of ds_maps saved as strings.

In my map editor, when I create a new one of these markers it gets added to the global list successfully (I am able to load and save the global.markers_list in an external file and they generate correctly on the map when I do).

When I create a new marker in my map editor (which is an object that holds the same data as the associated ds_map) I want to loop through the ds_list and make sure that I am not placing one with the same name as one that already exists (as this would cause problems with how I use the markers in the game itself).

Here is the relevant code for creating the marker objects:

Code:
with (instance_create(global.cell_hover_x, global.cell_hover_y, obj_marker))
        {
            temp_map = ds_map_create();
            //what chunk are we in?
            ds_map_add(temp_map, 'chunk', global.map_name);
            //what are the w,h coordinates of the marker (relative to the chunk)?
            ds_map_add(temp_map, 'w', global.cell_hover_w);
            ds_map_add(temp_map, 'h', global.cell_hover_h);
            //what is the name of the chunk?
            name = get_string('marker name?', 'marker_'+string(ds_list_size(global.markers)));
            ds_map_add(temp_map, 'name', name);
            show_debug_message('---');
            //check to see if a marker with this name already exists
            for (i = 0; i < ds_list_size(global.markers); i += 1)
            {
                //convert the ds_map string at list location i back into a ds_map
                temp_string = ds_list_find_value(global.markers, i);
                temp_map = ds_map_create();
                ds_map_read(temp_map, temp_string);
                //find the thing with the same name yo
                temp_name = ds_map_find_value(temp_map, 'name') 
                show_debug_message(string(temp_name)+' compared to '+string(name))
                if (temp_name == name)
                {
                    show_debug_message('Found marker with that name already')
                    ds_map_destroy(temp_map);
                    instance_destroy()
                    exit;
                }         
              
            }
          
            temp_string = ds_map_write(temp_map);
            ds_list_add(global.markers, temp_string);
            ds_map_destroy(temp_map);
        }
Here is my debug output.

Code:
---
default_start compared to marker_1
---
default_start compared to marker_2
default_start compared to marker_2
---
default_start compared to marker_3
default_start compared to marker_3
default_start compared to marker_3
---
default_start compared to default_start
Found marker with that name already
As you can see, rather than comparing the new name to each existing name in the list, it instead compares it to the first name in the list for as many times as there are markers. If I use the same name as the first marker in the list, it behaves as it should.

Must be some elementary mistake I am not seeing?
 
J

Jonathan E. Styles

Guest
@whale_cancer , could you provide an output of the ds_list for global.markers or upload the project to look at?

At first glance I want to say something is altering your i value in the for loop, your loop is broken somewhere, or the list size method is off somehow? I was hoping I could just cut and paste the code into GMS and do some troubleshooting, but it needs some of these variables loaded first to try. If I don't hear back from you - no worries. I will keep hammering away at it until I can determine something, at least!
 
W

whale_cancer

Guest
@whale_cancer , could you provide an output of the ds_list for global.markers or upload the project to look at?

At first glance I want to say something is altering your i value in the for loop, your loop is broken somewhere, or the list size method is off somehow? I was hoping I could just cut and paste the code into GMS and do some troubleshooting, but it needs some of these variables loaded first to try. If I don't hear back from you - no worries. I will keep hammering away at it until I can determine something, at least!
Whoah this is weird. The saving and loading of the markers has broken at some point, as it was working before, but now it is not. Very strange. I guess the problem now is why the list entries are not being added to the list correctly. Here is a quick video of me placing the markers. The top right side shows a list of markers generated with this code:

EDIT: The pause in the video after the mouse moves away from the white box is me hitting enter on the popup window asking for the name of the marker. The names entered are as you would expect, marker_0 to marker_3.

Code:
for (i = 0; i < ds_list_size(global.markers); i += 1)
            {
                //convert the ds_map string at list location i back into a ds_map
                temp_string = ds_list_find_value(global.markers, i);
                temp_map = ds_map_create();
                ds_map_read(temp_map, temp_string);
                //find the thing with the same name yo
                temp_name = ds_map_find_value(temp_map, 'name') 
              
                draw_set_halign(fa_right)
                draw_text(window_get_width() - 8, 32 + i * 8, temp_name )
                ds_map_destroy(temp_map);
            }
I can show the saving and loading code if needed, but the issue seems to be the actual adding of the ds_maps to the ds_list (which is the code in the original post).
 
J

Jonathan E. Styles

Guest
Whoah this is weird. The saving and loading of the markers has broken at some point, as it was working before, but now it is not. Very strange. I guess the problem now is why the list entries are not being added to the list correctly. Here is a quick video of me placing the markers. The top right side shows a list of markers generated with this code:

EDIT: The pause in the video after the mouse moves away from the white box is me hitting enter on the popup window asking for the name of the marker. The names entered are as you would expect, marker_0 to marker_3.

Code:
for (i = 0; i < ds_list_size(global.markers); i += 1)
            {
                //convert the ds_map string at list location i back into a ds_map
                temp_string = ds_list_find_value(global.markers, i);
                temp_map = ds_map_create();
                ds_map_read(temp_map, temp_string);
                //find the thing with the same name yo
                temp_name = ds_map_find_value(temp_map, 'name')
             
                draw_set_halign(fa_right)
                draw_text(window_get_width() - 8, 32 + i * 8, temp_name )
                ds_map_destroy(temp_map);
            }
I can show the saving and loading code if needed, but the issue seems to be the actual adding of the ds_maps to the ds_list (which is the code in the original post).
Forgive me if I am wrong, but I can't test the code on my end because it won't load a list of maps for me, I just get 'undefined' when I test (tried to push my own values). However, if I am parsing the code correctly you are creating a new "temp_map" in your for loop. After the loop completes you are then writing to the maps with "temp_map", but I believe you are wanting to write what was set before the for loop. If this is the case I would change the variable name in the for loop to something clever like, "temp_map2". ;)

See if this is heading the right direction. Until then I will keep trying to isolate something.
 
W

whale_cancer

Guest
Forgive me if I am wrong, but I can't test the code on my end because it won't load a list of maps for me, I just get 'undefined' when I test (tried to push my own values). However, if I am parsing the code correctly you are creating a new "temp_map" in your for loop. After the loop completes you are then writing to the maps with "temp_map", but I believe you are wanting to write what was set before the for loop. If this is the case I would change the variable name in the for loop to something clever like, "temp_map2". ;)

See if this is heading the right direction. Until then I will keep trying to isolate something.
Yup, this was EXACTLY it. I was just coming here to report the same thing. It was working before because the second use of temp_map wasn't overwriting the first, but when I added this check for a duplicate named marker entry, it was overwriting the first!

AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

Anywho, thanks for the assistance!
 
J

Jonathan E. Styles

Guest
My pleasure. Glad to be an extra pair of eyes. :) Happy coding!
 
Top