Nothing happens with this code yet I get no error (key input)

Started by DementedKirby, July 28, 2015, 07:15:32 PM

Previous topic - Next topic
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.


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?

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."

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?

Okay, so I put this inside of game_manager.lua (after where Christopho showed in the youtube tutorial on rupees):

-- 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...

You should put something like this (inside the game manager is usually the best place):

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


  -- 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.