Author Topic: Assigning Hotkeys to Items  (Read 1763 times)

DementedKirby

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Assigning Hotkeys to Items
« 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
Code: [Select]
-- 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:
Code: [Select]
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:
Quote
Error: 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.
« Last Edit: July 30, 2015, 02:26:13 am by DementedKirby »

Diarandor

  • Hero Member
  • *****
  • Posts: 1053
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Assigning Hotkeys to Items
« Reply #1 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).
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

DementedKirby

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Assigning Hotkeys to Items
« Reply #2 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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1053
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Assigning Hotkeys to Items
« Reply #3 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.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

DementedKirby

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Assigning Hotkeys to Items
« Reply #4 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!