Assigning Hotkeys to Items

Started by DementedKirby, July 30, 2015, 02:16:31 AM

Previous topic - Next topic
July 30, 2015, 02:16:31 AM Last Edit: July 30, 2015, 02:26:13 AM by DementedKirby
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.

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).
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

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.

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.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

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!