Author Topic: Trying to get where the item is ...  (Read 1325 times)

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Trying to get where the item is ...
« on: October 21, 2015, 07:06:02 pm »
Issue tracking bellow

Hi

I'm quite stuck with a problem with items, I'm currently working on a new bow mechanism. I'm stuck with a problem.
I'm trying to get where the item is assigned (slot 1 or 2) by getting it's keyboard key (I'm using on_key_pressed, on_command_pressed freeze the hero  ???

Code: (lua) [Select]
function game:on_key_released(key)
local map = game:get_map()
local hero = map:get_hero()

local bow_state = game:get_value("bow_state")
local can_fire = game:get_value("can_shoot")

if key == "x" and bow_state == 1 and item:get_amount() == 0 and can_fire == true and not game:is_suspended() then
 hero:set_tunic_sprite_id("hero/item/bow_shoot_tunic1")
 sol.audio.play_sound("/items/bow/no_arrows_shoot")
 hero:freeze()
-- can't shoot arrows, but reset to state 1
sol.timer.start(100, function()
    game:set_value("bow_state", 1)
game:set_value("can_shoot", false)
hero:set_tunic_sprite_id("hero/item/bow_moving_free_tunic1")
hero:unfreeze()
game:set_pause_allowed(true)
end)
elseif key == "x" and bow_state == 1 and item:get_amount() > 0 and can_fire == true and not game:is_suspended() then
 hero:set_tunic_sprite_id("hero/item/bow_shoot_tunic1")
shoot_arrow()
hero:freeze()
sol.timer.start(100, function()
game:set_value("bow_state", 1)
game:set_value("can_shoot", false)
hero:unfreeze()
hero:set_walking_speed(40)
hero:set_tunic_sprite_id("hero/item/bow_moving_free_tunic1")
game:set_pause_allowed(true)
end)
end
end

I'm trying to get the corresponding key where the item is assigned to replace the gross if key == "x" but it's kinda messy, Christopho suggested to me this code where the game retrieve info from the item but it's a mess too. I already tried a condition where the key value = x or v depending on the i value, but only x works, v doesn't, and if i print the value, the number I got is 1, even for the item in slot 2

Code: (lua) [Select]
function game:get_item_slot(item)
for i = 1, 2 do
  if game:get_item_assigned(i) == item then
  return i
end
return nil
« Last Edit: October 23, 2015, 10:52:19 pm by Username »

froggy77

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Trying to get where the item is ...
« Reply #1 on: October 26, 2015, 12:00:30 pm »
You forgot an "end" in your function.

Otherwise, I would test with something like that:
Code: [Select]
if game:get_value("_item_slot_1") == "bow" or game:get_value("_item_slot_2") == "bow" then