While everything that has been suggested so far can certainly be a valid and proper way of handling this, please keep in mind to tailor responses to the skill level of topic authors. OP has stated that they're "green as grass", and the clearly DnD-to-GML-converted code in the opening post should give it away that expecting them to know about the
lengthdir functions is probably asking for a bit too much at this point in time...
Of course, these functions are well-known and easy to use for experienced users, but before a newbie can even try to use them, they would first of all have to know they even
exist, or to find out that they do, they'd have to know what to search for to get the results they need. That is, by itself, a skill that needs to be learned over time, and not something a newbie should be expected to know. That's what they come here for - to ask where to start.
So,
@MonkeyLover, let's get you started. You seem to have done some preliminary research already, so good on you for that! Your plan of using a state machine for this sounds reasonable. Also, there's absolutely no need to apologize for asking how to do something... that's what this forum is for, after all.
Allow me to answer some stray questions before proceeding...
There
is a physics system wherein you can launch an object with an unpredictable landing spot, but since you said that this is a top-down game, I'd advise against using it. The physics system is mainly intended for use with games where the entire world is based on a physics simulation (think Angry Birds, Getting Over It...). Using the physics system for this would be like cutting a sheet of paper with a chainsaw: It works, but there are easier, more direct solutions available.
Before trying to look for ways to do something, I personally like to make a plan of what I want to do via the process of abstraction. Let's apply this here to figure out a simple solution to the issue. I'll be writing out my thoughts and how they lead me to the approach I'd take to implement this in the hopes that it'll give you the needed push in the right direction. Since you said you want to practice using states, I'll walk you through setting up a simple state machine.
I want to be able to dash.
I am only able to start a dash if I'm currently not dashing. (-> I have a dash state and a default state)
Dashing means moving faster than I normally do, without being able to change the direction in which I'm moving, for a limited period of time. (-> I can only control movement while in the default state)
Once I start a dash, after a fixed time, I stop dashing. (-> after dashing, I have to do something so I stop dashing after a while, e.g. setting an alarm or counting up using a variable, then changing states once time's up)
A simple way to set up a state machine is to use an
enum. If you haven't used those before, think of them as a list of descriptions something can have, e.g. a user on this community can either be a Member, Moderator or Admin, but never two at the same time and also can't be anything that's not on that list. So, we use an enum to define a list of states the player can be in:
GML:
enum PlayerState
{
DEFAULT,
DASHING
}
state = PlayerState.DEFAULT;
The main aspect of a state machine is separating what something can do depending on which state it is in. Using the setup above, we can handle this using a
switch statement:
GML:
switch (state)
{
case PlayerState.DEFAULT:
// Default state code
break;
case PlayerState.DASHING:
// Dash state code
break;
}
Put the movement code in the default section. Put the code that
applies movement to x/y after the
switch statement, because you always want to
apply movement, just not always be able to
control it.
Here's where things get interesting.
If you then add a check whether the player pressed the dash key and set the state to dashing, you'll be able to control the default and dashing state independently. Any code you add to the dash state section will only run after you've pressed the dash key.
As for limiting movement and controlling where the dash goes: If the dash is supposed to only work in the directions you can walk in, you don't really have to do anything more. Since you can't control movement while dashing, the movement you had before starting to dash will automatically carry over (since you are not setting it to any new value at all).
I hope this was more helpful than the stuff you found until now. See if you can get any further than until now with this and let us know how far you got. If you get stuck along the way, we'll be able to provide more detailed help and tell you where you took a wrong turn.