Switching the enum state and keep moving

Kami

Member
Hi, I have an action rpg project. In the player object i have an enum with 3 states: move state, dash state and melee state. When I´m dashing or attacking the player just stops until it finish the action.
It turns out that I´m trying to make the player move when i´m attacking or dashing (before the player dash, you can press the right button of mouse, select where you´re gonna dash, and release the button, making the player dash to where you selected. In this case, I want to make the player move when I´m selecting where I´m gonna dash).
If someone has an ideia to how overcome this, I would be so grateful. Thanks.
 

Kami

Member
Post your code, because it sounds like your state machine isn't even set up properly.
GML:
switch(state)
{
    case STATE.IDLE:
    {
        script_execute(move_state());
        break;
    }
    
    case STATE.DASH:
    {
        script_execute(dash_state());
        break;
    }
    
    case STATE.MELEE:
    {
        script_execute(melee_state());
        break;
    }
}

if(key_dash)
{
    state = STATE.DASH;
}

if(key_attack)
{
    state = STATE.MELEE;
}
this is my player´s step event, I tried to paste the code in every state script, but it didn´t work
 

Joe Ellis

Member
But why don't you just assign the state scripts themselves to "state" and execute them without the switch statement? Cus using an enum for the state is exactly the same as using the script itself, they're both globally recognized names..

You should also put the key_dash and attack checks in each state, cus the dash and melee states won't be checking for it while the player is doing those moves

So you basically just need in the step event:

GML:
script_execute(state)
 

kpenrose92

Member
Joe Ellis is right, but that won't necessarily solve your problem. If you want help with the problem, we need to see the code for the individual scripts as well.
 

Nidoking

Member
PROBLEM: Player does not move during "attack" or "dash" state.
CAUSE: Scripts for "attack" and "dash" state do not contain movement.
SOLUTION: Put the movement where you want the movement to happen.

This is really simple, everyone.
 

Yal

🐧 *penguin noises*
GMC Elder
I tried to do that, but it didn´t work
How didn't it work? There's way more ways things can not work than there are ways they can work. Also, we can't really do anything without seeing the movement code (and how it looked when you tried pasting it in the other states)
 

Kami

Member
Code:
key_up = keyboard_check(ord("W"));
key_down = keyboard_check(ord("S"));
key_right = keyboard_check(ord("D"));
key_left = keyboard_check(ord("A"));
key_dash = mouse_check_button(mb_right);
key_dash_released = mouse_check_button_released(mb_right);
key_attack = mouse_check_button(mb_left);

inputDirection = point_direction(0,0,key_right-key_left,key_down-key_up);
inputMagnitude = (key_right - key_left != 0) || (key_down - key_up != 0);
this is my get_input script.

Code:
script_execute(get_input);

#region Mov. bas.

//mover
hsp = lengthdir_x(inputMagnitude * spd, inputDirection);
vsp = lengthdir_y(inputMagnitude * spd, inputDirection);

#region colisao

//horizontal

if(place_meeting(x + hsp,y,oWall))
{
    while(!place_meeting(x+hsp,y,oWall))
    {
        x += sign(hsp);
    }
    
    hsp = 0;
    
}

x = x + hsp;

//vertical

if(place_meeting(x,y+vsp,oWall))
{
    while(!place_meeting(x,y+vsp,oWall))
    {
        y += sign(vsp);
    }
    
    vsp = 0;
    
}

y = y + vsp;

#endregion

#endregion

#region Animation

var _oldSprite = sprite_index;
if(inputMagnitude != 0)
{
    direction = inputDirection;
    sprite_index = spriteRun;
}
else sprite_index = spriteIdle;
if(_oldSprite != sprite_index) localFrame = 0;

//update image index
player_animate_sprite();

#endregion

#region Checks

if(key_attack)
{
    state = melee_state;
}

if(key_dash)
{
    state = dash_state;
}

#endregion
this is my move_state.

Code:
key_up = keyboard_check(ord("W"));
key_down = keyboard_check(ord("S"));
key_right = keyboard_check(ord("D"));
key_left = keyboard_check(ord("A"));
key_dash = mouse_check_button(mb_right);
key_dash_released = mouse_check_button_released(mb_right);
key_attack = mouse_check_button(mb_left);

inputDirection = point_direction(0,0,key_right-key_left,key_down-key_up);
inputMagnitude = (key_right - key_left != 0) || (key_down - key_up != 0);

