Solarus-Games English Forum

Solarus => Development => Topic started by: alexgleason on October 12, 2018, 02:26:23 AM

Title: Strange behavior with game:on_command_pressed() and hero:start_attack()
Post by: alexgleason on October 12, 2018, 02:26:23 AM
Not sure if this is a bug or user error, but I wanted to share.

My goal is to disable the "attack" command completely and use the sword as a standard item (with hero:start_attack()). In main.lua:

Code ( lua) Select

function game:on_command_pressed(command)
  -- Disable attacking; the stick is a regular item
  if command == "attack" then
    return true
  end
  ...
end


However, when I swing the sword, I can actually still press and hold the attack button while the sword is mid-animation. This causes sword-loading to trigger. I'm baffled by the fact I can do this when I've overridden the attack command to "return true" whenever it's pressed. ???

I ended up adding this to scripts/meta/hero.lua to solve my issue:

Code ( lua) Select
-- The hero can only swing the sword, nothing else
function hero:on_state_changed(state)
  if state == "sword loading"
  or state == "sword tapping"
  or state == "sword spin attack" then
    hero:freeze()
    hero:unfreeze()
  end
end


Freezing and unfreezing the hero when going into the sword loading state effectively disables that state, which is what I want. (In my game the sword is limited)