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

GUI Window System Not Working

Gamerev147

Member
I have a controller object for my GUI which creates a global list and draws the objects in the list from last to first. The objects are essentially windows that simply add their ID to the list.
It goes as follows:

obj_WindowController - Create
GML:
globalvar WINDOWS;
WINDOWS = ds_list_create();
obj_WindowController - Draw GUI
GML:
if (!ds_list_empty(WINDOWS)) {
    for (var i = ds_list_size(WINDOWS); i > 0; i--) {
        var window_id = ds_list_find_value(WINDOWS, i);
       
        if (window_id != 0) && (window_id != undefined) {
            with (window_id) {
                draw_set_color(c_gray);
                draw_roundrect(xx, yy, ww, hh, false);
                draw_set_color(c_white);
                draw_roundrect(xx, yy, ww, hh, true);
                //etc...
            }
        }
    }
}
And as I said before, the obj_Window simply adds its ID to the global list.

My problem is that the windows just don't draw. And when I try to create multiple of them, my game freezes.
What am I doing wrong? Any help is appreciated.

Thanks!
 

Nidoking

Member
The indices in a list go from 0 to ds_list_size - 1. You're starting at ds_list_size. That's not an element. That may not be your problem, but if you're going to iterate backwards, you should start at the actual last entry.

Might be a good idea to stick debug prints in there so you can see what's actually happening. Using the debugger to inspect WINDOWS and see the actual entries in it would also be a good idea.
 

Pzaan

Member
Is window_id the id of an instance of obj_Window or the id of the object?

Are xx, yy, ww, hh properties on each obj_Window instance or temporary variables defined elsewhere in obj_WindowController DrawGUI event?
 

Roldy

Member
What am I doing wrong?
Not debugging, that is what you are doing wrong.

When you have a problem with your code then you need to debug your code.

The debugger is key and you should get familiar with using it. Read the manual and practice with it.

Other than the debugger you can use things like show_debug_message to quickly make sanity check and determine which parts of code are being run or not and what the current state of things is.

e.g.

GML:
if (!ds_list_empty(WINDOWS)) {
    show_debug_message("List not empty"); // Verify your list has things in it

    for (var i = ds_list_size(WINDOWS); i > 0; i--) {
        var window_id = ds_list_find_value(WINDOWS, i);

        if (window_id != 0) && (window_id != undefined) {
            with (window_id) {
                show_debug_message("I am trying to draw stuff"); // Verify some object is trying to draw
                draw_set_color(c_gray);
                draw_roundrect(xx, yy, ww, hh, false);
                // Verify your data
                show_debug_message(string(xx) + " " + string(yy) + " " + string(ww) + " " + string(hh));
                draw_set_color(c_white);
                draw_roundrect(xx, yy, ww, hh, true);
                //etc...
            }
        }
    }
}
Debug your code until you narrow down the problem and have a specific question. Then ask that specific question here after looking through the manual for an answer.

Most likely with a bit of debugging you will find your own answers.
 

Gamerev147

Member
Is window_id the id of an instance of obj_Window or the id of the object?

Are xx, yy, ww, hh properties on each obj_Window instance or temporary variables defined elsewhere in obj_WindowController DrawGUI event?
window_id is the instance ID of the window object.
Those variables are held in the window object.
 

Gamerev147

Member
Not debugging, that is what you are doing wrong.

When you have a problem with your code then you need to debug your code.

The debugger is key and you should get familiar with using it. Read the manual and practice with it.

Other than the debugger you can use things like show_debug_message to quickly make sanity check and determine which parts of code are being run or not and what the current state of things is.

e.g.

GML:
if (!ds_list_empty(WINDOWS)) {
    show_debug_message("List not empty"); // Verify your list has things in it

    for (var i = ds_list_size(WINDOWS); i > 0; i--) {
        var window_id = ds_list_find_value(WINDOWS, i);

        if (window_id != 0) && (window_id != undefined) {
            with (window_id) {
                show_debug_message("I am trying to draw stuff"); // Verify some object is trying to draw
                draw_set_color(c_gray);
                draw_roundrect(xx, yy, ww, hh, false);
                // Verify your data
                show_debug_message(string(xx) + " " + string(yy) + " " + string(ww) + " " + string(hh));
                draw_set_color(c_white);
                draw_roundrect(xx, yy, ww, hh, true);
                //etc...
            }
        }
    }
}
Debug your code until you narrow down the problem and have a specific question. Then ask that specific question here after looking through the manual for an answer.

Most likely with a bit of debugging you will find your own answers.
Not entirely my problem, because if I print the list, all ID's show up just fine.
When I loop through the list normally, not backwards, it also works just fine.
It's an issue with the loop. It's not looping backwards correctly.
 

chamaeleon

Member
The indices in a list go from 0 to ds_list_size - 1. You're starting at ds_list_size. That's not an element. That may not be your problem, but if you're going to iterate backwards, you should start at the actual last entry.
Also, it needs a different stop condition to include the zeroth element.
 
I always find it kinda funny when people reply to their topics without having tested all the solutions given. Nidoking has told you exactly what the problem is (or at least, the most immediate problem, there may be others lurking behind this one). If I tell you a list has 5 entries, and you label the first entry as 0 and then start counting up from there, what number do you think the last entry will be? The size of the list minus one. 0, 1, 2, 3, 4, here we can see that you are indeed counting 5 entries, but the last entry is marked 4. That's how lists (and most other things) work on computers. They count from 0. This means if you want to start at the end of the list and count down, you start at list_size-1 and count down to and including 0.

Because you are reading outside the list, the ds_list_find_value() is returning 0 or undefined and therefore your window isn't drawing as you don't draw it when you get those values.
 
Top