Prevent attack command from attacking

Started by Porksteak, August 23, 2019, 05:46:06 AM

Previous topic - Next topic
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.

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."

Just temporarily depending on if the hero has enough stamina to attack

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?

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)

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:

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.
Other Handles: Kamigousu, Xejk, Mr Sheik

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:
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.