hsp = lengthdir_x(inputMagnitude * spd, inputDirection);
vsp = lengthdir_y(inputMagnitude * spd, inputDirection);


if(key_dash)
{
    draw_sprite_ext(sprite_index,0,mouse_x,mouse_y,1,1,0,c_white, .5);
    image_speed = 0;
    
}

draw_self();

if(key_dash_released)
{
    var path = path_add();
    path_set_closed(path,false);
    path_add_point(path,x,y,0);
    path_add_point(path,mouse_x,mouse_y,0);
    
    var distance = round(point_distance(x,y,mouse_x,mouse_y));
    
    //amount of ghosts
    var ghostsToMake = ceil(distance / 96);
    
    for(var i = .96; i >= 0; i -= .9 / ghostsToMake)
    {
        var ghost = instance_create_depth(path_get_x(path, i), path_get_y(path,i), depth, oPlayerDash);
        ghost.image_alpha = i;
    }
    
    x = mouse_x;
    y = mouse_y;
    
    //animate
    image_speed = 0;
    image_index = image_number - 1;
    state = move_state;
}

image_speed = 1;
this is my dash_state (with the movement)

Code:
script_execute(get_input);

hsp = lengthdir_x(inputMagnitude * spd, inputDirection);
vsp = lengthdir_y(inputMagnitude * spd, inputDirection);

//attack start
if(sprite_index != sPlayerMelee)
{
    //set up correct animation
    sprite_index = sPlayerMelee;
    localFrame = 0;
    image_index = 0;
    
    //Clear hit list
    if(!ds_exists(hitByAttack, ds_type_list)) hitByAttack = ds_list_create();
    ds_list_clear(hitByAttack);
}

CalcAttack(sPlayerMeleeHB);

//update sprite
player_animate_sprite();

if(animationEnd)
{
    state = move_state;
    animationEnd = false;
}
this is my melee_state

Code:
//use attack hb check for hits

mask_index = argument0;
var hitByAttackNow = ds_list_create();
//creating object list
var hits = instance_place_list(x,y,oBullet,hitByAttackNow,false);
//if something has been hit
if(hits > 0)
{
    for(var i = 0; i < hits; i++)
    {
        //if this instance gas not been hit, hit it
        var hitID = hitByAttackNow[| i];
        if(ds_list_find_index(hitByAttack, hitID) == -1)
        {
            ds_list_add(hitByAttack,hitID);
            //the attack happens here
            with(hitID)
            {
                image_blend = c_red;
            }
        }
    }
}

ds_list_destroy(hitByAttackNow);
and this is my attack calculator state

there is also other scripts like the animate script:
Code:
var _cardinalDirection = floor(((direction div 45) + 1) * 0.5);
var _totalFrames = sprite_get_number(sprite_index) / 4;
image_index = localFrame + (_cardinalDirection * _totalFrames);
localFrame += sprite_get_speed(sprite_index) / FRAME_RATE;

//if animation loop on next game step
if(localFrame >= _totalFrames)
{
    animationEnd = true;
    localFrame -= _totalFrames;
}
else animationEnd = false;
and for my step event, I just wrote script_execute(state)
for the variable state, I just setted it to my move state in create event: state = move_state;

I hope this is what you asked, I don´t have any other scritps to show.
 

Kami

Member
What do you desire the game to do? What does it do that is not the answer to the first question? Be specific.
I want to do exactly what i asked. People asked me to paste de move code in my state scritps, I did that, but it dosen´t work, so they asked for my code, and I sended all this. but I just want to know how I can solve this problem
 

Kami

Member
I want to do exactly what i asked. People asked me to paste de move code in my state scritps, I did that, but it dosen´t work, so they asked for my code, and I sended all this. but I just want to know how I can solve this problem
when i pasted all the move code in my dash and melee states, nothing changed at all
 

Nidoking

Member
when i pasted all the move code in my dash and melee states, nothing changed at all
Well I don't see that in the script where you say: "this is my dash_state (with the movement)", so I'm going to assume you didn't do it properly. Do you actually understand what these scripts do, or did you just copy them from somewhere and expect them to work a particular way? Can you explain the effect of each line? If yes, then I don't understand why you're confused here. If no, then I don't understand how you expect anyone to be able to solve this problem.
 
Top