PICO-8 Wiki
Advertisement
tostring( val )
Converts a non-string value to a string representation, using the vanilla Lua implementation.
val
The value to convert

Returns the string representation of the value, using vanilla Lua code instead of PICO-8's custom tostr() function. Please refer to that function for full documentation, as that is the one typically used, and therefore only the differences from it are documented here.

At the moment, these are the known differences from PICO-8's custom implementation:

  • There is only one parameter, whereas tostr accepts a second optional argument to control formatting of numbers.
  • Object references (tables, functions, coroutines), are formatted without square brackets and include the internal pointer associated with the reference, e.g. "table: 0x11eada34" vs. tostr()'s simpler "[table]".
  • Passing nothing, also known as the "empty" value, results in a runtime error, whereas tostr() will return a zero-length string.
  • The string version of nil will not have square brackets.

(If you, the reader, know of any other differences, please feel free to document them here.)

Warning[]

As an undocumented function, tostring() cannot be relied on for a game. Its functionality may change or be removed in future versions. Still, some may find a pragmatic use for it in tools or demos. Just be aware that you use it at your own risk.

Examples[]

s = 'v: '..tostring(12345)            -- 'v: 12345'
s = 'v: '..tostring(-12345.67)        -- 'v: -12345.67'

s = 'v: '..tostring(0x0f)             -- 'v: 15'

-- runtime error: attempt to concatenate a boolean value
s = 'v: '..true                    

s = 'v: '..tostring(true)             -- 'v: true'
s = 'v: '..tostring('my string')      -- 'v: my string'

s = 'v: '..tostring(nil)              -- 'v: nil'
s = 'v: '..tostring({1, 2, 3})        -- 'v: table: 0x0f31335e'

f = function() print('hi') end
s = 'v: '..tostring(f)                -- 'v: function: 0x0f313c1a'

-- f is a function that does not return a value
f = function() return end
a = 'v: '..tostring(f())              -- runtime error

See also[]

Advertisement