Difference between revisions of "love.joystick.getGamepadMappingString"

(Created page)
 
(Example: print the MRU gamepad name)
 
Line 12: Line 12:
 
=== Returns ===
 
=== Returns ===
 
{{param|string|mappingstring (nil)|A string containing the Joystick's gamepad mappings, or nil if the GUID is not recognized as a gamepad.}}
 
{{param|string|mappingstring (nil)|A string containing the Joystick's gamepad mappings, or nil if the GUID is not recognized as a gamepad.}}
 +
 +
== Example: print the MRU gamepad name ==
 +
 +
Track the most recently used gamepad and print its name when it changes.
 +
 +
<source lang="lua">
 +
local active_joystick
 +
function love.gamepadpressed(joystick, button)
 +
    if joystick == active_joystick then
 +
        return
 +
    end
 +
    active_joystick = joystick
 +
    -- Pull out the second column from the csv string.
 +
    local name = joystick:getGamepadMappingString():match("^%x*,(.-),")
 +
    if not name or name:len() < 3 then
 +
        -- Names like "*" are not useful, so fall back to device name.
 +
        name = joystick:getName()
 +
    end
 +
    local index = joystick:getConnectedIndex()
 +
    print(string.format("Changing primary gamepad to #%d '%s'.", index, name))
 +
end
 +
</source>
 +
 +
 +
  
 
== See Also ==
 
== See Also ==

Latest revision as of 01:07, 31 December 2021

Available since LÖVE 11.3
This function is not supported in earlier versions.

Gets the full gamepad mapping string of the Joysticks which have the given GUID, or nil if the GUID isn't recognized as a gamepad.

The mapping string contains binding information used to map the Joystick's buttons an axes to the standard gamepad layout, and can be used later with love.joystick.loadGamepadMappings.

Function

Synopsis

mappingstring = love.joystick.getGamepadMappingString( guid )

Arguments

string guid
The GUID value to get the mapping string for.

Returns

string mappingstring (nil)
A string containing the Joystick's gamepad mappings, or nil if the GUID is not recognized as a gamepad.

Example: print the MRU gamepad name

Track the most recently used gamepad and print its name when it changes.

local active_joystick
function love.gamepadpressed(joystick, button)
    if joystick == active_joystick then
        return
    end
    active_joystick = joystick
    -- Pull out the second column from the csv string.
    local name = joystick:getGamepadMappingString():match("^%x*,(.-),")
    if not name or name:len() < 3 then
        -- Names like "*" are not useful, so fall back to device name.
        name = joystick:getName()
    end
    local index = joystick:getConnectedIndex()
    print(string.format("Changing primary gamepad to #%d '%s'.", index, name))
end



See Also

Other Languages