Powder Toy Lua API (preview)
Contents
- 1 bz2
- 2 elements
- 3 event
- 4 fileSystem
- 5 graphics
- 6 http
- 7 interface
- 7.1 interface.activeMenu
- 7.2 interface.activeTool
- 7.3 interface.addComponent
- 7.4 interface.beginConfirm
- 7.5 interface.beginInput
- 7.6 interface.beginMessageBox
- 7.7 interface.beginThrowError
- 7.8 interface.brushID
- 7.9 interface.brushRadius
- 7.10 interface.button
- 7.11 interface.checkbox
- 7.12 interface.closeWindow
- 7.13 interface.console
- 7.14 interface.dropTextInput
- 7.15 interface.grabTextInput
- 7.16 interface.label
- 7.17 interface.menuEnabled
- 7.18 interface.mousePosition
- 7.19 interface.numMenus
- 7.20 interface.perfectCircleBrush
- 7.21 interface.progressBar
- 7.22 interface.removeComponent
- 7.23 interface.showWindow
- 7.24 interface.slider
- 7.25 interface.textInputRect
- 7.26 interface.textbox
- 7.27 interface.window
- 7.28 interface.windowSize
- 7.29 constants
- 8 platform
- 9 renderer
- 9.1 renderer.colorMode
- 9.2 renderer.debugHud
- 9.3 renderer.decorations
- 9.4 renderer.depth3d
- 9.5 renderer.displayModes
- 9.6 renderer.fireSize
- 9.7 renderer.grid
- 9.8 renderer.hud
- 9.9 renderer.renderModes
- 9.10 renderer.showBrush
- 9.11 renderer.useDisplayPreset
- 9.12 renderer.zoomEnabled
- 9.13 renderer.zoomScope
- 9.14 renderer.zoomWindow
- 10 simulation
- 10.1 simulation.addCustomGol
- 10.2 simulation.adjustCoords
- 10.3 simulation.airMode
- 10.4 simulation.ambientAirTemp
- 10.5 simulation.ambientHeat
- 10.6 simulation.ambientHeatSim
- 10.7 simulation.brush
- 10.8 simulation.canMove
- 10.9 simulation.clearRect
- 10.10 simulation.clearSim
- 10.11 simulation.createBox
- 10.12 simulation.createLine
- 10.13 simulation.createParts
- 10.14 simulation.createWallBox
- 10.15 simulation.createWallLine
- 10.16 simulation.createWalls
- 10.17 simulation.customGravity
- 10.18 simulation.decoBox
- 10.19 simulation.decoBrush
- 10.20 simulation.decoColor
- 10.21 simulation.decoLine
- 10.22 simulation.decoSpace
- 10.23 simulation.deleteStamp
- 10.24 simulation.edgeMode
- 10.25 simulation.elecMap
- 10.26 simulation.elementCount
- 10.27 simulation.fanVelocityX
- 10.28 simulation.fanVelocityY
- 10.29 simulation.floodDeco
- 10.30 simulation.floodParts
- 10.31 simulation.floodWalls
- 10.32 simulation.frameRender
- 10.33 simulation.getSaveID
- 10.34 simulation.golSpeedRatio
- 10.35 simulation.gravityField
- 10.36 simulation.gravityGrid
- 10.37 simulation.gravityMass
- 10.38 simulation.gravityMode
- 10.39 simulation.heatSim
- 10.40 simulation.historyForward
- 10.41 simulation.historyRestore
- 10.42 simulation.lastUpdatedID
- 10.43 simulation.listCustomGol
- 10.44 simulation.listStamps
- 10.45 simulation.loadSave
- 10.46 simulation.loadStamp
- 10.47 simulation.neighbors
- 10.48 simulation.newtonianGravity
- 10.49 simulation.partChangeType
- 10.50 simulation.partCount
- 10.51 simulation.partCreate
- 10.52 simulation.partExists
- 10.53 simulation.partID
- 10.54 simulation.partKill
- 10.55 simulation.partNeighbors
- 10.56 simulation.partPosition
- 10.57 simulation.partProperty
- 10.58 simulation.parts
- 10.59 simulation.paused
- 10.60 simulation.photons
- 10.61 simulation.pmap
- 10.62 simulation.pressure
- 10.63 simulation.prettyPowders
- 10.64 simulation.reloadSave
- 10.65 simulation.removeCustomGol
- 10.66 simulation.replaceModeFlags
- 10.67 simulation.resetGravityField
- 10.68 simulation.resetPressure
- 10.69 simulation.resetSpark
- 10.70 simulation.resetTemp
- 10.71 simulation.resetVelocity
- 10.72 simulation.saveStamp
- 10.73 simulation.signs
- 10.74 simulation.signs constants
- 10.75 simulation.signs[n]
- 10.76 simulation.signs.new
- 10.77 simulation.signs.delete
- 10.78 simulation.takeSnapshot
- 10.79 simulation.temperatureScale
- 10.80 simulation.toolBox
- 10.81 simulation.toolBrush
- 10.82 simulation.toolLine
- 10.83 simulation.updateUpTo
- 10.84 simulation.velocityX
- 10.85 simulation.velocityY
- 10.86 simulation.wallMap
- 10.87 simulation.walls
- 10.88 simulation.walls[id]
- 10.89 simulation.walls.'WL'
- 10.90 simulation.waterEqualization
- 10.91 constants
- 11 socket
- 12 tpt
- 13 Button
- 14 Checkbox
- 15 Label
- 16 ProgressBar
- 17 Slider
- 18 Textbox
- 19 Window
- 19.1 Window:addComponent
- 19.2 Window:onBlur
- 19.3 Window:onDraw
- 19.4 Window:onExit
- 19.5 Window:onFocus
- 19.6 Window:onInitialized
- 19.7 Window:onKeyPress
- 19.8 Window:onKeyRelease
- 19.9 Window:onMouseDown
- 19.10 Window:onMouseMove
- 19.11 Window:onMouseUp
- 19.12 Window:onMouseWheel
- 19.13 Window:onTick
- 19.14 Window:onTryExit
- 19.15 Window:onTryOkay
- 19.16 Window:position
- 19.17 Window:removeComponent
- 19.18 Window:size
bz2
The bz2
API provides access to the bzip2 library TPT bundles. It can be used for compressing and decompressing blocks of data, such as TPT saves.
Only one-shot functionality is exposed because streaming functionality is only useful for tasks that are beyond the scope of this API.
Unless stated otherwise, all functions raise errors if supplied with parameters that disagree with their descriptions.
bz2.compress
Compress data with bzip2 at compression level 9.
compressedData, errCode, errStr = bz2.compress(data, [maxSize])
-
data
: string, the data to be compressed -
maxSize
: number, upper limit on the length ofcompressedData
; defaults to0
, which means no limit -
compressedData
: string, the compressed data, ornil
on error -
errCode
:nil
, or one of the following values on error:-
bz2.COMPRESS_NOMEM
: out of memory, TPT will probably crash soon -
bz2.COMPRESS_LIMIT
: the length ofcompressedData
would exceedmaxSize
-
-
errStr
:nil
, or a human-friendly string that explains the error
bz2.decompress
Decompress bzip2-compressed data.
data, errCode, errStr = bz2.decompress(compressedData, [maxSize])
-
compressedData
: string, the compressed data -
maxSize
: number, upper limit on the length ofdata
; defaults to0
, which means no limit -
data
: string, the original data, ornil
on error -
errCode
:nil
, or one of the following values on error:-
bz2.DECOMPRESS_NOMEM
: out of memory, TPT will probably crash soon -
bz2.DECOMPRESS_LIMIT
: the length ofdata
would exceedmaxSize
-
bz2.DECOMPRESS_TYPE
:compressedData
is not bzip2-compressed data -
bz2.DECOMPRESS_BAD
:compressedData
is otherwise corrupt -
bz2.DECOMPRESS_EOF
:compressedData
contains data beyond the end of the bzip2 stream
-
-
errStr
:nil
, or a human-friendly string that explains the error
elements
The elements
API contains methods and constants for creating and modifying elements.
The shorter alias elem
is also available.
Unless stated otherwise, all functions raise errors if supplied with parameters that disagree with their descriptions.
elements.[group]_PT_[iname]
watrNumber = elements.DEFAULT_PT_WATR
There are two ways to refer to elements: element numbers and element identifiers, which are strings of the form "[group]_PT_[iname]"
. Both [group]
and [iname]
can be any string without underscores (_
). All built-in elements are in group DEFAULT
. For example, the identifier of WATR is "DEFAULT_PT_WATR"
, because WATR belongs to the group DEFAULT
and its internal name is WATR
. This is different from the Name
property.
These constants map element identifiers to element numbers. The element number of WATR is 2
, so elements.DEFAULT_PT_WATR
is also 2
.
elements.allocate
Create a new element.
elemNumber = elements.allocate(group, iname)
-
group
: string without underscores (_
), the group the element belongs to; gets uppercased by the function -
iname
: string without underscores (_
), the internal name of the element; gets uppercased by the function -
elemNumber
: the positive element number allocated for the element created, or-1
on error, if there are no free element numbers left
group
should be something unique to your script, and should be the same across the entire script. It is common to use a simpler version of your username or the script’s name, for example if your script is called Ultimate Chemistry Pack v3, you might use "CHEMPACK3"
as the group name.
iname
should be unique to the element within your script, and should ultimately resemble the Name
property of the element. For example, if your element’s name is C-6 you should use C6
as the internal name.
The resulting element identifier must be unique across all scripts in use at any point in time. Elements that seem like built-in elements, i.e. ones in the group DEFAULT
, cannot be created. Note that, as stated above, both group
and iname
get uppercased, so elements.allocate("CheMpaCk3", "c6")
is equivalent to elements.allocate("CHEMPACK3", "C6")
.
Make the choice such that it is convenient to refer to your element via an elements.[group]_PT_[iname]
constant. While it is perfectly possible to type elem["Ultimate Chemistry Pack v3_PT_C-6"]
, it is much more convenient to type elem.CHEMPACK3_PT_C6
.
The new element is created with all the default properties, and will not be visible until you modify it to show up in the menu.
elements.element
Query all or update multiple properties of an element.
elemProps = elements.element(elemNumber) -- query variant elements.element(elemNumber, elemProps) -- update variant
-
elemNumber
: number of the element whose properties are to be queried or update -
elemProps
: table that maps property names to property values
The keys and values of elemProps
are the same as the propName
and propValue
parameters of elements.property. The query variant returns all properties of the element in elemProps
with the same caveats as elements.property. The update variant accepts any subset of properties, only updates the ones present in the table, applying the same checks as elements.property.
This function is commonly used to base an element off another element by first copying all properties of the source element and applying them to the new element, and then customizing the new element a bit afterwards:
local purpleGold = elem.allocate("EXAMPLE", "PGLD") assert(purpleGold ~= -1, "ran out of element numbers") elem.element(purpleGold, elem.element(elem.DEFAULT_PT_GOLD)) elem.property(purpleGold, "Name", "PGLD") elem.property(purpleGold, "Color", 0x8040FF)
elements.exists
Check whether a number is a real element number and refers to an element.
exists = elements.exists(elemNumber)
-
elemNumber
: number of the element to be checked -
exists
: boolean,true
ifelemNumber
refers to an element
If an element exists, there exists a corresponding elements.[group]_PT_[iname]
constant, and conversely, if there exists such a constant, there exists a corresponding element.
elements.free
Free a previously allocated element.
elements.free(elemNumber)
-
elemNumber
: number of the element to be freed
The element number is freed and can used later by another script. Built-in elements, i.e. elements in the group DEFAULT
, cannot be freed.
elements.getByName
Find an element by name, the Name
property.
elementNumber = elements.getByName(name)
-
name
: string, the name to find the element by -
elemNumber
: positive number of the elementname
refers to, or-1
on error if no such element exists
This function converts a human-friendly element name to an element number, essentially the same way the PROP tool or the console works.
elements.loadDefault
Restore the set of elements to its initial state at startup.
elements.loadDefault()
This frees all elements created and resets all properties of all built-in elements to their defaults.
elements.property
Query or update a property of an element.
propValue = elements.property(elemNumber, propName) -- query variant elements.property(elemNumber, propName, propValue) -- update variant elements.property(elemNumber, "Update", propValue, [runWhen]) -- special update variant for the Update property
-
elemNumber
: number of the element whose property is to be queried or updated -
propName
: string, name of the property to be queried or updated -
propValue
: various types, value of the property to be queried or updated -
runWhen
: number, specifies when the update function should be run, one of:-
elements.UPDATE_AFTER
: run before the built-in update function, this is the default -
elements.UPDATE_REPLACE
: run instead of the built-in update function -
elements.UPDATE_BEFORE
: run after the built-in update function
-
The following table lists all element properties accessible via this function:
propName
|
propValue type
|
description |
---|---|---|
"Advection"
|
number | |
"AirDrag"
|
number | |
"AirLoss"
|
number | |
"CarriesTypeIn"
|
number | |
"ChangeType"
|
function | read-only |
"Collision"
|
number | |
"Color"
|
number | |
"Create"
|
function | read-only |
"CreateAllowed"
|
function | read-only |
"CtypeDraw"
|
function | read-only |
"DefaultProperties"
|
table | |
"Description"
|
string | |
"Diffusion"
|
number | |
"Explosive"
|
number | |
"Falldown"
|
number | |
"Flammable"
|
number | |
"Graphics"
|
function | read-only |
"Gravity"
|
number | |
"Hardness"
|
number | |
"HeatConduct"
|
number | |
"HighPressure"
|
number | |
"HighPressureTransition"
|
element number | |
"HighTemperature"
|
number | |
"HighTemperatureTransition"
|
element number | |
"HotAir"
|
number | |
"Identifier"
|
string | element identifier, read-only |
"Loss"
|
number | |
"LowPressure"
|
number | |
"LowPressureTransition"
|
element number | |
"LowTemperature"
|
number | |
"LowTemperatureTransition"
|
element number | |
"Meltable"
|
0 or 1
|
|
"MenuSection"
|
menu number | |
"MenuVisible"
|
0 or 1
|
|
"Name"
|
string | text that appears on the element button |
"NewtonianGravity"
|
number | |
"PhotonReflectWavelengths"
|
number | |
"Properties"
|
number | |
"Temperature"
|
number | |
"Update"
|
function | read-only |
"Weight"
|
number |
runWhen
is not possible to specify with elements.element.
event
event.getModifiers
event.register
event.unregister
fileSystem
fileSystem.copy
fileSystem.exists
fileSystem.isDirectory
fileSystem.isFile
fileSystem.isLink
fileSystem.list
fileSystem.makeDirectory
fileSystem.move
fileSystem.removeDirectory
fileSystem.removeFile
graphics
graphics.drawCircle
graphics.drawLine
graphics.drawPixel
graphics.drawRect
graphics.drawText
graphics.fillCircle
graphics.fillRect
graphics.getColors
graphics.getHexColor
graphics.setClipRect
graphics.textSize
constants
http
http.get
http.getAuthToken
http.post
interface
interface.activeMenu
interface.activeTool
interface.addComponent
interface.beginConfirm
interface.beginInput
interface.beginMessageBox
interface.beginThrowError
interface.brushID
interface.brushRadius
interface.button
interface.checkbox
interface.closeWindow
interface.console
interface.dropTextInput
interface.grabTextInput
interface.label
interface.mousePosition
interface.numMenus
interface.perfectCircleBrush
interface.progressBar
interface.removeComponent
interface.showWindow
interface.slider
interface.textInputRect
interface.textbox
interface.window
interface.windowSize
constants
platform
platform.clipboardCopy
platform.clipboardPaste
platform.exeName
platform.ident
platform.openLink
platform.platform
platform.releaseType
platform.restart
renderer
renderer.colorMode
renderer.debugHud
renderer.decorations
renderer.depth3d
renderer.displayModes
renderer.fireSize
renderer.grid
renderer.hud
renderer.renderModes
renderer.showBrush
renderer.useDisplayPreset
renderer.zoomEnabled
renderer.zoomScope
renderer.zoomWindow
simulation
simulation.addCustomGol
simulation.adjustCoords
simulation.airMode
simulation.ambientAirTemp
simulation.ambientHeat
simulation.ambientHeatSim
simulation.brush
simulation.canMove
simulation.clearRect
simulation.clearSim
simulation.createBox
simulation.createLine
simulation.createParts
simulation.createWallBox
simulation.createWallLine
simulation.createWalls
simulation.customGravity
simulation.decoBox
simulation.decoBrush
simulation.decoColor
simulation.decoLine
simulation.decoSpace
simulation.deleteStamp
simulation.edgeMode
simulation.elecMap
simulation.elementCount
simulation.fanVelocityX
simulation.fanVelocityY
simulation.floodDeco
simulation.floodParts
simulation.floodWalls
simulation.frameRender
simulation.getSaveID
simulation.golSpeedRatio
simulation.gravityField
simulation.gravityGrid
simulation.gravityMass
simulation.gravityMode
simulation.heatSim
simulation.historyForward
simulation.historyRestore
simulation.lastUpdatedID
simulation.listCustomGol
simulation.listStamps
simulation.loadSave
simulation.loadStamp
simulation.neighbors
simulation.newtonianGravity
simulation.partChangeType
simulation.partCount
simulation.partCreate
simulation.partExists
simulation.partID
simulation.partKill
simulation.partNeighbors
simulation.partPosition
simulation.partProperty
simulation.parts
simulation.paused
simulation.photons
simulation.pmap
simulation.pressure
simulation.prettyPowders
simulation.reloadSave
simulation.removeCustomGol
simulation.replaceModeFlags
simulation.resetGravityField
simulation.resetPressure
simulation.resetSpark
simulation.resetTemp
simulation.resetVelocity
simulation.saveStamp
simulation.signs
simulation.signs constants
simulation.signs[n]
simulation.signs.new
simulation.signs.delete
simulation.takeSnapshot
simulation.temperatureScale
simulation.toolBox
simulation.toolBrush
simulation.toolLine
simulation.updateUpTo
simulation.velocityX
simulation.velocityY
simulation.wallMap
simulation.walls
simulation.walls[id]
simulation.walls.'WL'
simulation.waterEqualization
constants
socket
socket.getTime
socket.sleep
socket.tcp
tpt
tpt.debug
tpt.drawCap
tpt.fpsCap
tpt.getUserName
tpt.installScriptManager
tpt.log
tpt.record
tpt.screenshot
tpt.version
Button
Button:action
Button:enabled
Button:position
Button:size
Button:text
Button:visible
Checkbox
Checkbox:action
Checkbox:checked
Checkbox:position
Checkbox:size
Checkbox:text
Checkbox:visible
Label
Label:position
Label:size
Label:text
Label:visible
ProgressBar
ProgressBar:position
ProgressBar:progress
ProgressBar:size
ProgressBar:status
ProgressBar:visible
Slider
Slider:onValueChanged
Slider:position
Slider:size
Slider:steps
Slider:value
Slider:visible
Textbox
Textbox:onTextChanged
Textbox:position
Textbox:readonly
Textbox:size
Textbox:text
Textbox:visible
Window
Window:addComponent
Window:onBlur
Window:onDraw
Window:onExit
Window:onFocus
Window:onInitialized
Window:onKeyPress
Window:onKeyRelease
Window:onMouseDown
Window:onMouseMove
Window:onMouseUp
Window:onMouseWheel
Window:onTick
Window:onTryExit
Window:onTryOkay
Window:position
Window:removeComponent