Author Topic: Nothing happens with this code yet I get no error (key input)  (Read 1899 times)

DementedKirby

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
So I'm testing a custom screen and button configuration. So I'm literally stripping everything down to basics to see what I can accomplished. So far even the first step is proving to be quite the hurdle.

Code: [Select]
local map = ...

function map:on_started()
   if sol.input.is_key_pressed("q") then
      sol.audio.play_sound("bush")
   end
end

What I want to do with this (for the moment) is just to test if I can get an effect when pressing a key. I run the game, nothing happens when I press the "q" key on the keyboard yet I get no error.txt. So I know that at least the syntax is correct. I'm reading the documentation (the input) and sol.input.is_key_pressed("key") returns a boolean value so this should be working. I understand that Solarus automatically detects whether a key is being pressed so I don't have to go through all the work of coding the keyboard detection each time. So, my hypothesis was that this should've been enough. Apparently it isn't. What's wrong?

Diarandor

  • Hero Member
  • *****
  • Posts: 1045
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Nothing happens with this code yet I get no error (key input)
« Reply #1 on: July 28, 2015, 08:20:36 pm »
The problem is that the event map:on_started() is called once (when the map starts, obviously ;D).
You should use a different event, like game:on_command_pressed() or game:on_key_pressed(), which are called whenever you press a game command or key, respectively.
“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: Nothing happens with this code yet I get no error (key input)
« Reply #2 on: July 28, 2015, 08:50:29 pm »
And that would be in which .lua? The game_manager.lua? Where exactly then do I use the corresponding code?

EDIT:
Okay, maybe I was too ambiguous:
I was using that code in a map.lua. If I want it to be global for the whole game, where/how would I go about doing it?

DementedKirby

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Nothing happens with this code yet I get no error (key input)
« Reply #3 on: July 28, 2015, 09:28:10 pm »
Okay, so I put this inside of game_manager.lua (after where Christopho showed in the youtube tutorial on rupees):
Code: [Select]
-- Keyboard testing
  function keyboard()
   if game:on_key_pressed("q") then
      sol.audio.play_sound("bush")
   end
  end

I get no error.txt but again, I can't hear the sound I'm using to prove that the key is doing something when pressed. Again, I have no clue what to do...

Diarandor

  • Hero Member
  • *****
  • Posts: 1045
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Nothing happens with this code yet I get no error (key input)
« Reply #4 on: July 28, 2015, 10:28:50 pm »
You should put something like this (inside the game manager is usually the best place):
Code: [Select]
function game:on_command_pressed(command)
 if command == "action" and blablablablabla then
  blablablablabla
 elseif
   ...
 end
end
This function is an event that is called automatically by the engine when necessary and in case it is defined.  (In your script, the function keyboard() is never called, just defined.) I recommend you to study scripts of other people and also the Lua APi.
« Last Edit: July 28, 2015, 10:31:35 pm by Diarandor »
“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: Nothing happens with this code yet I get no error (key input)
« Reply #5 on: July 28, 2015, 10:57:02 pm »
Code: [Select]
  -- Keyboard testing
  function game:on_character_pressed(key)
   if key == "q" then
      sol.audio.play_sound("bush")
   end
  end

Thanks! Now I got it! With this I'm ever so closer to making my own menu system.