Solarus-Games English Forum

Solarus => Development => Topic started by: DementedKirby on July 28, 2015, 07:15:32 PM

Title: Nothing happens with this code yet I get no error (key input)
Post by: DementedKirby on July 28, 2015, 07:15:32 PM
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?
Title: Re: Nothing happens with this code yet I get no error (key input)
Post by: Diarandor 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.
Title: Re: Nothing happens with this code yet I get no error (key input)
Post by: DementedKirby 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?
Title: Re: Nothing happens with this code yet I get no error (key input)
Post by: DementedKirby 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):

-- 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...
Title: Re: Nothing happens with this code yet I get no error (key input)
Post by: Diarandor on July 28, 2015, 10:28:50 PM
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.
Title: Re: Nothing happens with this code yet I get no error (key input)
Post by: DementedKirby on July 28, 2015, 10:57:02 PM

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