• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] Where should I put hit, knockback, immunity and health - state machine or?

MartinK12

Member
I have basic working state machine for player in platformer game and it’s working fine.
Currently I have player states: on_ground, in_air, idle.

I want to add hit – for starter it will be basic collision event but later I want to create my own collision code in end step.

Whenever player gets hit by enemy I want to implement those things:
-knockback – player will be moved to the opposite side and above just few pixels,
-immunity - for 3 seconds player won’t be able to get hit again,
-lower health,
-player must be able to move all the time.

Should I:
-create hit state
-just implement those above things into states I already have
-just put all those things into step?

Where to put those things? I just can't get the grasp of hit :(
What solution is the most commonly used for hit?
Thank You
 

Rob

Member
So when the player is hit you want this to happen:
  • Move Player back (checking for collisions)
  • Set immunity timer
  • player hp -= damage
  • Player shouldn't be able to move while being knocked back, right?
Code:
//wherever you check for being hit by a monster, the code should be ignored if "obj_player.immune_timer > 0"

//Step event of player
if (hit == true && immune_timer == 0){ //Im presuming you have a boolean called "hit" that is set to true when contact is made with an enemy
  immune_timer = room_speed * 3;
  hp -= damage;
  knockback = true;
}

//Run down immunity timer
immune_timer --;

//In movement code, check to see if knockback is true and disable normal movement if knockback is true (I presume you want the player to be knocked back regardless of what they were trying to do before)
This could be wrong depending on what code you have at the moment though. Not every example works in every situation
 

MartinK12

Member
Ah, finally I'm getting some clearer picture of how this should work :) Big Thank You :)

Yes, player should not be able to move while knocked back but I want player to move while immune - immunity will be longer then knockback, but I can do this.

So @Rob you usually put this code in step and already existing states? You don't create hit state, right?
 
M

maxdax5

Guest
The way i do it, I create an instance of that specific event. Tho this is using action boxes. Press a number 1 - 9 and the box that has that number create the skill.

Create event of nitrous skill:
Code:
//This skill make the player go faster for x time.
creator = x;  // Dont need the position of it so store the id of the action box that created it.
creator.cooldown = 10*room_speed;
timer = 1.2*room_speed;
ini_open(working_directory+string(version)+'\saves\'+string(obj_player.tank_slot)+'.sav');
topspeed = ini_read_real(string(obj_player.tank_slot),'topspeed',0.82);
lvl = ini_read_real(string(obj_player.tank_slot),'skill_nitrous',0);
ini_close();
topspeed = topspeed*(0.5+(lvl*0.1));
obj_player.topspeed += topspeed;
obj_player.dspeed = obj_player.topspeed;
Step event of nitrous skill:
Code:
//Here is the duration of the skill and when ending, return back the speed of the player to default.
if (timer > 0){
    timer -= 1;
}else{
    obj_player.topspeed -= topspeed;
    instance_destroy();
}
 

Rob

Member
Ah, finally I'm getting some clearer picture of how this should work :) Big Thank You :)

Yes, player should not be able to move while knocked back but I want player to move while immune - immunity will be longer then knockback, but I can do this.

So @Rob you usually put this code in step and already existing states? You don't create hit state, right?
I think I made something like it one time - a top down action rpg smacking goblins into a wall but I don't think it needs its own state, especially if you want the player to be able to do other stuff whilst the "hit" stuff is happening.

State systems are a good way to close off code - to make sure that one code block isn't interfering with another - but in this case you want the player to be able to do the other things whilst immune.

Don't forget to set hit to false when immune_timer = 0;

Also - if you find a way that's easier to understand and works, feel free to ignore what I put as there may be a better/easier way to do it depending on your code
 
Top