Author Topic: Prevent attack command from attacking  (Read 1400 times)

Porksteak

  • Newbie
  • *
  • Posts: 15
    • View Profile
Prevent attack command from attacking
« on: August 23, 2019, 05:46:06 am »
Is there to make it so the attack command being pressed stops the hero from attacking? I've tried using on_command_pressed(), on_state_changed() and on_stage_changing() but nothing seems to be working for me.

Diarandor

  • Hero Member
  • *****
  • Posts: 1053
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Prevent attack command from attacking
« Reply #1 on: August 26, 2019, 03:37:30 am »
To give an adequate answer for your purposes we need to know this: do you want to always disable the sword attack in all your game? Or just temporarily under certain states/conditions?
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Porksteak

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Prevent attack command from attacking
« Reply #2 on: August 26, 2019, 03:03:50 pm »
Just temporarily depending on if the hero has enough stamina to attack

Max

  • Full Member
  • ***
  • Posts: 223
    • View Profile
Re: Prevent attack command from attacking
« Reply #3 on: August 26, 2019, 07:54:05 pm »
Can you post the things you tried? Like the actual code, you said you used on_state_changed() but like, HOW did you use it?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Prevent attack command from attacking
« Reply #4 on: August 26, 2019, 09:40:05 pm »
Two solutions:
- Associate the attack command to no keyboard key and no joypad action: game:set_command_keyboard_binding("attack", nil)
Or
- Remove the attack ability: game:set_ability("sword", nil)

Kamigousu

  • Newbie
  • *
  • Posts: 27
  • Game Design 'Hobbyist'...for now.
    • View Profile
Re: Prevent attack command from attacking
« Reply #5 on: August 27, 2019, 01:15:49 am »
If you are using stamina to determine if the hero can attack, you may want to use a stamina check in your attack command.

So if you had a function like this:
Code: [Select]
function game:on_command_pressed(command)
  if command == "attack" and stamina < 0 then
    game:simulate_command_released("attack")
  end
end

This is just an example but it is similar to what I am using in my own game. Of course, this is assuming you have all ready set up stamina in your game so you can use methods like game:get_stamina() or game:set_stamina(). Without that, you cant really check to see if the hero can attack or not.

Generally, if I need to stop a command press under certain circumstances, I use logic gates in game:on_command_pressed() to determine if the command should be allowed or not. I'm sure the other ways mentioned work just as well, but I have never tried them.
Build a man a fire, he will be warm for the night. Set a man on fire, he will be warm for the rest of his life.

Cluedrew

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Prevent attack command from attacking
« Reply #6 on: September 17, 2019, 12:46:47 am »
I haven't tried this but from the documentation and my memories from the one time a looked over this part of the engine you should be able to do something like Kamigousu's solution but say you have handled the input to prevent the action from happening:
Code: [Select]
function game:on_command_pressed(command)
    if "attack" == command and stamina < 0 then
        return true
    end
    -- Anything else you want to do.
end

Also returning false doesn't do anything so you can put the condition of the if statement as the return value if you are doing nothing else in the event.