Difference between revisions of "Lua API:Elements"

From The Powder Toy
Jump to: navigation, search
m
(98.0: Replace with LBPHacker's version (mostly). Link to existing shared doc pages where needed.)
 
(28 intermediate revisions by 13 users not shown)
Line 1: Line 1:
The Elements API Methods and constants for modifying and creating elements. If you want to add an update function or graphics function, see the Legacy API, specifically the functions tpt.element_func and tpt.graphics_func
+
The <code>elements</code> API contains methods and constants for creating and modifying elements.
 +
 
 +
'''The shorter alias <code>elem</code> is also available.'''
 +
 
 +
Unless stated otherwise, all functions raise errors if supplied with parameters that disagree with their descriptions.
 +
 
 
== Methods ==
 
== Methods ==
  
=== elements.allocate ===  
+
=== elements.allocate ===
number elements.allocate(string group, string name)
+
 
Use this function to create a new element. This function will return the id of your element, and create a unique identifier that can be used to modify the properties later. The identifier is in the form GROUP_PT_NAME, where group is the name of the mod or script (or just anything unique, like your username), and name is the name of the element. For example, elements.allocate("mymod", "virus") would create the identifier MYMOD_PT_VIRUS.
+
Create a new element.
 +
 
 +
<pre>elemNumber = elements.allocate(group, iname)</pre>
 +
* <code>group</code>: string without underscores (<code>_</code>), the group the element belongs to; gets uppercased by the function
 +
* <code>iname</code>: string without underscores (<code>_</code>), the internal name of the element; gets uppercased by the function
 +
* <code>elemNumber</code>: the positive element number allocated for the element created, or <code>-1</code> on error, if there are no free element numbers left
 +
 
 +
<code>group</code> 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 <code>&quot;CHEMPACK3&quot;</code> as the group name.
  
The identifier is added as a constant in the elements table, so elements.MYMOD_PT_VIRUS would be equivalent to the new element's id, and can be used as the elementID argument to any of the functions below.
+
<code>iname</code> should be unique to the element within your script, and should ultimately resemble the [[#elements.property|<code>Name</code> property]] of the element. For example, if your element’s name is ''C-6'' you should use <code>C6</code> as the internal name.
  
The new element is created with all the default properties, and won't be visible until you modify it to show up in the menu.
+
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 <code>DEFAULT</code>, cannot be created. Note that, as stated above, both <code>group</code> and <code>iname</code> get uppercased, so <code>elements.allocate(&quot;CheMpaCk3&quot;, &quot;c6&quot;)</code> is equivalent to <code>elements.allocate(&quot;CHEMPACK3&quot;, &quot;C6&quot;)</code>.
  
=== elements.free ===
+
Make the choice such that it is convenient to refer to your element via an [[#elements.group_pt_iname|<code>elements.[group]_PT_[iname]</code> constant]]. While it is perfectly possible to type <code>elem[&quot;Ultimate Chemistry Pack v3_PT_C-6&quot;]</code>, it is much more convenient to type <code>elem.CHEMPACK3_PT_C6</code>.
nil elements.free(number elementID)
 
Free a previously allocated element, so it will disappear from the game. The element id will be freed and can used later by another script. elementID must be a non-default element (i.e you cannot free the default WATR element)
 
  
=== elements.loadDefault ===
+
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.
nil elements.loadDefault()
+
 
Resets all elements to the original state. This will also erase any elements created with any scripts, only the default elements will be available.
+
=== elements.property ===
 +
 
 +
Query or update a property of an element.
 +
 
 +
<pre>propValue = elements.property(elemNumber, propName) -- query variant
 +
elements.property(elemNumber, propName, propValue) -- update variant
 +
elements.property(elemNumber, &quot;Update&quot;, propValue, [runWhen]) -- special update variant for the Update property</pre>
 +
* <code>elemNumber</code>: number of the element whose property is to be queried or updated
 +
* <code>propName</code>: string, name of the property to be queried or updated
 +
* <code>propValue</code>: various types, value of the property to be queried or updated
 +
* <code>runWhen</code>: number, specifies when the update function should be run, one of:
 +
** <code>elements.UPDATE_AFTER</code>: run before the built-in update function, this is the default
 +
** <code>elements.UPDATE_REPLACE</code>: run instead of the built-in update function
 +
** <code>elements.UPDATE_BEFORE</code>: run after the built-in update function
 +
 
 +
For more information on what properties there are to use in elements.property, and how to use them, see this page: [[Element_Properties]].
 +
 
 +
When working with the "MenuSection" or the "Properties" property, use one of the provided [[#Constants|constants]].
 +
 
 +
The "Identifier" property is read-only and cannot be set.
  
nil elements.loadDefault(number elementID)
+
Several event callback functions are implemented, such as "Update" and "Graphics". To set these, use a function for <code>propValue</code>. They are not included in the tables created with elements.element, and the functions can't be returned with elements.property either. This means copying all of an elements properties using elements.element will not set event callbacks for the new element. For help on creating these, see [[Element_Properties#Callback_functions]].
Reset an element to its original state before it was modified
 
  
 
=== elements.element ===
 
=== elements.element ===
table elements.element(number elementID)
 
Returns a table containing all of an element's properties (Name, Description, etc)
 
  
nil elements.element(number elementID, table properties)
+
Query all or update multiple properties of an element.
Sets the properties from the given table onto the element.
+
 
 +
<pre>elemProps = elements.element(elemNumber) -- query variant
 +
elements.element(elemNumber, elemProps) -- update variant</pre>
 +
* <code>elemNumber</code>: number of the element whose properties are to be queried or update
 +
* <code>elemProps</code>: table that maps property names to property values
 +
 
 +
The keys and values of <code>elemProps</code> are the same as the <code>propName</code> and <code>propValue</code> parameters of [[#elements.property|elements.property]]. The query variant returns all properties of the element in <code>elemProps</code> with the same caveats as [[#elements.property|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|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:
 +
 
 +
<pre>local purpleGold = elem.allocate(&quot;EXAMPLE&quot;, &quot;PGLD&quot;)
 +
assert(purpleGold ~= -1, &quot;ran out of element numbers&quot;)
 +
elem.element(purpleGold, elem.element(elem.DEFAULT_PT_GOLD))
 +
elem.property(purpleGold, &quot;Name&quot;, &quot;PGLD&quot;)
 +
elem.property(purpleGold, &quot;Color&quot;, 0x8040FF)</pre>
 +
 
 +
=== elements.exists ===
 +
 
 +
Check whether a number is a real element number and refers to an element.
 +
 
 +
<pre>exists = elements.exists(elemNumber)</pre>
 +
* <code>elemNumber</code>: number of the element to be checked
 +
* <code>exists</code>: boolean, <code>true</code> if <code>elemNumber</code> refers to an element
 +
 
 +
If an element exists, there exists a corresponding [[#elements.group_pt_iname|<code>elements.[group]_PT_[iname]</code> constant]], and conversely, if there exists such a constant, there exists a corresponding element.
 +
 
 +
=== elements.free ===
 +
 
 +
Free a previously allocated element.
 +
 
 +
<pre>elements.free(elemNumber)</pre>
 +
* <code>elemNumber</code>: number of the element to be freed
  
These two functions are useful for copying or templating from already present elements, for example
+
The element number is freed and can used later by another script. Built-in elements, i.e. elements in the group <code>DEFAULT</code>, cannot be freed.
<syntaxhighlight lang="lua">
 
local myNewElement = elements.allocate("wiki", "expl")
 
elements.element(elements.WIKI_PT_EXPL, elements.element(elements.DEFAULT_PT_PLEX))
 
elements.property(elements.WIKI_PT_EXPL, "Name", "EXPL")
 
elements.property(elements.WIKI_PT_EXPL, "Description", "This is an example element from the Wiki")
 
</syntaxhighlight>
 
In this example, the element properties for our new element (EXPL) are copied from WATR
 
  
=== elements.property ===
+
<span id="elements.getbyname"></span>
object elements.property(number elementID, string property)
+
=== elements.getByName ===
Gets the value of an element property
+
 
 +
Find an element by name, the [[#elements.property|<code>Name</code> property]].
 +
 
 +
<pre>elementNumber = elements.getByName(name)</pre>
 +
* <code>name</code>: string, the name to find the element by
 +
* <code>elemNumber</code>: positive number of the element <code>name</code> refers to, or <code>-1</code> on error if no such element exists
  
nil elements.property(number elementID, string property, object value)
+
This function converts a human-friendly element name to an element number, essentially the same way the PROP tool or the console works.
Sets the value of an element property
 
  
== Properties ==
+
<span id="elements.loaddefault"></span>
After creating an element, you can modify any of these properties. Be sure to set Name, Description, and MenuVisible at least.
+
=== elements.loadDefault ===
  
; Name
+
Restore the set of elements to its initial state at startup.
Name of your element
+
 
; Colour
+
<pre>elements.loadDefault()</pre>
; Color
+
This frees all elements created and resets all properties of all built-in elements to their defaults.
These two are the same thing, just use whichever one you like
 
; MenuVisible
 
Set this to 1 so the element shows up
 
; MenuSection
 
Set this to one of the menusection constants listed below
 
; Advection
 
; AirDrag
 
Larger this value , larger air pressure it creates from your particle falling down
 
; AirLoss
 
Same like airdrag.
 
; Loss
 
; Collision
 
; Gravity
 
; Diffusion
 
; HotAir
 
How much it will increase the pressure around it every frame
 
; Falldown
 
0 = solid, gas, or energy particle, 1 = powder, 2 = liquid
 
; Flammable
 
The higher the value, the faster it will burn (on contact with FIRE, SPRK, etc). Extremely high values increase the temperature too.
 
; Explosive
 
0 = does not explode, 1 = when touching fire, 2 = when touching fire or when pressure > 2.5
 
; Meltable
 
; Hardness
 
1/Resistance to acid; larger this value = less acid resistance.
 
; Weight
 
Weight of your particle, if you set your particle weight at 47000 it can deform Titanium.
 
; Temperature
 
starting temperature, in Kelvin
 
; HeatConduct
 
Heat conductivity , larger this value = faster heat conductivity to your particle.
 
; Description
 
; State
 
Use the state constants listed below
 
; Properties
 
See the property constants listed below. You can set multiple of these, just add them together with + or use the bit api to do this. Be sure to use the Type constants here too, or else it may not act as expected with things like walls.
 
; LowPressure
 
; LowPressureTransition
 
; HighPressure
 
; HighPressureTransition
 
; LowTemperature
 
; LowTemperatureTransition
 
; HighTemperature
 
; HighTemperatureTransition
 
; Update
 
; Graphics
 
These last two can be used to set the update functions or graphics functions. Use a function as the value of the property to set. They are not included in the tables created with elements.element, and the functions can't be returned with elements.property either. This means copying all of an elements properties using elements.element will not set these for the new element.
 
  
 
== Constants ==
 
== Constants ==
Any of these constants can be accessed with elements.<constant name here>
 
  
=== Element identifiers ===
+
<span id="elements.group_pt_iname"></span>
All of the default element identifiers are prefixed with <code>DEFAUL_PT_</code>, for example, the identifier for WATR is <code>DEFAULT_PT_WATR</code>. Do not assume all elements identifiers are the same as their names, TNT has the identifier BANG, for example. To find an elements identifier, you can check the source file for any given element in <tt>src/simulation/elements/</tt>
+
=== elements.[group]_PT_[iname] ===
  
=== States ===
+
<pre>watrNumber = elements.DEFAULT_PT_WATR</pre>
There are just 3 constants for element state (Used in the "State" property of elements)
+
There are two ways to refer to elements: element numbers and element identifiers, which are strings of the form <code>&quot;[group]_PT_[iname]&quot;</code>. Both <code>[group]</code> and <code>[iname]</code> can be any string without underscores (<code>_</code>). All built-in elements are in group <code>DEFAULT</code>. For example, the identifier of WATR is <code>&quot;DEFAULT_PT_WATR&quot;</code>, because WATR belongs to the group <code>DEFAULT</code> and its internal name is <code>WATR</code>. This is different from the [[#elements.property|<code>Name</code> property]].
; ST_NONE
+
 
: Used by some "unusual" elements such as Photons
+
These constants map element identifiers to element numbers. The element number of WATR is <code>2</code>, so <code>elements.DEFAULT_PT_WATR</code> is also <code>2</code>.
; ST_SOLID
+
 
; ST_LIQUID
+
=== Properties ===
; ST_GAS
+
 
These values do not alter the physical properties of elements, but instead are used internally for identification. Powders use the solid state.
+
These constants should be used when setting "Properties". More info on the properties can be found here: [[Element_Properties#.Property_Constants]]
  
=== Types ===
 
These are used in the element "Properties" property and can alter some behavior (such as interaction with portals, walls, etc)
 
 
; TYPE_PART
 
; TYPE_PART
: Describes particulate elements such as powders
 
 
; TYPE_LIQUID
 
; TYPE_LIQUID
 
; TYPE_GAS
 
; TYPE_GAS
 
; TYPE_SOLID
 
; TYPE_SOLID
 
; TYPE_ENERGY
 
; TYPE_ENERGY
: Energy type particles may pass through others and won't create black holes when stacked.
 
 
=== Properties ===
 
 
; PROP_CONDUCTS
 
; PROP_CONDUCTS
: Allows an element to automatically conduct SPRK, requires PROP_LIFE_DEC
 
 
; PROP_BLACK
 
; PROP_BLACK
: Elements with this property absorb photons of any color
 
 
; PROP_NEUTPENETRATE
 
; PROP_NEUTPENETRATE
: Elements with this property allow neutrons to go through it
 
 
; PROP_NEUTABSORB
 
; PROP_NEUTABSORB
: Element will absorb neutrons
 
 
; PROP_NEUTPASS
 
; PROP_NEUTPASS
: Element can be displaced by neutrons (observe behavior of wood with neutrons to see)
 
 
; PROP_DEADLY
 
; PROP_DEADLY
: Element will kill stickmen and fighters
 
 
; PROP_HOT_GLOW
 
; PROP_HOT_GLOW
: Element will glow red when it approaches it's melting point.
 
 
; PROP_LIFE
 
; PROP_LIFE
: Unused
 
 
; PROP_RADIOACTIVE
 
; PROP_RADIOACTIVE
: Unused
 
 
; PROP_LIFE_DEC
 
; PROP_LIFE_DEC
: The "life" property of particles will be reduced by 1 every frame
 
 
; PROP_LIFE_KILL
 
; PROP_LIFE_KILL
: Particles will be destroyed when the "life" property is less than or equal to zero
 
 
; PROP_LIFE_KILL_DEC
 
; PROP_LIFE_KILL_DEC
: When used with PROP_LIFE_DEC, particles will be destroyed when the "life" property is decremented to 0. If already at 0 it will be fine.
 
 
; PROP_SPARKSETTLE
 
; PROP_SPARKSETTLE
: Allows sparks/embers to contact without being destroyed
 
 
; PROP_NOAMBHEAT
 
; PROP_NOAMBHEAT
: Prevents particles from exchanging heat with the air when ambient heat is enabled.
+
; PROP_DRAWONCTYPE
 +
; PROP_NOCTYPEDRAW
  
 
=== Menu sections ===
 
=== Menu sections ===
These are used for the menusection property
+
These are used for the MenuSection property
 +
 
 
; SC_WALL
 
; SC_WALL
 
; SC_ELEC
 
; SC_ELEC
Line 171: Line 160:
 
; SC_TOOL
 
; SC_TOOL
 
; SC_DECO
 
; SC_DECO
SC_CRACKER and SC_CRACKER2 are not accessible from lua or in the game, but have id numbers of 15 and 16
 
 
[[Category:Lua]]
 

Latest revision as of 03:59, 13 March 2024

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.

Methods

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.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

For more information on what properties there are to use in elements.property, and how to use them, see this page: Element_Properties.

When working with the "MenuSection" or the "Properties" property, use one of the provided constants.

The "Identifier" property is read-only and cannot be set.

Several event callback functions are implemented, such as "Update" and "Graphics". To set these, use a function for propValue. They are not included in the tables created with elements.element, and the functions can't be returned with elements.property either. This means copying all of an elements properties using elements.element will not set event callbacks for the new element. For help on creating these, see Element_Properties#Callback_functions.

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 if elemNumber 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 element name 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.

Constants

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.

Properties

These constants should be used when setting "Properties". More info on the properties can be found here: Element_Properties#.Property_Constants

TYPE_PART
TYPE_LIQUID
TYPE_GAS
TYPE_SOLID
TYPE_ENERGY
PROP_CONDUCTS
PROP_BLACK
PROP_NEUTPENETRATE
PROP_NEUTABSORB
PROP_NEUTPASS
PROP_DEADLY
PROP_HOT_GLOW
PROP_LIFE
PROP_RADIOACTIVE
PROP_LIFE_DEC
PROP_LIFE_KILL
PROP_LIFE_KILL_DEC
PROP_SPARKSETTLE
PROP_NOAMBHEAT
PROP_DRAWONCTYPE
PROP_NOCTYPEDRAW

Menu sections

These are used for the MenuSection property

SC_WALL
SC_ELEC
SC_POWERED
SC_SENSOR
SC_FORCE
SC_EXPLOSIVE
SC_GAS
SC_LIQUID
SC_POWDERS
SC_SOLIDS
SC_NUCLEAR
SC_SPECIAL
SC_LIFE
SC_TOOL
SC_DECO