Solarus-Games English Forum

Solarus => Development => Topic started by: DementedKirby on July 30, 2015, 02:16:31 AM

Title: Assigning Hotkeys to Items
Post by: DementedKirby on July 30, 2015, 02:16:31 AM
Okay, so I'm testing and I want to be able to quickly go to items - more specifically, hit a key on the keyboard and the same effect as using the item occurs - regardless if the hero has the item or not. I've got the key to work. What I'm doing is using

-- Keyboard testing
  function game:on_character_pressed(key)
   if key == "q" then
      sol.main.load_file("book")
      sol.main.do_file("book")
   end
  end

inside of quest_manager.lua. The book.lua it's opening is just this:

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

item:get_map():get_entity("hero"):set_animation("book")
game:start_dialog("book.test", nil, function()
item:set_finished()
end)

and I get the following error:
QuoteError: In book: [string "book.lua"]:2: attempt to index local 'item' (a nil value)
This is an error I keep getting constantly and I really have no idea why. Apart from this error everything else seems to be working. I just gotta overcome this obstacle.
Title: Re: Assigning Hotkeys to Items
Post by: Diarandor on July 30, 2015, 04:28:59 AM
I think you cannot get an item directly from its script, only the engine can do that, so probably this has no sense. Maybe, try using game:get_item(item_name).
Title: Re: Assigning Hotkeys to Items
Post by: DementedKirby on July 30, 2015, 05:09:26 AM
But then the variable game would have to be declared somewhere, no? Oh, and the book.lua I was referring to isn't the item script; it's a lua I made in the same directory so that sol.main.load_file("book") and sol.main.do_file("book") would access them. The accessing part isn't the problem. It happens just fine. It's what happens when they lua file is called that I get the error. I know the lua is called and accessed because the error comes from book.lua, which means it's being read. I will check your suggestion in the mean time to see if maybe that's the problem.
Title: Re: Assigning Hotkeys to Items
Post by: Diarandor on July 30, 2015, 05:41:26 AM
Yes, the script is loaded and executed. But as far as I know, you are not getting a solarus item entity that the engine can handle. That is why the function item:get_game() is not defined for your item variable (which is actually nil), and that produces the error in line 2.
Title: Re: Assigning Hotkeys to Items
Post by: DementedKirby on July 30, 2015, 05:47:23 AM
Ah, I see. Well, I guess I'm just gonna have to do this with map scripts then in the mean time. Thanks for clearing up what limitations I could have with this!