-
Notifications
You must be signed in to change notification settings - Fork 0
API Additional Lua Features
PICO-8 also exposes 2 features of Lua for advanced users: Metatables and Coroutines.
For more information, please refer to the Lua 5.2 manual.
Metatables can be used to define the behaviour of objects under particular operations. For example, to use tables to represent 2D vectors that can be added together, the '+' operator is redefined by defining an "__add" function for the metatable:
VEC2D={
__ADD=FUNCTION(A,B)
RETURN {X=(A.X+B.X), Y=(A.Y+B.Y)}
END
}
V1={X=2,Y=9} SETMETATABLE(V1, VEC2D)
V2={X=1,Y=5} SETMETATABLE(V2, VEC2D)
V3 = V1+V2
PRINT(V3.X..","..V3.Y) -- 3,14
Set table TBL metatable to M
return the current metatable for table t, or nil if none is set
Raw access to the table, as if no metamethods were defined.
The list of function arguments can be specified with ...
FUNCTION PREPRINT(PRE, S, ...)
LOCAL S2 = PRE..TOSTR(S)
PRINT(S2, ...) -- PASS THE REMAINING ARGUMENTS ON TO PRINT()
END
To accept a variable number of arguments, use them to define a table and/or use Lua's select() function. select(index, ...) returns all of the arguments after index.
FUNCTION FOO(...)
LOCAL ARGS={...} -- BECOMES A TABLE OF ARGUMENTS
FOREACH(ARGS, PRINT)
?SELECT("#",...) -- ALTERNATIVE WAY TO COUNT THE NUMBER OF ARGUMENTS
FOO2(SELECT(3,...)) -- PASS ARGUMENTS FROM 3 ONWARDS TO FOO2()
END
Coroutines offer a way to run different parts of a program in a somewhat concurrent way, similar to threads. A function can be called as a coroutine, suspended with
FUNCTION HEY()
PRINT("DOING SOMETHING")
YIELD()
PRINT("DOING THE NEXT THING")
YIELD()
PRINT("FINISHED")
END
C = COCREATE(HEY)
FOR I=1,3 DO CORESUME(C) END
Create a coroutine for function f.
Run or continue the coroutine c. Parameters p0, p1โฆ are passed to the coroutine's function.
Returns true if the coroutine completes without any errors Returns false, error_message if there is an error.
** Runtime errors that occur inside coroutines do not cause the program to stop running. It is a good idea to wrap CORESUME() inside an ASSERT(). If the assert fails, it will print the error message generated by coresume.
Return the status of coroutine C as a string:
- "running"
- "suspended"
- "dead"
Suspend execution and return to the caller.
- ๐ Keys
- ๐ Hello World
- ๐พ Example Cartridges
- ๐ File System
โคด๏ธ Loading and Saving- ๐ Using an External Text Editor
- ๐ฝ Backups
- ๐ง Configuration
- ๐ธ Screenshots and GIFs
- ๐ Sharing Cartridges
- ๐ SPLORE
- ๐ผ๏ธ Sprite Sheet / Label (.png)
- ๐ต SFX and Music (.wav)
- ๐ค MAP and CODE
- ๐พ Cartridges (.p8, .p8.png, .p8.rom)
- ๐ Web Applications (.html)
- ๐ค Binary Applications (.bin)
- ๐น๏ธ Uploading to itch.io
- ๐พ Exporting Multiple Cartridges
- ๐ฅ Running EXPORT from the host operating system