(State Machine) Am I in the right direction with this?

So today I tried to make a navigatable menu using only knowledge I have gathered from state machines and enums. At the moment nothing shows up. This is what I have inserted into the room... just these 4 things.
test_fightScreenInterface - GameMaker Studio 2_ 10_24_2020 7_29_51 PM.png

And here is the code I have for a parent object that I have made for each of the 4 buttons.

test_fightScreenInterface - GameMaker Studio 2_ 10_24_2020 6_04_23 PM.png


And I have written the following code in each and every object's individual creation in the room.
(Just open the image in a new tab. It should show up better in a new tab.)

973548h.png
(the one's with '_hover' at the end of the name is simply the same sprite but lighter colors and the symbol is the SOUL heart)


The buttons show up when I run the game but no desired hovering is working. I feel like a little bit of this is a step in the right direction but I don't know where I screwed up. I really need some help with what is going on.
 

chefdez

Member
You need to add the parent into the room. I believe it needs to load first in the instance creation order so that your method will work.
 
  • Dislike
Reactions: Yal
When I insert the parent, the following ERROR shows up.

Code:
############################################################################################

ERROR in

action number 1

of  Step Event0

for object obj_fightButton_PARENT:



Variable obj_fightButton_PARENT.regularState(100003, -2147483648) not set before reading it.

at gml_Object_obj_fightButton_PARENT_Step_0 (line 5) -        image_index = regularState;

############################################################################################

gml_Object_obj_fightButton_PARENT_Step_0 (line 5)
 

Slyddar

Member
You need to add the parent into the room. I believe it needs to load first in the instance creation order so that your method will work.
This is not correct. The parent code will run for the children, as long as the children events don't overwrite the parents event. The parent object does not need to be placed in the room.

For the OP, placing that code in the instance creation code doesn't work as it will only run once. Instead you could have a switch statement in the parent step event, that looks at the sprite_index. That way you can place all the code there.

You really should use an array to manage this. I made this tutorial a while back, which might help you.

 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
So today I tried to make a navigatable menu using only knowledge I have gathered from state machines and enums. At the moment nothing shows up. This is what I have inserted into the room... just these 4 things.
View attachment 35263

And here is the code I have for a parent object that I have made for each of the 4 buttons.

View attachment 35265


And I have written the following code in each and every object's individual creation in the room.
(Just open the image in a new tab. It should show up better in a new tab.)

View attachment 35266
(the one's with '_hover' at the end of the name is simply the same sprite but lighter colors and the symbol is the SOUL heart)


The buttons show up when I run the game but no desired hovering is working. I feel like a little bit of this is a step in the right direction but I don't know where I screwed up. I really need some help with what is going on.
Creation code is only run once, after the instance's Create Event is run. So the keyboard checks will only be checked once, and nothing happens unless you hold left / right when the room loads. You should move those to the Step Event of the objects instead.

And actually, you might as well move the state setup code to the Create Event while you're at it, since you have 4 separate unique objects instead of 4 instances of the same object... instance creation code is mostly useful when you want a specific instance to do something different from every other instance of that object (e.g. set the sprite and dialogue of an NPC object to something unique for that character), but in this case where you have buttons that will always do the same thing every time, it's just spreading your code around so it's harder to maintain. Just put an event_inherited() as the first line of the Create Event (to run the parent's setup code) and then paste the now-Creation Code, and you'll run both the parent's code and the custom code.

There's two caveats with moving the keyboard checks to the step event: key checks are global, so depending on execution order, more than one of the buttons could run the "hover the next button" code when a button is pressed. Also, keyboard_check() is true every frame a key is held, so you'd change to the next button 60 times per second. You might wanna add in a global cooldown before the focused button can be moved again, solving both of these issues at once.
 

chefdez

Member
This is not correct. The parent code will run for the children, as long as the children events don't overwrite the parents event. The parent object does not need to be placed in the room.

For the OP, placing that code in the instance creation code doesn't work as it will only run once. Instead you could have a switch statement in the parent step event, that looks at the sprite_index. That way you can place all the code there.

You really should use an array to manage this. I made this tutorial a while back, which might help you.

Oh I missed that the code was place in the creation code. It makes sense that the code should be in the corresponding step event. Thanks I didn't know children code will run even if the parent isn't in the room.
 

chamaeleon

Member
Oh I missed that the code was place in the creation code. It makes sense that the code should be in the corresponding step event. Thanks I didn't know children code will run even if the parent isn't in the room.
And if you do place code in each child object's step event, you may or may not need to call event_inherited();. If you don't, the parent code won't execute, at which point you'll have to think about why the child is a child in the first place, is it because you do want event code to be inherited and executed as you might expect, or is it for the purpose of having an object type you can specify in functions that takes objects in order to match/process instances that are derived from this parent class, but does not have the same behavior at all.
 

Yal

🐧 *penguin noises*
GMC Elder
And if you do place code in each child object's step event, you may or may not need to call event_inherited();. If you don't, the parent code won't execute, at which point you'll have to think about why the child is a child in the first place, is it because you do want event code to be inherited and executed as you might expect, or is it for the purpose of having an object type you can specify in functions that takes objects in order to match/process instances that are derived from this parent class, but does not have the same behavior at all.
I've got a habit to just put event_inherited as the first line of every event, even if the object doesn't have a parent... just in case that'll change later. (Not for things that obviously will never have a parent, like one-off controllers and particle objects, but for things that might get one later, and also definitely for things that already have a parent, but it doesn't have code in those events - because if that changes later and only some of the children will be affected, weird bugs will happen)
 
Top