Author Topic: Strange behavior with game:on_command_pressed() and hero:start_attack()  (Read 27 times)

alexgleason

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Vegan on a Desert Island
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
  1. function game:on_command_pressed(command)
  2.   -- Disable attacking; the stick is a regular item
  3.   if command == "attack" then
  4.     return true
  5.   end
  6.   ...
  7. 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
  1. -- The hero can only swing the sword, nothing else
  2. function hero:on_state_changed(state)
  3.   if state == "sword loading"
  4.   or state == "sword tapping"
  5.   or state == "sword spin attack" then
  6.     hero:freeze()
  7.     hero:unfreeze()
  8.   end
  9. 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)