Solarus-Games English Forum

Solarus => Development => Topic started by: Porksteak on August 23, 2019, 05:46:06 AM

Title: Prevent attack command from attacking
Post by: Porksteak 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.
Title: Re: Prevent attack command from attacking
Post by: Diarandor 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?
Title: Re: Prevent attack command from attacking
Post by: Porksteak on August 26, 2019, 03:03:50 PM
Just temporarily depending on if the hero has enough stamina to attack
Title: Re: Prevent attack command from attacking
Post by: Max 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?
Title: Re: Prevent attack command from attacking
Post by: Christopho 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)
Title: Re: Prevent attack command from attacking
Post by: Mr Shiek 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:

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.
Title: Re: Prevent attack command from attacking
Post by: Cluedrew 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:
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.