Trying to change key binding

Started by Starlock, August 24, 2015, 09:33:41 PM

Previous topic - Next topic
I tried to change the key binding for when you attack after obtaining the sword from the c key to the e key but whenever I load the game it still remains on the c key.
The code i used was

function game:on_character_pressed(key)
if key == "e" then
game:get_command_keyboard_binding("attack")   
   end
  end

August 24, 2015, 09:54:59 PM #1 Last Edit: August 24, 2015, 09:58:03 PM by Diarandor
I think that is normal because to set the command binding you need to use game:set_command_keyboard_binding(command, key).
(You are using the getter function and not the setter.) It is not necessary to call the event game:on_character_pressed(key). Try to write somewhere in the game manager

game:set_command_keyboard_binding("attack", "e")

That should probably work, although I haven't tested this code.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

I tried that code and it gave me the error

Error: In on_finished: [string "scripts/game_manager.lua"]:83: bad argument #2 to set_command_keyboard_binding (string or nil expected, got string)      :-\

Try to put it in the event game:on_started(). I have a similar code for my joypad that works, like this:

function game:on_started()
  -- Prepare the dialog box menu and the HUD.
  game:initialize_dialog_box()
  hud = hud_manager:create(game)
  pause_menu = pause_manager:create(game, exists)
  -- more code that I am using... and:
  -- Configure some joypad buttons. [Change this for a menu later!]
  game:set_command_joypad_binding("action", "button 10")
  game:set_command_joypad_binding("attack", "button 11")
  game:set_command_joypad_binding("item_1", "button 12")
  game:set_command_joypad_binding("item_2", "button 13")
end
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

The game is able to run again now but the code still doesn't seem to affect the keys as it still uses the c key... I might try looking in the roth source code to see if it has anything that might make this work

What Solarus version are you using? game:set_command_keyboard_binding() was broken before Solarus 1.4.3.
You can also use game:capture_command_binding(), it might be easier for what you want.

I'm running 1.4.4 yet still it won't work. I feel like there must be some simple syntax error that I keep glossing over but nothing appears in error.txt besides that <eof> is expected near end on line 80... This is the full code with Diarandor's code example:
Code ( lua) Select
-- Script that creates a game ready to be played.

-- Usage:
-- local game_manager = require("scripts/game_manager")
-- local game = game_manager:create("savegame_file_name")
-- game:start()

local dialog_box_manager = require("scripts/dialog_box")

local game_manager = {}

-- Sets initial values for a new savegame of this quest.
local function initialize_new_savegame(game)
  game:set_starting_location("house")
  game:set_max_money(100)
  game:set_max_life(12)
  game:set_life(game:get_max_life())
end

-- Creates a game ready to be played.
function game_manager:create(file)

  -- Create the game (but do not start it).
  local exists = sol.game.exists(file)
  local game = sol.game.load(file)
  if not exists then
    -- This is a new savegame file.
    initialize_new_savegame(game)
  end

  local dialog_box

  -- Function called when the player runs this game.
function game:on_started()
  -- Prepare the dialog box menu and the HUD.
  game:initialize_dialog_box()
  hud = hud_manager:create(game)
  pause_menu = pause_manager:create(game, exists)
  -- more code that I am using... and:
  -- Configure some joypad buttons. [Change this for a menu later!]
  game:set_command_keyboard_binding("action", "space_bar")
  game:set_command_keyboard_binding("attack", "e")
  game:set_command_keyboard_binding("item_1", "q")
  game:set_command_keyboard_binding("item_2", "w")
end

    dialog_box = dialog_box_manager:create(game)
  end

  -- Function called when the game stops.
  function game:on_finished()

    dialog_box:quit()
    dialog_box = nil
  end

  function game:on_paused()

    game:start_dialog("pause.save", function(answer)
      if answer == 2 then
        game:save()
      end
      game:set_paused(false)
    end)
  end

  local rupee_icon = sol.surface.create("hud/rupee_icon.png")
  local rupee_text = sol.text_surface.create()
  rupee_text:set_font("8_bit")

  function game:on_draw(dst_surface)

    rupee_icon:draw_region(0, 0, 12, 12, dst_surface, 10, 220)
    rupee_text:set_text(game:get_money())
    rupee_text:draw(dst_surface, 25, 225)
  end


  return game
end

return game_manager



There is an extra end at line 48. Your indentation is incorrect, making it easy to get wrong with syntax.