GMS 2 "Trying to draw non-existing sprite" error while using variable

E.M.I

Member
Hey GameMaker Community! I've been working on a top-down game and have run into an issue with the depth system. I have two scripts that seem to be causing this error: scr_create_draworder, which creates the draw_order and draworder_size variables I'm using to store the grid size, as well as the i variable, used to adjust the size of the depth grid according to the values of the draworder_size and draworder_size variables. Here's the code:

Code:
/// scr_draw_draworder();
   
    // draw everything in the draworder

    for(var index = 0; index < global.draworder_size; index++){
       
        // search list for appropiate Y!
        var l = global.draworder[# 0, index];
       
        var count = ds_list_size(l)-1;
       
        //check, if instances in list
        if (count >= 0){
       
            while(count >= 0){
                // target instance
                var i = ds_list_find_value(l, count--);
                if (i.subtype = "z-axis"){
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y-i.z, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);                      
                } else {
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);  
                }
            }
        }
    }
Then you have the scr_draw_draworder script, which actually draws the objects. Here's that code:

Code:
/// scr_add_draworder

var cam = view_get_camera(0);    
   
if (argument0.y > camera_get_view_y(cam)+600) return false;
if (argument0.y < camera_get_view_y(cam)-250) return false;
if (argument0.x > camera_get_view_x(cam)+600) return false;
if (argument0.x < camera_get_view_x(cam)-250) return false;
       
// add offset to instance.y
   var yy = round(argument0.y)+DO_OFFSET;
// if out of bounds, don't add
   if (yy < 0 or yy >= global.draworder_size) return false;
       
// search list, which is needed
   var list = global.draworder[# 0, yy];
   
// add to this list
   ds_list_add(list, argument0);
   return true;
The problem is, all the uses of the i variable in scr_draw_draworder return the "Trying to draw non-existing sprite" error (specifically the uses in line 21). Anyone have any idea of what's going on? Thanks in advance!
 
Last edited:

immortalx

Member
If I'm reading this right, your count variable equals zero even when the number of elements in the list is 1.
If that's the case, then in the var i = ds_list_find_value function you're trying to get the value at index -1.
That means you should set the count variable to be equal to the list size (and not list size-1) and your while loop condition to count>0 (not count>=0)
 
T

Timothy

Guest
Hey GameMaker Community! I've been working on a top-down game and have run into an issue with the depth system. I have two scripts that seem to be causing this error: scr_create_draworder, which creates the draw_order and draworder_size variables I'm using to store the grid size, as well as the i variable, used to adjust the size of the depth grid according to the values of the draworder_size and draworder_size variables. Here's the code:

Code:
/// scr_draw_draworder();
   
    // draw everything in the draworder

    for(var index = 0; index < global.draworder_size; index++){
       
        // search list for appropiate Y!
        var l = global.draworder[# 0, index];
       
        var count = ds_list_size(l)-1;
       
        //check, if instances in list
        if (count >= 0){
       
            while(count >= 0){
                // target instance
                var i = ds_list_find_value(l, count--);
                if (i.subtype = "z-axis"){
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y-i.z, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);                      
                } else {
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);  
                }
            }
        }
    }
Then you have the scr_draw_draworder script, which actually draws the objects. Here's that code:

Code:
/// scr_draw_draworder();
  
    // draw everything in the draworder

    for(var index = 0; index < global.draworder_size; index++){
      
        // search list for appropiate Y!
        var l = global.draworder[# 0, index];
      
        var count = ds_list_size(l)-1;
      
        //check, if instances in list
        if (count >= 0){
      
            while(count >= 0){
                // target instance
                var i = ds_list_find_value(l, count--);
                if (i.subtype = "z-axis"){
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y-i.z, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);                       
                } else {
                    draw_sprite_ext(i.sprite_index, i.image_index, i.x, i.y, i.image_xscale, i.image_yscale, i.image_angle, i.image_blend, i.image_alpha);   
                }
            }
        }
    }
The problem is, all the uses of the i variable in scr_draw_draworder return the "Trying to draw non-existing sprite" error (specifically the uses in line 21). Anyone have any idea of what's going on? Thanks in advance!
You posted the same script twice i think.

But anyway, are you sure that all your instance in the list have a valid sprite index? Also, a with() statement would be better here. (also the if (count >= 0) is redundant).
 

E.M.I

Member
If I'm reading this right, your count variable equals zero even when the number of elements in the list is 1.
If that's the case, then in the var i = ds_list_find_value function you're trying to get the value at index -1.
That means you should set the count variable to be equal to the list size (and not list size-1) and your while loop condition to count>0 (not count>=0)
Thanks, I'll try it out!
 

E.M.I

Member
You posted the same script twice i think.

But anyway, are you sure that all your instance in the list have a valid sprite index? Also, a with() statement would be better here. (also the if (count >= 0) is redundant).
Oh crap, sorry about that. Must've slipped me by. I've edited the original post to correct that.
 
T

Timothy

Guest
I'm not sure what your passing into the second script... but I would add argument0.id to the list rather than just argument0.
 
Top