Pack: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Felice Enellen
(mention .n and flesh out the examples to demonstrate its use)
imported>Felice Enellen
(different var name so we don't trigger the generic syntax highlighting of the word "table")
Line 10: Line 10:
== Examples ==
== Examples ==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table = pack(x, y, dx, dy)  -- returns {x, y, dx, dy}
tbl = pack(x, y, dx, dy)  -- returns {x, y, dx, dy}
print(#table)                -- prints "4"
print(#tbl)                -- prints "4"
print(table.n)              -- prints "4"
print(tbl.n)              -- prints "4"


table = pack(x, y)          -- returns {x, y}
tbl = pack(x, y)          -- returns {x, y}
print(#table)                -- prints "2"
print(#tbl)                -- prints "2"
print(table.n)              -- prints "2"
print(tbl.n)              -- prints "2"


table = pack(x, y, nil, nil) -- returns {x, y}
tbl = pack(x, y, nil, nil) -- returns {x, y}
print(#table)                -- prints "2"
print(#tbl)                -- prints "2"
print(table.n)              -- prints "4"
print(tbl.n)              -- prints "4"
</syntaxhighlight>
</syntaxhighlight>



Revision as of 16:23, 29 September 2020

pack( ... )
Creates a table from the given parameters.
...
parameters

Returns a new table with all parameters stored sequentially into keys [1], [2], etc.

The table will also contain a member .n which specifies how many arguments were passed to pack(), including nil arguments. This can be used to detect the difference between an argument that was not passed at all and a nil value that was deliberately passed, since the # operator stops when it reaches a nil value.

Examples

tbl = pack(x, y, dx, dy)   -- returns {x, y, dx, dy}
print(#tbl)                -- prints "4"
print(tbl.n)               -- prints "4"

tbl = pack(x, y)           -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)               -- prints "2"

tbl = pack(x, y, nil, nil) -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)               -- prints "4"

 See also