can we extend on_command_pressed(command) ?

Started by MetalZelda, January 16, 2017, 02:04:09 PM

Previous topic - Next topic
January 16, 2017, 02:04:09 PM Last Edit: January 16, 2017, 02:05:42 PM by MetalZelda
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) Select

-- Group all key bindings, build in and custom. This is used in the Option submenu, File selection screen
-- This also get the savegame values of bindings.
game.command= {
--  Command           Keyboard           Joypad
  {"right"  , "_keyboard_right" , "_joypad_right"},
  {"up"     , "_keyboard_up"    , "_joypad_up_key"},
  {"left"   , "_keyboard_left"  , "_joypad_left_key"},
  {"down"   , "_keyboard_down"  , "_joypad_down_key"},

  {"item_1" , "_keyboard_item_1", "_joypad_item_1"},
  {"item_2" , "_keyboard_item_2", "_joypad_item_2"},
   
  {"attack" , "_keyboard_attack", "_joypad_attack"},
  {"action" , "_keyboard_action", "_joypad_action"},
  {"pause"  , "_keyboard_pause" , "_joypad_pause"},

  {"shield" , "keyboard_shield" , "joypad_shield"},
  {"minimap", "keyboard_minimap", "joypad_minimap"},
  {"camera" , "keyboard_camera" , "joypad_camera"},
  {"items"  , "keyboard_items"  , "joypad_items"}
}


Something like this for a command

Code (lua) Select

  function menu:on_command_pressed(command)
    if command == "minimap" then
      -- Run the code
    end
  end


Is more efficient than this

Code (lua) Select

  function menu:on_key_pressed(key)
    if key== game:get_value("keyboard_minimap") then
      -- Run the code
    end
  end

function menu:on_joypad_hat_moved(hat, state)
    C/C
  end

function menu:on_joypad_button_pressed(key)
    C/P
  end

function menu:on_joypad_axis_moved(axis, state)
   C/P
  end

The best you can do is to call on_command_pressed("minimap") yourself. The engine won't call it with non built-in values.