SOLVED Struct not set before reading it

netoxinaa

Member
Hello, I'm new to structs and I came up with this idea of using them for every Menu I have in my game, so each struct menu handles his own variables, text, keys, positioning, etc. In a create event I declared 2 structs, itemMenu and itemOptionMenu. Both of them have almost the same variables and a function "display" which is used to draw the menu and to handle selections. In the step event I added that if a struct variable is true, execute their display function respectively. I want itemMenu to show up first, then if spacebar is pressed, I want itemOptionMenu to show up by changing a variable to true. This spacebar checking is inside the display function of itemMenu. However when I do press it, it pops and error saying "Variable struct.itemOptionMenu(100217, -2147483648) not set before reading it.". I don't know why this happens because both structs are declared in the create event. This is the code:

Create Event
GML:
itemMenu = {
    //Some other vars
   
    activeKeys: true,
    show : false,
    display :    function(){
                    //Drawing the menu and handling selection
                   
                    if (keySelect)
                    {
                        activeKeys = false;
                        itemOptionMenu.show = true;
                        itemOptionMenu.activeKeys = true;
                    }
                }
};

itemOptionMenu = {
    //Some other vars
   
    activeKeys:  false,
    show : false,
    display :    function(){
                    //Drawing the menu and handling selection
                }
};
Step Event
GML:
if (itemMenu.show) itemMenu.display();
if (itemOptionMenu.show) itemOptionMenu.display();
I tried declaring itemOptionMenu before itemMenu and using with statement in the spacebar checking but none of them worked. And I couldn't find something relatable in the manual.
Edit: I don't know why in the code activeKeys is in another color, it should be white lol
 

Cpaz

Member
I know structs are a bit funky with scope.
Try tossing a breakpoint in the step event before you use the structs, are they there? If so, do they have any values?
 

FrostyCat

Redemption Seeker
Anonymous functions declared as values of a struct act from the scope of that struct, not the current instance. In your code, itemMenu clearly does not have a key named itemOptionMenu, hence the error.
 
Last edited:

netoxinaa

Member
@FrostyCat damn what a shame. I managed to accomplish what I wanted adding some if's in the step event, it would've been cooler to had all the code in just one place tho. Also I'm curious about where did you get that info, or did you know it from experience? Thanks!
 

FrostyCat

Redemption Seeker
It's easy if you improvised it a little.
GML:
itemMenu = {
    owner: id,
    activeKeys: true,
    show : false,
    display: function() {
        //Drawing the menu and handling selection
        if (owner.keySelect) {
            activeKeys = false;
            owner.itemOptionMenu.show = true;
            owner.itemOptionMenu.activeKeys = true;
        }
    }
};
 
Top