Skip to content

Constants

Wolfyxon edited this page Nov 10, 2024 · 15 revisions

When your game is ran or build with Lover, you may access certain constants like the game's name or version defined in lover.toml.

See Project configuration

Skip to the list of constants.

Usage

Use the os.getenv function.

Example

main.lua

local name = os.getenv("LOVER_NAME")
local author = os.getenv("LOVER_AUTHOR")
local version = os.getenv("LOVER_VERSION")

print(name, "by", author)
print("Version:", version)

lover.toml

[package]
name = "Cool game"
author = "Cool person"
version = "1.0"

Output

Cool game by Cool Person
Version: 1.0

Available constants

  • LOVER_VERSION: Version of your game
  • LOVER_NAME: Name of your game
  • LOVER_AUTHOR: You
  • LOVER_DESCRIPTION: Description of your game
  • LOVER_CONTEXT: "build" or "run". Used to check if the game is packaged or ran with lover run.
  • LOVER_TIMESTAMP: Epoch UNIX timestamp of when the game was built or ran by lover run.

Custom constants

Important

When the game is built, those constants are NOT treated like regular env variables, so passing things like DRI_PRIME won't have any effect, besides being readable by your game. They only work like this with lover run.

Read more

You can see how to include custom environment variables in the project configuration.

You can also do

SOME_ENV_VARIABLE=test lover run

Best practices

Default values

If your game is open source or developed by a larger team, it's recommended that you don't rely on those constants in a way that the game will crash without them.

For example you can use

gameVersion = os.getenv("LOVER_VERSION") or "unknown"

so if the game is ran by another tool or regular love, it will still work.

How it works?

In testing

When you run lover run, all constants are passed to the love process as environment variables.

In production

Environment variables cannot be modified when the process is running, so when your game is built, an extra piece of code is injected into it containing a table with all constants and the os.getenv function is modified to first look into that table before checking the actual env variables.

You can preview it by running

lover module
Clone this wiki locally