GML Trouble with attack while moving

K

Kotic

Guest
Im currently having trouble getting my character to attack while he is moving.

1. I can get him to move and attack separately
2. I can get him to start the attack animation and move while the animation is going

but I CANT get him to start the attack animation while mid movement, and after I stop moving it takes a couple seconds before it lets me attack after standing still.

Sorry for my redundant code lines and messiness. Any tips and help for coding in gml and organization is appreciated.

This is in my step for my character for swapping states, on creation, moving state is default. using enum in creation of room.
Code:
switch (states)
{
case states.attacking: scr_attacking(); break;
case states.moving: scr_moving(); break;
}
Moving script

Code:
var dir = point_direction(x,y,mouse_x,mouse_y); //variable to point character at mouse area

max_speed = 8 //player speed
image_speed = 0 //no animation while standing still
if (speed > max_speed) { speed = max_speed; } //max speed to try and stop fast diag move

if mouse_check_button_pressed(mb_left) //attack button to change states and run script
{
states = states.attacking
}

 //4 directions that determine which sprite to use
if(dir > 45 && dir <= 135){
    if (sprite_index!=Spr_darkelfwu) image_index = 0;
  sprite_index = Spr_darkelfwu;
}
if(dir > 135 && dir <= 225){
     if (sprite_index!=Spr_darkelfwl) image_index = 0;
  sprite_index = Spr_darkelfwl;
}
if(dir > 225 && dir <= 315){
    if (sprite_index!=Spr_darkelfwd) image_index = 0;
  sprite_index = Spr_darkelfwd;
}
if(dir > 315 || dir <= 45){
   if (sprite_index!=Spr_darkelfwr) image_index = 0;
  sprite_index = Spr_darkelfwr;
}
//movement
if (keyboard_check(ord('D')))
{
image_speed = 10
x += 8;
}
if (keyboard_check(ord('A')))
{
image_speed = 10
x -= 8;
}
if (keyboard_check(ord('W')))
{
image_speed = 10
y -= 8;
}
if (keyboard_check(ord('S')))
{
image_speed = 10
y += 8;
}
and Attacking script
Code:
max_speed = 8
if (speed > max_speed) { speed = max_speed; }

image_speed = 0.5
dir = point_direction(x,y,mouse_x,mouse_y);

if(dir > 45 && dir <= 135){
     if (sprite_index!=Spr_darkelfau) image_index = 0;
  sprite_index = Spr_darkelfau;
}
if(dir > 135 && dir <= 225){
     if (sprite_index!=Spr_darkelfal) image_index = 0;
  sprite_index = Spr_darkelfal;
}
if(dir > 225 && dir <= 315){
     if (sprite_index!=Spr_darkelfad) image_index = 0;
  sprite_index = Spr_darkelfad;
}
if(dir > 315 || dir <= 45){
      if (sprite_index!=Spr_darkelfar) image_index = 0;
  sprite_index = Spr_darkelfar;
}

if (keyboard_check(ord('D')))
{
x += 8;
}
if (keyboard_check(ord('A')))
{
x -= 8;
}
if (keyboard_check(ord('W')))
{
y -= 8;
}
if (keyboard_check(ord('S')))
{
y += 8;
}

if image_index = 5
{
states = states.moving
}
Gif of what the code does

https://gyazo.com/23c1f6a6615bfc57e27d48aa8600155d
 

CoderJoe

Member
Thats a lot of code. Looks ok but maybe adding a return statement in the movement script would help. When you change the state to attacking, a return statement should be added to keep the rest of the code of that script from running.
Also just imo I would have a idle state. You could switch to this state at the end of movement or attacking. It seems like you sorta implemented that by setting image speed to 0 in the movement script. For a good state machine, you want all checking and switching states done in one loop (probably the step event). Then each state script can manage its own code. You can also have certain states "override" others (like attacking in the middle of movement) or states that just go into an idle state when they are finished.
...That was longer than expected but hopefully that helps!
 
K

Kotic

Guest
Thats a lot of code. Looks ok but maybe adding a return statement in the movement script would help. When you change the state to attacking, a return statement should be added to keep the rest of the code of that script from running.
Also just imo I would have a idle state. You could switch to this state at the end of movement or attacking. It seems like you sorta implemented that by setting image speed to 0 in the movement script. For a good state machine, you want all checking and switching states done in one loop (probably the step event). Then each state script can manage its own code. You can also have certain states "override" others (like attacking in the middle of movement) or states that just go into an idle state when they are finished.
...That was longer than expected but hopefully that helps!
I think overriding the movement script with attacking might be my solution. Could explain to me how I'd have 1 state override another? I still have movement code in my attacking script so in theory I'd still be able to move while attacking even if my movement script isnt active.
 

CoderJoe

Member
So pretty much you've already sorta done that by having a check for attack in the beginning of the movement script. You just need to make sure that it actually switches states to attacking (and probably stops the movement code unless you want to move and attack). I can't really design the code for you since I don't know your needs or how the rest of your game is structured. You might take a look at using the debugger to help make sure states are changed.
 
Top