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

GameMaker Need help with ds_list

A

Audun Paudun

Guest
Hello!
I'm making a grid based game kind of like tetris only without the gravity. The player places tiles onto the grid and gets points when rows/columns are full of tiles. When a row(s) is full the game populates a list with the ID's , coordinates and colors of the tiles in said row(s). The game then calculates the score based on how many adjacent tiles there are of the same color.
Here is the start of the code:
Code:
if (!ds_list_empty(global.list_remaining)){
  
    remaining_size = ds_list_size(global.list_remaining);
  
    for(g=0; g<remaining_size; g++){
      
        if(ds_list_empty(global.list_checked)){
            ds_list_add(global.list_checked, ds_list_find_value(global.list_remaining, 0));
            ds_list_add(global.list_colors_checked,string_char_at(string(ds_list_find_value(global.list_remaining,0)), 1));
            ds_list_add(global.list_coords_checked,string_char_at(string(ds_list_find_value(global.list_remaining,0)), 2) + string_char_at(string(ds_list_find_value(global.list_remaining,0)), 3));
            ds_list_delete(global.list_remaining,0);
        }
  
        current_coord = string_char_at(string(ds_list_find_value(global.list_remaining, g)),2) + string_char_at(string(ds_list_find_value(global.list_remaining, g)),3);
        check_color = string_char_at(string(ds_list_find_value(global.list_remaining, g)), 1);
        check_over = string_char_at(current_coord,1) + string(real(string_char_at(current_coord,2))-1);
The entries in the list look like this; 302100004, where the first number is the color of the tile, the next two are the coordinates and the rest is the tile's ID.

The first if-statement checks if list_checked is empty, and if so, adds to it the first entry in list_remaining. It them deletes the entry in list_remaining. This is where it all goes wrong. the varible current_coord is set to the coordinates of entry number g. Here it seems that var current_coord is set to undefined for some reason. What could be the reason for this? It is my understanding that when I delete index 0 from a ds_list, the rest of the entries move up one space so to speak?
Does anyone know why var current_coord is set to undefined?

EDIT: The entries in list_remaining are integers not strings if that info helps.
 
Last edited by a moderator:

chamaeleon

Member
Hello!
I'm making a grid based game kind of like tetris only without the gravity. The player places tiles onto the grid and gets points when rows/columns are full of tiles. When a row(s) is full the game populates a list with the ID's , coordinates and colors of the tiles in said row(s). The game then calculates the score based on how many adjacent tiles there are of the same color.
Here is the start of the code:
Code:
if (!ds_list_empty(global.list_remaining)){
 
    remaining_size = ds_list_size(global.list_remaining);
 
    for(g=0; g<remaining_size; g++){
     
        if(ds_list_empty(global.list_checked)){
            ds_list_add(global.list_checked, ds_list_find_value(global.list_remaining, 0));
            ds_list_add(global.list_colors_checked,string_char_at(string(ds_list_find_value(global.list_remaining,0)), 1));
            ds_list_add(global.list_coords_checked,string_char_at(string(ds_list_find_value(global.list_remaining,0)), 2) + string_char_at(string(ds_list_find_value(global.list_remaining,0)), 3));
            ds_list_delete(global.list_remaining,0);
        }
 
        current_coord = string_char_at(string(ds_list_find_value(global.list_remaining, g)),2) + string_char_at(string(ds_list_find_value(global.list_remaining, g)),3);
        check_color = string_char_at(string(ds_list_find_value(global.list_remaining, g)), 1);
        check_over = string_char_at(current_coord,1) + string(real(string_char_at(current_coord,2))-1);
The entries in the list look like this; 302100004, where the first number is the color of the tile, the next two are the coordinates and the rest is the tile's ID.

The first if-statement checks if list_checked is empty, and if so, adds to it the first entry in list_remaining. It them deletes the entry in list_remaining. This is where it all goes wrong. the varible current_coord is set to the coordinates of entry number g. Here it seems that var current_coord is set to undefined for some reason. What could be the reason for this? It is my understanding that when I delete index 0 from a ds_list, the rest of the entries move up one space so to speak?
Does anyone know why var current_coord is set to undefined?

EDIT: The entries in list_remaining are integers not strings if that info helps.
First glance seems to tell me that g will go one step beyond the size of the list if you remove one element, since you set the end condition value before the for loop but don't modify it when you remove one in the if statement. When your final statements after the if statement then tries to look up the last(?) g entry in the list you could be getting garbage data back. Without additional debug information using perhaps some show_debug_message() between lines with pertinent information about the list, g, output values, etc., that's my best guess.
 

TheouAegis

Member
It's just as chamaeleon said.

When the for loop ends, it ends on the breakpoint. So

for(var i=0; i<10; i++)

After that loop, i will be 10, not 9. if you have a list with 10 entries in it, the last entry will be entry number nine, not entry number 10. If you try to read entry number 10, you will get an undefined value.
 
A

Audun Paudun

Guest
Turns out that was exactly what was happening. I left out most of the algorith thinking it didn't matter since I thought the problem was that index 0 in the list was set to undefined or something like that. I did update the variable storing the size of the list later in the code, but it didn't work for some reason.
 
Top