Page 1 of 1

Adding additional arguments to love.keypressed()

Posted: Tue May 12, 2015 11:46 am
by Bindie
Hey, I need help on the love.keypressed().

Basically I have up to 9 players all with individual keysets:

Code: Select all

player = {
			{str = 1,
			vel = 0,
			spd = 100,
			kck = 0,
			bmb = 1,
			control = {
						"w", -- Up
						"s", -- Down
						"a", -- Left
						"d", -- Right
						"lctrl", -- Lay
						"e", -- Use
						},
			graphics = 1,
			anim = 1,
			animTime = 0,
			x = 0,
			y = 0,
			w = 63,
			h = 63,
			direction = 1,
			},

			{str = 1,
			vel = 0,
			spd = 100,
			kck = 0,
			bmb = 1,
			control = {
						"up", -- Up
						"down", -- Down
						"left", -- Left
						"right", -- Right
						"rctrl", -- Lay
						"lshift", -- Use
						},
			graphics = 1,
			anim = 1,
			animTime = 0,
			x = 0,
			y = 0,
			w = 63,
			h = 63,
			direction = 1,
			},
		}
How do I pass entire keySets into love.keypressed?

I uploaded my main.lua, it's very short and don't mind that I passed all players into love.keypressed, I was attempting to pass their keysets into the function to not have to write for example the bomb laying function for every key related to bomb-laying.

Re: Adding additional arguments to love.keypressed()

Posted: Tue May 12, 2015 12:56 pm
by BOT-Brad
That's not really how love.keypressed works. You shouldn't call it yourself, it is a function that will get called automatically by Love during run-time.

Code: Select all

function love.keypressed(k, isrepeat)
    for i, p in ipairs(players) do
        if k==p.control[5] then -- Did we press the "Lay" key?
            --trigger this players "lay" function
        end
    end
end
For the 4 movement keys, I recommend using love.keyboard.isDown.

Re: Adding additional arguments to love.keypressed()

Posted: Tue May 12, 2015 1:39 pm
by ivan
Bindie wrote:How do I pass entire keySets into love.keypressed?
I haven't used Love2D too much but I've programmed similar things so here is my suggestion.

Save your key mappings to an external file, don't include "speed", "vel", "anim" there, just save the mappings in a table like:

Code: Select all

local mappings = 
{
  moveup = "up",
  movedown = "down"
  [ ... ]
}
return mappings
Make sure the format is: "mappings[action] = key"
This way, there are no duplicate "actions" in the table.
Probably a good idea to make the file user-editable in the future.

Then you either use a callback like Bot-Brad mentioned:

Code: Select all

function love.keypressed(pressedKey, isrepeat)
    -- for each action
    for action, mappedKey in pairs(mappings) do
        if pressedKey == mappedKey then
            --perform "action"
        end
    end
end
With the approach I mentioned,
I think it's simpler to "poll" the system instead of waiting for callbacks:

Code: Select all

function processInput(mappings)
    -- for each action
    for action, mappedKey in pairs(mappings) do
        if love.keyboard.isDown( mappedKey ) then
            --perform "action"
        end
    end
end

Re: Adding additional arguments to love.keypressed()

Posted: Tue May 12, 2015 6:34 pm
by Bindie
Thanks.