Search code examples
gml

Gamemaker - draw_sprite not functioning as it should with value in data structure


My code to draw the sprite based on the position of my variable in my for loop is as follows:

for (var i = 0; i <= ds_list_size(Player.buffs) i += 1) {
cuBuff = ds_list_find_value(Player.buffs, i)
if cuBuff == sprite8
draw_sprite (cuBuff, 1, x -148, y -233,)

It's working as it should up until I remove the if cuBuff == sprite8

When I do it returns the error:

#

FATAL ERROR in action number 1 of Draw Event for object obj_UIPlayer:

draw_sprite argument 1 incorrect type (5) expecting a Number (YYGI32) at gml_Object_obj_UIPlayer_DrawEvent_1 (line 16) - draw_sprite (cuBuff, 1, x -148, y -233,)

#

I can't have this if statement there as it needs to draw based on the cuBuff variable and it may not equal "sprite8"

Thank you kindly.


Solution

  • You should have posted more details on how the list is created and populated. But I assume Player.buffs contains sprite indices. You forgot a semicolon in your for loop and you should use less than comperator instead of less than or equal. As an extra layer of protection, you should also check if the return value of ds_list_find_value is not undefined.

    Try something like this:

    for (i = 0; i < ds_list_size(Player.buffs); i++) {
        cuBuff = ds_list_find_value(Player.buffs, i);
        if (!is_undefined(cuBuff)) {
            draw_sprite(cuBuff, 0, x - 148, y - 233);
        }
    }