Solarus > Development

Prevent attack command from attacking

<< < (2/2)

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: ---function game:on_command_pressed(command)
  if command == "attack" and stamina < 0 then

--- End code ---

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.

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: ---function game:on_command_pressed(command)
    if "attack" == command and stamina < 0 then
        return true
    -- Anything else you want to do.
--- End code ---

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.


[0] Message Index

[*] Previous page

Go to full version