Tokens

From Pico-8 Wiki
Revision as of 20:53, 12 June 2022 by 107.204.125.41 (talk)
Jump to navigation Jump to search

One of the stricter limits PICO-8 places on its carts' code is that they must contain no more than 8192 tokens.

A token is one of:

  • A literal value, such as nil, false, true, any number (e.g. 123, 0xff.ff, -3, ~1e4), or any string (of any size).
  • A variable or operator
  • An opening bracket ('(', '[', '{')
  • A keyword, with the exception of end and local

In particular, the following are not counted as tokens:

  • The comma (,), semicolon (;), period (.), colon (:), and double-colon (::)
  • Closing brackets (')', ']', '}')
  • The end and local keywords
  • The unary minus (-) and complement (~) operators when applied on a numeric literal

Token saving tips

Common token saving tips that apply to most situations:

  • Instead of assigning one variable per statement, combine assignments into a single statement, saving on the '=' token:

    a=1 b=2 c=3 → a,b,c = 1,2,3

    local a=1; local b=2 → local a,b = 1,2
  • You can omit trailing nils when assigning variables:

    mynum,myobj,myobj2 = 3,nil,nil -> mynum,myobj,myobj2 = 3

    local a,b = nil,nil → local a,b
    • You can also omit the declaration entirely and it will be equivalent to declaring it as nil. It is worth commenting so you do not lose track of your variables.
  • You can usually omit trailing nils when calling functions:

    f(a,b,nil,nil) → f(a,b)

    Beware that some functions do check the number of arguments provided, and differ in behaviour between a nil and an unprovided argument
  • When calling a function with only 1 argument, and that argument is a string or table literal, you can omit the parentheses to save a token:

    f("smart") → f "smart"

    f({1,2,3}) → f {1,2,3}
  • You can usually pass nil instead of false to boolean variables and parameters, allowing you to use other tricks in this list that allow omitting nil.

    f(a,b,false) → f(a,b)

    local a,b = true, false → local a,b = true

    Beware that while most operators and functions operating on booleans accept nil as false, a small few (like direct comparison via ==) can still break this approach
  • When creating your own functions, move any parameters that may not need to be specified or that can have a good default value to the end of the parameter list, so that callers can usually avoid passing them.
  • Avoid defining constants or computing constants, preferring to use a single constant instead.

    hour=60*60; f(hour); g(hour) → f(3600); g(3600)

    Beware that this can make changing those constants harder, so you may want to do it only once you're sure the constants won't change anymore.
  • Use a local to save the value of a repeated computation, when doing so will save tokens. (The more complex the computation and the more it is repeated, the more likely that a local will save tokens)

    f(a+1); g(a+1); h(a+1) → local ap1=a+1; f(ap1); g(ap1); h(ap1)

    f(a.x, a.x+1, a.x+5, a.x+10, a.x+12); g(a.x, -a.x) → local ax=a.x; f(ax, ax+1, ax+5, ax+10, ax+12); g(ax, -ax)
  • For constant tables of numbers or strings, you can use a string literal with split() to save many tokens.

    t={1,2,3,10,9,8,99,-1} → t=split "1,2,3,10,9,8,99,-1"
  • Use and and or instead of if when possible:

    if (f()) a=x else a=y → a = f() and x or y (Beware: only works if x can never be false or nil!)
  • Prefer member access over array indexing:

    tbl['special'] → tbl.special

    An object like {a=1,b=2,c=3} takes fewer tokens to read/write values from that {1,2,3}, though the latter takes fewer tokens to construct. This is usually beneficial as objects usually need to be accessed in multiple places but only need to be constructed in one function.

Miscallenous token saving tips for specific situations:

  • You can save 1 token by converting pairs(mytable) to next, mytable (which is equivalent, with the minor exception that pairs calls the __pairs metamethod if you implemented one)