Skip to content

Instantly share code, notes, and snippets.

@hazzard993
Created April 11, 2019 06:57
Show Gist options
  • Save hazzard993/0da5ef0c8a7ce39c2b763a52f8323cf8 to your computer and use it in GitHub Desktop.
Save hazzard993/0da5ef0c8a7ce39c2b763a52f8323cf8 to your computer and use it in GitHub Desktop.

Registries

LÖVE 2D can interact with many things through the use of strings.

love.audio.getEffect("anEffect");
// Nothing crashes if anEffect didn't exist

love.filesystem.readFile("file.txt");
// No crash, just returns an error

This repo uses registries, optionally, to remove concerns about file paths, image files and other external things being written incorrectly or being misused.

interface FilePathRegistry {
    "file.txt": any;
}

This registry will make sure "file.txt" is the only file path that can be accessed.

TypeScript will make sure of this giving you compile-time errors and autocomplete options.

You can also use metadata on these entries.

interface FilePathRegistry {
    "file.txt": { lines: 64 };
}

You (or a .sh file) will have to make sure this metadata is true. But if you keep it up to date...

function read80LineFile(file: File<{
    lines: 80
}>) {
    // ...
}

const file = love.filesystem.read("file.txt");
read80LineFile(file);
// Not allowed, file must have 80 lines of text.

You can take this in depth as much as you want/can. Just note that simple comparisons like this are available, not too much else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment