OFFICIAL Tech Blog - Finite State Machines

Nocturne

Friendly Tyrant
Forum Staff
Admin
002_blog_post.png

Got a new YoYoGames tech blog for you all! Ever wondered what a Finite State Machine is or how to make one? We got you covered (both GML and DnD!)


 

jackquake

Member
Agreed. Nearly every object I create has a Finite State Machine in the step event. Very clean and organized.

Good stuff Mark! Keep 'em coming!

Edit: One more thing... I've NEVER used a user event before (didn't really understand why they were useful). Thanks to this article, now I do. I can see how they help immensely with code organization vs. scripts. More good stuff!
 
View attachment 29381

Got a new YoYoGames tech blog for you all! Ever wondered what a Finite State Machine is or how to make one? We got you covered (both GML and DnD!)​
It's been a while since I've wondered that... but yes, I have.

  • initial state: put water on to boil, go to waiting state
  • waiting state: if the water is boiling add pasta and go to cooking state otherwise keep waiting
  • cooking state: test pasta by throwing it at a wall and if it sticks then go to serving state, otherwise keep cooking
  • serving state: put the pasta on a plate and go to eating state
  • eating state: eat the pasta and if finished and still hungry go to the initial state, otherwise end.
This is a pretty good introduction to "states" but you've over-simplified something fairly complex...

Take the cooking state for example: I'd recommend setting an alarm to fire the "test pasta by throwing it against the wall" event otherwise you'll probably run out of pasta long before you get to the serving state (and don't even get me started on the "cleanup" state.) Depending on the type of pasta you're using: I'd recommend waiting 8-10 minutes and then firing "test pasta" every 60 seconds thereafter. I'd also recommend working in a "stir water" event every few minutes or so during the cooking state.

In all seriousness: This was a good read, @Nocturne. Yes, it's a bit longer than average, but you did an excellent job of speaking to the lowest common denominator and providing links for those who wish to go "farther down the rabbit-hole." You couldn't have chosen a more helpful example than using a FSM for menu buttons (something that almost every game needs) and are done poorly more often than they're done well. The inclusion of project files/assets is always a nice bonus!
I decided to check out the DND™ tutorial in addition to the GML tutorial.

First and foremost: Thank you so much for including a Drag and Drop™ version of this post. I genuinely appreciate it. I feel that DnD™ is very underrated - Go to the YYG Marketplace and search for "DnD" or "Drag and Drop" if you don't believe me.) 👻

It makes me happy to see it getting the attention which it deserves. I'll be honest: I've "taken a quick look" at using DnD™ in GMS2 but I didn't look very closely. This tutorial was the first time that I've seen a "higher level" concept (macros, switch statements, etc.) in this format. YYG has done a terrific job of expanding what's possible with Drag and Drop™ and has stayed true to it's original spirit (which is to allow someone who knows nothing about programming to create their first game(s) and learn the fundamentals of programming in the process... but on the other hand, it seems to have lost some of it's simplicity.

This is the first screenshot in the article:

This doesn't look much more simple than using GML to accomplish the same task... but I haven't used it so take that with a shaker of salt.

One thing that does bother me is how tall it is (which made this difficult to read on a low-resolution monitor.) If I designed the UI: I would have used more horizontal space and a bit less vertical space. Take the Macros block for instance: I'd move Value parallel to Macro. Little things like that, and reducing padding/margins could really add up and make it much easier to skim a project written in DnD™ and/or follow along with this tutorial (which, again, was excellent.)

Edit: On second thought, something like this would help reduce UI clutter without sacrificing the readability of screenshots.

field-suggestion.gif

Just some food for thought. I'm looking forward to the next tutorial.

Thanks to everyone at YYG for the hard-work they put into developing and supporting GMS. Y'all are the real MVPs!

PS: Is there (or can there) be a fast and easy way to generate handsome-looking screenshots like the ones in this tutorial? The thought of taking multiple screenshots, removing the background color (and then cropping/padding the images to line-up properly) is just dreadful. If I don't have to do that - I'll start posting simple DND tutorials on the YYG Marketplace and in the Programming Forum.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Thank you @SilentxxBunny for the excellent feedback! I am making a conscious effort to (where possible) use DnD in equal measure with GML as it is far more powerful now than it ever was before, and it IS actually used by a lot of people. Now, let's address some of the points you raise:

This doesn't look much more simple than using GML to accomplish the same task... but I haven't used it so take that with a shaker of salt.
Well, really, DnD isn't simpler than GML and probably shouldn't be thought of as such... it should be thought of as visual GML, with the same capacity, but formatted for people that are more visually orientated than code/logic orientated.

One thing that does bother me is how tall it is (which made this difficult to read on a low-resolution monitor.) If I designed the UI: I would have used more horizontal space and a bit less vertical space. Take the Macros block for instance: I'd move Value parallel to Macro. Little things like that, and reducing padding/margins could really add up and make it much easier to skim a project written in DnD™ and/or follow along with this tutorial (which, again, was excellent.)
Yup, I agree completely, and I pushed for a long time when GMS2 was in developement to use more horizontal space and less vertical space. However, this was what was done and this is how it is and I doubt it'll be getting changed in the 2.X lifetime. It is definitely something that YYG wants to address in the future though!

PS: Is there (or can there) be a fast and easy way to generate handsome-looking screenshots like the ones in this tutorial? The thought of taking multiple screenshots, removing the background color (and then cropping/padding the images to line-up properly) is just dreadful. If I don't have to do that - I'll start posting simple DND tutorials on the YYG Marketplace and in the Programming Forum.
Nope, sorry. No easy way about it... My solution has been to set up a few Photoshop actions that take most of the pain out of the work, but even then every single screenshot needs a fair bit of manual editing to get it to look good (like removing the GMS shadows from all the action boxes... what a pain!). On average a DnD tutorial takes me twice as long to do than a GML one, even though the text is almost always the exact same because of the need to create and edit all the images. That said, I see no reason why you'd want to do this when you can simply do a rectangular crop that includes the DnD workspace background. Sure it's not quite as pretty but it'll be fine and functional.

Thanks again!
 
Z

zendraw

Guest
i dont u nderstand why people use enums? as if you cant just use local variables. what specific case is there where enums excel at?
 

Crescent

Member
i dont u nderstand why people use enums? as if you cant just use local variables. what specific case is there where enums excel at?
Enums are great for things with predefined structure that are used in multiple places in your code.

For instance, stats in an RPG:

GML:
enum STAT {
    HP,
    MP,
    ATK,
    DEF,
    SPD
}
If you have an array called "stats" in each relevant instance, you can now access the HP stat with:

Code:
my_hp = stats[STAT.HP];
 
D

Deleted member 45063

Guest
i dont u nderstand why people use enums? as if you cant just use local variables. what specific case is there where enums excel at?
Other than what @Crescent stated (and many other useful cases for enums) you can also generally think of enums as namespaced constants, so following the provided example, instead of having a bunch of macros:
GML:
#macro STAT_HP 0
#macro STAT_MP 1
#macro STAT_ATK 2
#macro STAT_DEF 3
#macro STAT_SPD 4
You can just have an enum with the closely related constants. It also helps clean up autocomplete in that regard.
 
Last edited by a moderator:

Evanski

Raccoon Lord
Forum Staff
Moderator
AND FOR SWITCH STATEMENTS!
GML:
enum hit_list {
    bob,
    fred,
    gorge,
    harold,
    jerimiha
}
switch(people_to_attack_with_bees)
{
    case hit_list.bob: instance_Destroy(bob_house); break;
    case hit_list.fred: instance_Destroy(george); break;
    case hit_list.harold: instance_Destroy(oatmeal); break;

    default: game_end(); break;
}
 

drandula

Member
Enums can be used for "magic numbers" which occur in many places as same. This helps readibility, as it gives context (eg. Position[coord.z] instead of Position[2]) . Also when you need to change "magic number" value everywhere, you need to just update enums.

Magic numbers in code are usually bad, so with enums you can avoid them. Or you can use Macros, which I use to predetermine size of datastructures, when they are constant and used globally. Instead of using "8" I can say "datasize_status" etc.
 
Last edited:

Crescent

Member
Other than what @Crescent stated (and many other useful cases for enums) you can also generally think of enums as namespaced constants, so following the provided example, instead of having a bunch of macros:
GML:
#macro STAT_HP 0
#macro STAT_MP 1
#macro STAT_ATK 2
#macro STAT_DEF 3
#macro STAT_SPD 4
You can just have an enum with the closely related constants. It also helps clean up autocomplete in that regard.
You're right; forgot to mention how useful it is to have the code completion show you all enums under that (so-called) namespace.
 
Last edited:

Elodman

Member
Many thanks for all these fine coffee ( or strawberry ) - break tuts !

( reading them may take a coffee-break, but then better not to count the hours necessary to rewire a snail-coder brain like mine )

Quite a cool & unique oasis GMC has become, or rather remained, methinks. 🏆
 
Top