Adding additional arguments to love.keypressed()

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Adding additional arguments to love.keypressed()

Post 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.
Attachments
main.lua
(2.12 KiB) Downloaded 156 times
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

Re: Adding additional arguments to love.keypressed()

Post 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.
Follow me on GitHub! | Send me a friend request on PSN!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Adding additional arguments to love.keypressed()

Post 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
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Adding additional arguments to love.keypressed()

Post by Bindie »

Thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests