[Solved] hero:start_attack() not working (when called from an item script)

Started by alexgleason, October 11, 2018, 08:29:09 PM

Previous topic - Next topic
Hi, I was hoping to create an item that triggers the hero's normal attack on_using(). It's simple:

Code ( lua) Select
local item = ...
local game = item:get_game()

function item:on_created()
  self:set_savegame_variable("stick")
  self:set_assignable(true)
end

function item:on_using()
  print("beep")
  game:get_hero():start_attack() -- Normal sword attack
end


When I equip the item and press the button, the console outputs "beep", and then the player becomes frozen. The sword is not swung. From the console, I can unfreeze the player with: sol.main.game:get_hero():unfreeze().

Any clue why start_attack() isn't working here? I can call sol.main.game:get_hero():start_attack() from the console at any time and it works, but the same thing doesn't work inside this item script. Thanks!
RIP Aaron Swartz

I guess it is because the attack is not allowed during the item state. Try to call item:set_finished() just before hero:start_attack().

Quote from: Christopho on October 11, 2018, 09:04:03 PM
I guess it is because the attack is not allowed during the item state. Try to call item:set_finished() just before hero:start_attack().

Ohh, that makes sense! Calling item:set_finished() lets the function continue executing, but strangely the sword is still never swung. This is my code now.

Code ( lua) Select
local item = ...
local game = item:get_game()

function item:on_created()
  self:set_savegame_variable("stick")
  self:set_assignable(true)
end

function item:on_using()
  print("beep")
  item:set_finished()
  print("boop")
  game:get_hero():start_attack()
  print("blip")
end


Pressing the item button does indeed print:

beep
boop
blip


But the sword still doesn't swing. The player doesn't freeze, either. Nothing happens except printing to the console. ???
RIP Aaron Swartz

Wait, I got it! Just had to add hero:unfreeze(). This code works:

Code ( lua) Select
-- ♡ Copying is an act of love. Please copy and share.

-- Script that lets you use the sword as a normal item.
-- Note that the sword button cannot be held with this method (for charging or tapping)

local item = ...
local game = item:get_game()

function item:on_created()
  self:set_savegame_variable("stick")
  self:set_assignable(true)
end

function item:on_using()
  local hero = game:get_hero()
  hero:unfreeze()
  hero:start_attack()
  item:set_finished()
end


Thanks Christopho!

EDIT 1:

Note that this approach has some downsides:

1. The item button cannot be held to hold the sword for longer. You'll need to script that if you want it. I did achieve that here with a different type of item.
2. It doesn't seem you can tap the item button to swing the sword very fast. The animation must totally complete before you can press the item button again.

EDIT 2:

Using the held item command approach improves the situation:

Code ( lua) Select
-- ♡ Copying is an act of love. Please copy and share.

-- Script that lets you use the sword as a normal item.
-- Note that the sword button cannot be held with this method (for charging or tapping)

local item = ...
local game = item:get_game()

function item:on_created()
  self:set_savegame_variable("stick")
  self:set_assignable(true)
end

function item:on_command_pressed(command)
  local hero = game:get_hero()
  hero:start_attack()
  item:set_finished()
end


This enables me to tap the sword button very quickly and I don't have to unfreeze the hero first. It still does not let me press and hold the sword button, which doesn't seem possible with the current API. Fortunately this is a feature I'm not using in my game and have disabled anyway.
RIP Aaron Swartz

I've actually been planning on trying to figure out how to force the sword to complete its animation before the next attack, so your downside is my upside, haha. I think it can make an attack feel much weightier, and forces strategy if you lock the player into the animation.

Quote from: Max on October 12, 2018, 12:07:12 AM
so your downside is my upside, haha.

Aaaannndd this is how a bug becomes a feature. ;D
RIP Aaron Swartz