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

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • 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) [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
« Last Edit: January 16, 2017, 02:05:42 pm by MetalZelda »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1172
    • 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

  • Hero Member
  • *****
  • Posts: 551
    • 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