Using the Begin Step to give priority to one instance_change over antoher

Bentley

Member
Hello, I've been working on a Street Fighter like project (just for practice, there's no way I can actually do it). I've come across a tricky problem. For example: "D" is to punch, but holding back for two seconds and pressing "D" is also how you shoot your special. To solve this problem, I decided to give priority to special moves. I did this by placing the code for the special in the Begin Step (I suppose you could place it above the other code in the Step, but I foresee that becoming a bit messy).

I'm using objects as states and I'm using instance_change to change states. Here's the code

Code:
BEGIN STEP EVENT

//Special
if (key_right) && (key_left)
{
    if (key_punch)
    {
        instance_change(obj_guile_special, true);
        exit;
    }
}

STEP EVENT

//Enter Punch State
if (key_punch)
{
    instance_change(obj_guile_punch, true);
    exit;
}


I don't know that much about the Begin Step. I only know that it runs before the other Step Events (which is why I'm using it to give priority).

I know that there are going to be countless other problems like this. Am I using the Begin Step the right way? Is there a better way to approach this problem? Thank you for reading.
 
Last edited:

chamaeleon

Member
Any reason this wouldn't work?
Code:
if (key_right) && (key_left)
{
    if (key_punch)
    {
        instance_change(obj_guile_special, true);
        exit;
    }
}
else if (key_punch)
{
    instance_change(obj_guile_punch, true);
    exit;
}
I would think that using begin and step events in addition to regular step events would make more sense when coordinating behavior between different instances. In this case, it seems it's just a matter of keep track of key presses and what combination triggers what behavior. In a more general sense you may want to look into state machines (many examples exist in the forum, just search for state machine and see what style of implementation appeals to you and your comfort level with programming).

Edit: Missed reading you use instances as states. Well, anyway, without more context of code I am not sure I see a benefit breaking things down into different step events.
 
This is definitely not how you would normally handle this.

You should be using a finite state machine. No instance changes. (In fact, in my 9 years with GM I have never once used instance_change).

Do some research on fighting game programming in general and you'll hear the same thing. And while you are reading, you'll hear a lot about how you can use a finite state machine to give priority to certain states.
 

sylvain_l

Member
Edit: Missed reading you use instances as states. Well, anyway, without more context of code I am not sure I see a benefit breaking things down into different step events.
it has perhaps a value; following instance_change the new object isn't fully available until next step event.
so doing the change in the begin step and then swithing to the step event for the follow action could be the right thing to do.
(cause the step event of the previous event isn't going to be run)

but doing it the way he does for precedence between combo key, is risky (that mean a spaghetti of key check around each possible "state" object and their different begin steps/steps events).
because if you change to object1 in object0-begin_step; then what's going to be executed is what is in the object1-step event.
same also go when from objectX-begin_step event you switch to object0-step event without running before it begin_step (so you'd better be damn sure what you are doing and the precedence switch/escape by swithing object instance is really what you want to happen)

so @Bentley are you sure that's what you want ? (IMO following @chamaeleon or @Pixelated_Pope would result in much cleaner/readable/bugfree solution)
 

TheouAegis

Member
You want to hold the key for 2 seconds?

Code:
if (key_right)
{
    if image_xscale == 1
        hold_to += 1;
    else
        hold_fro += 1;
}
else
if (key_left)
{
    if image_xscale == 1
        hold_from += 1;
    else
        hold_to += 1;
}
else
{
    hold_to = 0;
    hold_fro = 0;
}
Then when you punch...

Code:
if key_punch
{
    if hold_to >= room_speed * 2
        //do one attack
    else
    if hold_fro >= room_speed * 2
        //do another attack
    else
        //do normal punch
}
Gets more complicated when you start adding serial combos.
 

Bentley

Member
Hey Chameleon, thanks for the reply. You pointed out something I missed: the Step still runs. The logic behind it was to instance change in the Begin Step so that the code in the Step wouldn't run. As you pointed out, it will.

Is my understanding correct?
 
Last edited:

Bentley

Member
You want to hold the key for 2 seconds?

Code:
if (key_right)
{
    if image_xscale == 1
        hold_to += 1;
    else
        hold_fro += 1;
}
else
if (key_left)
{
    if image_xscale == 1
        hold_from += 1;
    else
        hold_to += 1;
}
else
{
    hold_to = 0;
    hold_fro = 0;
}
Then when you punch...

Code:
if key_punch
{
    if hold_to >= room_speed * 2
        //do one attack
    else
    if hold_fro >= room_speed * 2
        //do another attack
    else
        //do normal punch
}
Gets more complicated when you start adding serial combos.
Thanks TheouAegis. Yeah, that's the way I should handle the "back for two seconds" special. Right now, I just have it where the player holds left and right and then presses punch. It's this way temporarily until I figure out a priority system. But your code works, it does the special how I'd want and gives priority to it over the punch key.
 
Top