Author Topic: Item command held on item?  (Read 177 times)

Porksteak

  • Newbie
  • *
  • Posts: 7
    • View Profile
Item command held on item?
« on: June 19, 2017, 04:12:22 pm »
Hi I wanted to try and make some kind of energy blast attack that gets stronger the longer you hold it. Is there a way in the item to check if the specific item key that the item is assigned to is being held down?

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Item command held on item?
« Reply #1 on: June 19, 2017, 07:09:01 pm »
What you'll probably want to do is create a timer (or a set of timers) that count for how long you want the difference of time between the instances of charge.

So doing something like this:
Code: Lua
  1. -- At beinging of code
  2. local timer = nil
  3.  
  4. --  Code before timers
  5. local function start_timer()
  6.   timer = sol.timer.start(item, 1000, function()
  7.     --code relating to when timer reaches max time
  8.   end) -- This timer lasts 1 sec and belongs to "context" item if defined. To increase the time, change the 2nd value to desired time in milliseconds.
  9.  
  10. end
  11.  
  12. local function end_timer()
  13.   local time_remaining = timer:get_remaining_time()
  14.   --code relating to translating time into charge
  15.   --I.E. Total charge value / (time_remaining * -1)
  16.   --return charge_strength
  17. end
  18.  


See http://www.solarus-games.org/doc/latest/lua_api_timer.html for more details relating to timers.

For checking if the item is being held down, I would look to these sets of events: http://www.solarus-games.org/doc/latest/lua_api_game.html#lua_api_game_on_key_pressed
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

Porksteak

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Item command held on item?
« Reply #2 on: June 20, 2017, 05:20:58 pm »
I started the item, but I can't get the command released function to work so the hero stays frozen

This is function:on_using()
Code: Lua
  1. function item:on_using()
  2. local map = game:get_map()
  3. local hero = game:get_hero()
  4.  
  5. local direction = hero:get_direction()
  6.  
  7.  
  8.  
  9.  
  10.   if game:is_command_pressed("item_1") or game:is_command_pressed("item_2") then
  11.   state = 1
  12.       if blastlevel < 4 then
  13.     timer = sol.timer.start(2000, function()
  14.       blastlevel = blastlevel + 1
  15.     return true
  16.     end)
  17.   end
  18.   end
  19.  
  20.  
  21.  
  22.     if game:get_item_assigned(2) == "blast" and not game:is_command_pressed("item_2") and state == 1 then
  23.       game:simulate_command_released("item_2")
  24.     end
  25.  
  26.     if game:get_item_assigned(1) == "blast" and not game:is_command_pressed("item_1") and state == 1 then
  27.       game:simulate_command_released("item_1")
  28.     end
  29.  
  30.  
  31. end
  32.  
  33.  
  34. function game:on_command_released(command)
  35.   if command == "item_1" or "item_2" then
  36.     timer:stop()
  37.     hero:unfreeze()
  38.     item:fire_blast()
  39.   end
  40. end
  41.  

MetalZelda

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
Re: Item command held on item?
« Reply #3 on: June 21, 2017, 12:34:22 pm »
To make the hero controllable just call item:set_fiinished()

But, you might want to control the hero and having the item to work. I would link a menu to this item and control everrything from this menu, you will have access to on_command_pressed / released.

That's what I do for my bow, hookshot and other stuffs

Diarandor

  • Hero Member
  • *****
  • Posts: 746
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Item command held on item?
« Reply #4 on: June 21, 2017, 04:48:59 pm »
To make the hero controllable just call item:set_fiinished()

But, you might want to control the hero and having the item to work.

You can directly unfreeze the hero (which allows to control him) before calling item:set_finished(), which is probably what he needs. This is what I use for my "custom jump", which allows to control the hero during the jump. But don't forget to call item:set_finished() when you finish using the item! :)

Porksteak

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Item command held on item?
« Reply #5 on: June 28, 2017, 04:25:46 am »
Yeah, but the problem is the unfreeze() is in the on_command_released function, but when the key is released nothing happens and the hero stays frozen

Diarandor

  • Hero Member
  • *****
  • Posts: 746
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Item command held on item?
« Reply #6 on: June 28, 2017, 09:36:51 am »
I started the item, but I can't get the command released function to work so the hero stays frozen

This is function:on_using()
Code: Lua
  1. function item:on_using()
  2. local map = game:get_map()
  3. local hero = game:get_hero()
  4.  
  5. local direction = hero:get_direction()
  6.  
  7.  
  8.  
  9.  
  10.   if game:is_command_pressed("item_1") or game:is_command_pressed("item_2") then
  11.   state = 1
  12.       if blastlevel < 4 then
  13.     timer = sol.timer.start(2000, function()
  14.       blastlevel = blastlevel + 1
  15.     return true
  16.     end)
  17.   end
  18.   end
  19.  
  20.  
  21.  
  22.     if game:get_item_assigned(2) == "blast" and not game:is_command_pressed("item_2") and state == 1 then
  23.       game:simulate_command_released("item_2")
  24.     end
  25.  
  26.     if game:get_item_assigned(1) == "blast" and not game:is_command_pressed("item_1") and state == 1 then
  27.       game:simulate_command_released("item_1")
  28.     end
  29.  
  30.  
  31. end
  32.  
  33.  
  34. function game:on_command_released(command)
  35.   if command == "item_1" or "item_2" then
  36.     timer:stop()
  37.     hero:unfreeze()
  38.     item:fire_blast()
  39.   end
  40. end
  41.  

Are you aware of the fact that defining "game" events inside an item script is a bad idea and that one event definition will override other previous definitions of that event in other scripts? I am talking about game.on_command_released.