cheatcode.lua - Add konami code style cheatcodes to your game

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
farzher
Prole
Posts: 41
Joined: Fri Jul 31, 2015 5:38 pm

cheatcode.lua - Add konami code style cheatcodes to your game

Post by farzher »

Example:

Code: Select all

Cheatcode = require('cheatcode')

local codes = {}
codes['{up}{up}{down}{down}{left}{right}{left}{right}ba{return}'] = function() lives = lives + 99 end
codes.bighead = function() bighead = not bighead end
Cheatcode(codes)

function love.keyreleased(key)
  Cheatcode.handle(key)
end
This might also be useful as a super simple no UI debugging tool (for enabling/disabling `noclip` for example)

Download: https://github.com/farzher/cheatcode.lua
Attachments
cheatcode.lua
(1.86 KiB) Downloaded 144 times
butts
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: cheatcode.lua - Add konami code style cheatcodes to your game

Post by airstruck »

Interesting use of a trie there :)

Just a suggestion, you could make it a bit more flexible, eliminate global state and (if you want) metatables by changing the API a bit:

Code: Select all

Cheatcode = require('cheatcode')

local codes = {}
codes['{up}{up}{down}{down}{left}{right}{left}{right}ba{return}'] = function() lives = lives + 99 end
codes.bighead = function() bighead = not bighead end
local cheatcodes = Cheatcode(codes)

function love.keyreleased(key)
  cheatcodes:handle(key)
end
Thought it would be interesting to try to build the trie with pattern matching, it's ugly as hell but here it is:

Code: Select all

local function trie (t, text)
    ('}' .. text .. '{'):gsub('%b}{', function (m)
        return '}' .. m:sub(2, -2):gsub('.', '{%0}') ..'{' 
    end):sub(2, -2):gsub('%b{}', function (m)
        m = m:sub(2, -2)
        t[m] = t[m] or {}
        t = t[m]
    end)
end
http://hastebin.com/qukarasipi.lua
User avatar
farzher
Prole
Posts: 41
Joined: Fri Jul 31, 2015 5:38 pm

Re: cheatcode.lua - Add konami code style cheatcodes to your game

Post by farzher »

Oh, cool! I've never heard of trie before. I guess that is exactly what I did.
This is probably the best performance solution for this problem, right?

I don't see any advantage to your API change suggestion, it's just a bit more code. Global state I think is good here, you'd never want more than 1 of these.

Your trie function is more confusing than puzzles in The Witness.
Although it's small, and works; that's pretty cool
butts
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

Re: cheatcode.lua - Add konami code style cheatcodes to your game

Post by Le_juiceBOX »

moist
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: cheatcode.lua - Add konami code style cheatcodes to your game

Post by Pangit »

Going to use this lib. Very nice. Good job dude.

Update:

Very nice, I use this library for testing. Especially developing new locations in game ect its great to be able to break the game rules ect..
Post Reply

Who is online

Users browsing this forum: No registered users and 86 guests