Author Topic: [Solved]Disable spin Attack?  (Read 395 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
[Solved]Disable spin Attack?
« on: April 12, 2017, 12:16:54 am »
is it possible to disable the spin attack?
« Last Edit: April 27, 2017, 07:05:54 pm by zutokaza »

MetalZelda

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
Re: Disable spin Attack?
« Reply #1 on: April 15, 2017, 02:08:38 pm »
You can, by overwriting the spin attack loading state in hero:on_state_changed

just unfreeze the hero and that would work

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: Disable spin Attack?
« Reply #2 on: April 16, 2017, 07:22:12 am »
Could I get an example?

I tried this, but is fails...
Code: Lua
  1. function hero:on_state_changed("sword spin attack")
  2.   hero:unfreeze()
  3. end

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: Disable spin Attack?
« Reply #3 on: April 16, 2017, 10:18:40 am »
I got it somewhat working now, but it does not disable anything. It just unfreezes the player after a spin attack.

Code: Lua
  1. function hero:on_state_changed(state)
  2.  
  3.   if state == "sword spin attack" then
  4.     hero:unfreeze()
  5.   end
  6. end

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Disable spin Attack?
« Reply #4 on: April 27, 2017, 06:53:58 pm »
In game_manager.lua.

You will need to require Christopho's multi_events script.

Code: Lua
  1. require("scripts/multi_events")

That way you register the script.

Code: Lua
  1. game:register_event("on_started", function()
  2.  
  3. end

Add the following inside the register event. Basically, it is a timer called on_activated and I use MetalZelda's clever hack. It works for me.

Code: Lua
  1. game:register_event("on_started", function()
  2. --Create on_activated
  3. sol.timer.start(100, function()
  4.   if game.on_activated ~= nil then
  5.     game:on_activated()
  6.  return true
  7.   end
  8. end)
  9.  
  10. --timer function
  11. function game:on_activated()
  12. --MetalZelda's clever hack
  13.   if game:get_hero():get_state() == "sword loading" then
  14.    if not game:get_value("skill_spin_attack") then
  15.    game:simulate_command_released("attack")
  16.    end
  17.  end
  18. end
  19.  
  20.   end) -- end of game:register_event
« Last Edit: April 27, 2017, 07:31:57 pm by Zefk »

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
[Solved]Disable spin Attack?
« Reply #5 on: April 27, 2017, 07:05:28 pm »
That works perfectly. I was able to activate it with set_value. Thanks again zefk!

MetalZelda

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
Re: [Solved]Disable spin Attack?
« Reply #6 on: April 28, 2017, 01:11:52 am »
Just rewrite hero:on_state_changed.
To disable the spin attack, the state we need to unfreeze the hero is called "sword loading"
You can still access game function through.
This way, it is more convenient since you rewrite the function it belongs to

Code: Lua
  1. hero:register_event("on_state_changed", function(hero, state)
  2.   if state == "sword loading" then
  3.     local game = hero:get_game()
  4.     if not game:get_value("skill_spin_attack") then
  5.       game:simulate_command_released("attack")
  6.     end
  7.   end
  8. end
« Last Edit: April 28, 2017, 01:17:24 am by MetalZelda »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: [Solved]Disable spin Attack?
« Reply #7 on: April 28, 2017, 01:50:31 am »
@MetalZelda
I am not sure why, but I get an error.
Code: [Select]
[2770] Error: In on_started: [string "scripts/game_manager.lua"]:17: attempt to index global 'hero' (a nil value)
This is basically identical to your code and works for me. I thank you for the tip. This actually functions better than my suggestion.

Code: Lua
  1. game:register_event("on_started", function()
  2.  
  3.   local hero = game:get_hero()
  4.  
  5.   hero:register_event("on_state_changed", function(hero, state)
  6.     if state == "sword loading" then
  7.       if not game:get_value("skill_spin_attack") then
  8.         game:simulate_command_released("attack")
  9.       end
  10.     end
  11.   end) -- end of hero:register
  12. end) -- end of game:register
  13.  

MetalZelda

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
Re: [Solved]Disable spin Attack?
« Reply #8 on: April 28, 2017, 12:26:56 pm »
You can do the following.

using metatable.
http://www.solarus-games.org/doc/latest/lua_api_main.html#lua_api_main_get_metatable


You need
1 file, called hero.lua, if you like organisation, put it in a new folder called "meta"

In main.lua, in the begining, require this file
Code: Lua
  1. require("meta/hero")

in hero.lua
Code: Lua
  1. local hero_meta = sol.main.get_metatable("hero")
  2.  
  3. function hero_meta:on_state_changed(state)
  4.   if state == "sword loading" then
  5.     local game = self:get_game()
  6.     if not game:get_value("skill_spin_attack") then
  7.       game:simulate_command_released("attack")
  8.     end
  9.   end
  10. end

Your solution works as well, but this one is more efficient, the new state is already defined when the game starts, not applied when the game starts
« Last Edit: April 28, 2017, 12:29:53 pm by MetalZelda »