[LIBRARY] tt.lua - Text triggers for lua!

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

[LIBRARY] tt.lua - Text triggers for lua!

Post by veethree »

I'm sure there is a more proper name for what this does, I just don't know what it is, Or how to look it up. So i'm just calling it text triggers. This library helps you call a certain function when a certain sequence of keys is pressed. Just like old video game cheat codes.

Proper documentation on github, Bad documentation below.

Load

Code: Select all

tt = require "tt"
Update buffer

Code: Select all

function love.keypressed(key)
    tt:updateBuffer(key)
end
tt:updateBuffer() can also go in textinput or mousepressed. Or wheverever you want, Just pass it something that makes sense.

Update internal timer

Code: Select all

function love.update(dt)
    tt:update(dt)
end
This is optional, But required if you want to take advantage of the 'buffer timeout' and 'wait for timeout' features.

Define some triggers

Code: Select all

tt:new("escape", love.event.push, "quit")
tt:new("hello", print, "Hello world!")
tt:new("upupdowndownleftrightleftrightba", something_neat)
Now when you type "hello" or do the konami code, Something might happen if you've set it up right.

Demo: It's just a single file so i'll just drop it here

Code: Select all

function love.load()
    -- Loading and configure tt
    tt = require "tt"
    tt:setWaitForTimeout(true)
    tt:setBufferTimeout(0.5)

    -- Defining some triggers
    tt:new("escape", love.event.push, "quit")
    tt:new("hello", print, "Hello world!")
    tt:new("mouse1mouse1", print, "Double click")
end

function love.update(dt)
    -- Updating the internal timer
    tt:update(dt)
end

function love.draw()
    -- Printing out the buffer
    love.graphics.print("Current buffer: "..tt:getBuffer(), 12, 12)
end

function love.keypressed(key)
    -- Updating the buffer
    tt:updateBuffer(key)
end

function love.textinput(t)
    -- In löve, You can also use 'textinput' to update the buffer.
    -- That way you can have triggers with capital letters, but a trigger like "escape"
    -- wont be triggered when the escape key is pressed. Only when you type it out.
    -- tt:updateBuffer(t)
end

function love.mousepressed(x, y, b)
    -- You could also update the buffer in mousepressed, For example to detect a double click
    -- Here i send the buffer a string like "mouse1" instead of just passing it "b" directly
    -- This is so the "double click" trigger doesnt also trigger if you just type "11" 
    local char = "mouse"..b
    tt:updateBuffer(char)
end
Attachments
tt.lua
v1.1
(3.93 KiB) Downloaded 182 times
Last edited by veethree on Tue Oct 12, 2021 10:50 pm, edited 1 time in total.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: [LIBRARY] tt.lua - Text triggers for lua!

Post by Gunroar:Cannon() »

I like it? Could the key arguments be in a table or something instead of one long string?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: [LIBRARY] tt.lua - Text triggers for lua!

Post by Gunroar:Cannon() »

I like it? Could the key arguments be in a table or something instead of one long string?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: [LIBRARY] tt.lua - Text triggers for lua!

Post by veethree »

Gunroar:Cannon() wrote: Tue Oct 12, 2021 7:53 pm I like it? Could the key arguments be in a table or something instead of one long string?
Sure. I've updated it. Now the tt:new() function can also take a table of strings

Code: Select all

tt:new({"w", "w"}, print, "double w")
Also added an input filter function, So you can easily set it up to ignore input under certain conditions

Code: Select all

tt:setInputFilter(function() if someCondition then return false end)
Also renamed tt:update() to tt:updateTimer.
User avatar
WAWAWA
Prole
Posts: 32
Joined: Fri Aug 13, 2021 5:10 pm

Re: [LIBRARY] tt.lua - Text triggers for lua!

Post by WAWAWA »

This is great, keep up the good work fellow lua user.
User avatar
Nikki
Citizen
Posts: 83
Joined: Wed Jan 25, 2017 5:42 pm

Re: [LIBRARY] tt.lua - Text triggers for lua!

Post by Nikki »

I'm sure there is a more proper name for what this does,
My first thought would be 'konami code'
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 57 guests