Question

I have a function that allows me to add and remove character in a line by i want to limit it to around 10 characters

function love.keypressed(key, unicode)
    if key == "backspace" or key == "delete" then
        player = string.sub(player, 1, #player-1)
    elseif unicode > 31 and unicode < 127 then
        player = player .. string.char(unicode)
    end
end
Was it helpful?

Solution

Could you not just restrict the length by not adding to the string if it's too long? Or were you after something else?

function love.keypressed(key, unicode)
    if key == "backspace" or key == "delete" then
        player = string.sub(player, 1, #player-1)
    elseif unicode > 31 and unicode < 127 and #player <=10 then
        player = player .. string.char(unicode)
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top