Author Topic: can we extend on_command_pressed(command) ?  (Read 171 times)

MetalZelda

  • Sr. Member
  • ****
  • Posts: 454
    • View Profile
can we extend on_command_pressed(command) ?
« on: January 16, 2017, 02:04:09 pm »
Hello,

My question concerns on_command_pressed(command) and custom inputs.

The advantage with on_command_pressed is that it englobe both keyboard and joypad bindings with ease, so it is not necessary to have in each menu that need to check a custom input such functions (on_joypad_*_pressed/moved), the command customisation menu is an exception.

Is it possible to do a such thing ? for example, let's say that I implemented a new input "minimap" and I wanna add it as a build-in command for on_command_pressed(command)

Want an example, well

This is the command table of my game

Code: Lua
  1. -- Group all key bindings, build in and custom. This is used in the Option submenu, File selection screen
  2. -- This also get the savegame values of bindings.
  3. game.command= {
  4. --  Command           Keyboard           Joypad
  5.   {"right"  , "_keyboard_right" , "_joypad_right"},
  6.   {"up"     , "_keyboard_up"    , "_joypad_up_key"},
  7.   {"left"   , "_keyboard_left"  , "_joypad_left_key"},
  8.   {"down"   , "_keyboard_down"  , "_joypad_down_key"},
  9.        
  10.   {"item_1" , "_keyboard_item_1", "_joypad_item_1"},
  11.   {"item_2" , "_keyboard_item_2", "_joypad_item_2"},
  12.    
  13.   {"attack" , "_keyboard_attack", "_joypad_attack"},
  14.   {"action" , "_keyboard_action", "_joypad_action"},
  15.   {"pause"  , "_keyboard_pause" , "_joypad_pause"},
  16.        
  17.   {"shield" , "keyboard_shield" , "joypad_shield"},
  18.   {"minimap", "keyboard_minimap", "joypad_minimap"},
  19.   {"camera" , "keyboard_camera" , "joypad_camera"},
  20.   {"items"  , "keyboard_items"  , "joypad_items"}
  21. }

Something like this for a command

Code: Lua
  1.   function menu:on_command_pressed(command)
  2.     if command == "minimap" then
  3.       -- Run the code
  4.     end
  5.   end
  6.  

Is more efficient than this

Code: Lua
  1.   function menu:on_key_pressed(key)
  2.     if key== game:get_value("keyboard_minimap") then
  3.       -- Run the code
  4.     end
  5.   end
  6.  
  7.  function menu:on_joypad_hat_moved(hat, state)
  8.     C/C
  9.   end
  10.  
  11.  function menu:on_joypad_button_pressed(key)
  12.     C/P
  13.   end
  14.  
  15.  function menu:on_joypad_axis_moved(axis, state)
  16.    C/P
  17.   end
  18.  
« Last Edit: January 16, 2017, 02:05:42 pm by MetalZelda »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1001
    • View Profile
Re: can we extend on_command_pressed(command) ?
« Reply #1 on: January 16, 2017, 02:12:30 pm »
The best you can do is to call on_command_pressed("minimap") yourself. The engine won't call it with non built-in values.

MetalZelda

  • Sr. Member
  • ****
  • Posts: 454
    • View Profile
Re: can we extend on_command_pressed(command) ?
« Reply #2 on: January 17, 2017, 12:12:59 pm »
Oh that's clever, didn't though about it

Thanks