• 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!

Legacy GM Need help figuring out how to make something work.

S

supesdupes

Guest
At the moment, I'm making a little test game--a super basic side scroller-- just to learn the basics of coding. I have a room, a player character (obj_player) that can walk left and right, a chest that I can "open" (really just made it so that when I press a button the animation changes makes it look open), and a sword (obj_sword) that I've made for the player to pick up and use.
My problem is that I can't figure out how to let the player be able to attack with the sword after he picks it up. Here's what I have within obj_player so far:
  • Events:
    • Create:
      • ///Initialize Vars
        grav = .4;
        hsp = 0;
        vsp = 0;
        initial_movespeed = 1
        movespeed = initial_movespeed
        image_speed = .325

        attacking = false
        abletoattack = false
        swordobtained = false
    • Step:
      • ///Movement
        scr_input()
        scr_movement()
        scr_collision()
      • ///Animation
        if hsp != 0
        {
        sprite_index = spr_player_run
        }
        else
        {
        sprite_index = spr_player
        }

        if (place_meeting(x,y,obj_sword)) and (keyboard_check(ord('E')) and swordobtained = false)
        {
        swordobtained = true
        abletoattack = true
        }
    • <Space>:
      • ///Sword Attack
        if abletoattack = true and !attacking
        {
        sprite_index = spr_player_swordattack
        image_index = 0
        attacking = true

        with instance_create(x,y,obj_sword_hitbox)
        {
        image_xscale = obj_player.image_xscale
        }
        }
Also. here's what I have within obj_sword:
  • Events
    • Create
      • swordobtained = false
  • Step
    • if (place_meeting(x,y,obj_player)) and (keyboard_check(ord('E')) and swordobtained = false)
      {
      swordobtained = true
      }
  • Post Draw
    • if swordobtained = true
      {
      instance_destroy();
      }
Any help would be much appreciated. Thanks!
 

Simon Gust

Member
Hello,
You could put an if-statement inside the player, asking if he has the sword.
Code:
///Sword Attack
if has_sword and abletoattack = true and !attacking 
{
   sprite_index = spr_player_swordattack
   image_index = 0
   attacking = true
   with instance_create(x,y,obj_sword_hitbox)
   {
      image_xscale = obj_player.image_xscale
   }
}
has_sword should be set to false in the create event.
When the player picks up the sword:
Code:
if (place_meeting(x,y,obj_player)) and (keyboard_check(ord('E')) and swordobtained = false)
{
   swordobtained = true
   with (obj_player)
   {
      has_sword = true;
      // this object will access the player's "has_sword" variable and set it to true so that the player is allowed to attack
   }
}
Hope that is what you're looking for.
 
S

supesdupes

Guest
Hello,
You could put an if-statement inside the player, asking if he has the sword.
Code:
///Sword Attack
if has_sword and abletoattack = true and !attacking
{
   sprite_index = spr_player_swordattack
   image_index = 0
   attacking = true
   with instance_create(x,y,obj_sword_hitbox)
   {
      image_xscale = obj_player.image_xscale
   }
}
has_sword should be set to false in the create event.
When the player picks up the sword:
Code:
if (place_meeting(x,y,obj_player)) and (keyboard_check(ord('E')) and swordobtained = false)
{
   swordobtained = true
   with (obj_player)
   {
      has_sword = true;
      // this object will access the player's "has_sword" variable and set it to true so that the player is allowed to attack
   }
}
Hope that is what you're looking for.
Thank you for replying, but it unfortunately did not work. I placed the first paragraph of code within the SPACE key event of the player, wrote has_sword = false within the create event of the player, and put the last block of code within the step event of the sword. Did i place anything in the wrong place? Or do you have any other suggestions?
 

Simon Gust

Member
Has it worked before the changes? Might be that the variables like "abletoattack" aren't true to begin with. Or that "attacking" is not false.
 
S

supesdupes

Guest
Has it worked before the changes? Might be that the variables like "abletoattack" aren't true to begin with. Or that "attacking" is not false.
No, i haven't gotten it to work at all. And I don't know what you mean about the vars. Within the player's create event, abletoattack, attacking, and has_sword are all set to be false
 

Simon Gust

Member
Well if it doesn't work in the first place I can't help that much. Try making the code simpler so you can sort out stuff that isn't working.
press-space event:
Code:
sprite_index = spr_player_swordattack
image_index = 0
attacking = true

with instance_create(x,y,obj_sword_hitbox)
{
   image_xscale = obj_player.image_xscale
}
The code is just run by pressing space, if it doesn't then that means there is something wrong with the event.
Then you add another check appart from the space-press check:
Code:
if (attacking == false)
{
   sprite_index = spr_player_swordattack
   image_index = 0
   attacking = true

   with instance_create(x,y,obj_sword_hitbox)
   {
      image_xscale = obj_player.image_xscale
   }
}
Add a check until you have what you want. That is how you always should code and even proffesionals do.
 
S

supesdupes

Guest
hey i actually figured it out on my own. i just added one line of code to your code:
Code:
if (place_meeting(x,y,obj_player)) and (keyboard_check(ord('E')) and swordobtained = false)
{
    swordobtained = true
    with (obj_player)
        {
            has_sword = true;
            abletoattack = true;     
}
it worked then, but the attack animation wouldn't stop, so i added an Animation End event to the player.
so now when i pick up the sword, i am actually able to attack; however, if you hold down space, the attack will loop (which i don't want) and the player can move while attacking but it looks really awkward because the player is stuck in the idle attack animation.
 
S

supesdupes

Guest
okay, so i changed the SPACE event to key press SPACE and it fixed the attack loop problem
 
Top