Difference between revisions of "Lua API:Elements"

From The Powder Toy
Jump to: navigation, search
m (Lua category)
(change a few things to make it easier to understand, add in a list of the properties you can set)
Line 1: Line 1:
The Elements API Methods and constants for modifying and creating elements.
+
The Elements API Methods and constants for modifying and creating elements. If you want to add an update function of graphics function, see the Legacy API, specifically the functions tpt.element_func and tpt.graphics_func
  
 
== Methods ==
 
== Methods ==
Line 5: Line 5:
 
=== elements.allocate ===  
 
=== elements.allocate ===  
 
  number elements.allocate(string group, string name)
 
  number elements.allocate(string group, string name)
Creates a new element where group is the name of the mod or script and name is the name of the element, combined, they must make a unique identifier in the form GROUP_PT_NAME (for example, elements.allocate("mymod", "virus") would create the identifier MYMOD_PT_VIRUS). The return value is the ID of the new element. this function also creates a constant value identifier in the elements table (elements.MYMOD_PT_VIRUS is defined as the elements new ID)
+
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.
 +
 
 +
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.
 +
 
 +
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.
  
 
=== elements.free ===  
 
=== elements.free ===  
 
  nil elements.free(number elementID)
 
  nil elements.free(number elementID)
Free a previously allocated element to be used later, elementID must be a non-default element (i.e you cannot free the default WATR element)
+
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 ===
 
=== elements.loadDefault ===
 
  nil elements.loadDefault()
 
  nil elements.loadDefault()
Resets all elements to the original state
+
Resets all elements to the original state. This will also erase any elements created with any scripts, only the default elements will be available.
  
 
  nil elements.loadDefault(number elementID)
 
  nil elements.loadDefault(number elementID)
Reset an element to its original state before modification
+
Reset an element to its original state before it was modified
  
 
=== elements.element ===
 
=== elements.element ===
 
  table elements.element(number elementID)
 
  table elements.element(number elementID)
Returns a table populated with all of an elements properties (Name, Description, etc)
+
Returns a table containing all of an element's properties (Name, Description, etc)
  
 
  nil elements.element(number elementID, table properties)
 
  nil elements.element(number elementID, table properties)
Reads properties from the given table onto the element.
+
Sets the properties from the given table onto the element.
  
 
These two functions are useful for copying or templating from already present elements, for example
 
These two functions are useful for copying or templating from already present elements, for example
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
local myNewElement = elements.allocate("wiki", "expl")
 
local myNewElement = elements.allocate("wiki", "expl")
elements.element(elements.WIKI_PT_EXPL, elements.element(elements.DEFAULT_PT_WATR))
+
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, "Name", "EXPL")
 
elements.property(elements.WIKI_PT_EXPL, "Description", "This is an example element from the Wiki")
 
elements.property(elements.WIKI_PT_EXPL, "Description", "This is an example element from the Wiki")
Line 40: Line 44:
 
  nil elements.property(number elementID, string property, object value)
 
  nil elements.property(number elementID, string property, object value)
 
Sets the value of an element property
 
Sets the value of an element property
 +
 +
== Properties ==
 +
After creating an element, you can modify any of these properties. Be sure to set Name, Description, and MenuVisible at least.
 +
 +
; Name
 +
; Colour
 +
; Color
 +
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
 +
; AirLoss
 +
; Loss
 +
; Collision
 +
; Gravity
 +
; Diffusion
 +
; HotAir
 +
; Falldown
 +
0 = solid, gas, or energy particle, 1 = powder, 2 = liquid
 +
; Flammable
 +
; Explosive
 +
0 = does not explode, 1 = when touching fire, 2 = when touching fire or when pressure > 2.5
 +
; Meltable
 +
; Hardness
 +
; Weight
 +
; Temperature
 +
starting temperature, in Kelvin
 +
; HeatConduct
 +
; 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.
 +
; 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 element.property either. This means copying all of an elements properties using element.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 ===
 
=== Element identifiers ===
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>
+
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>
  
=== State ===
+
=== States ===
 
There are just 3 constants for element state (Used in the "State" property of elements)
 
There are just 3 constants for element state (Used in the "State" property of elements)
 
; ST_NONE
 
; ST_NONE
Line 53: Line 105:
 
; ST_LIQUID
 
; ST_LIQUID
 
; ST_GAS
 
; ST_GAS
These values do not alter the physical properties of elements, but instead are used internally for identification
+
These values do not alter the physical properties of elements, but instead are used internally for identification. Powders use the solid state.
  
=== Type ===  
+
=== Types ===  
These are used in the element "Properties" property and can alter some behaviour (such as interaction with portals, walls, etc)
+
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
 
: Describes particulate elements such as powders
Line 68: Line 120:
 
=== Properties ===
 
=== Properties ===
 
; PROP_CONDUCTS
 
; PROP_CONDUCTS
: Automatic behaviour, allows an element to conduct SPRK, requires PROP_LIFE_DEC
+
: Allows an element to automatically conduct SPRK, requires PROP_LIFE_DEC
 
; PROP_BLACK
 
; PROP_BLACK
: Elements with this property absorb photons of any colour
+
: Elements with this property absorb photons of any color
 
; PROP_NEUTPENETRATE
 
; PROP_NEUTPENETRATE
: Elements with this property allow neutrons to penetrate
+
: Elements with this property allow neutrons to go through it
 
; PROP_NEUTABSORB
 
; PROP_NEUTABSORB
 
: Element will absorb neutrons
 
: Element will absorb neutrons
 
; PROP_NEUTPASS
 
; PROP_NEUTPASS
: Element can be displaced by neutrons (observe behaviour of wood with neutrons)
+
: Element can be displaced by neutrons (observe behavior of wood with neutrons to see)
 
; PROP_DEADLY
 
; PROP_DEADLY
 
: Element will kill stickmen and fighters
 
: Element will kill stickmen and fighters
Line 90: Line 142:
 
: Particles will be destroyed when the "life" property is less than or equal to zero
 
: 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
+
: 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
 
: Allows sparks/embers to contact without being destroyed
Line 101: Line 153:
 
; SC_ELEC
 
; SC_ELEC
 
; SC_POWERED
 
; SC_POWERED
 +
; SC_SENSOR
 
; SC_FORCE
 
; SC_FORCE
 
; SC_EXPLOSIVE
 
; SC_EXPLOSIVE
Line 112: Line 165:
 
; 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 14 and 15
+
SC_CRACKER and SC_CRACKER2 are not accessible from lua or in the game, but have id numbers of 15 and 16
  
 
[[Category:Lua]]
 
[[Category:Lua]]

Revision as of 01:53, 5 March 2013

The Elements API Methods and constants for modifying and creating elements. If you want to add an update function of graphics function, see the Legacy API, specifically the functions tpt.element_func and tpt.graphics_func

Methods

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.

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.

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.

elements.free

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

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.

nil elements.loadDefault(number elementID)

Reset an element to its original state before it was modified

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)

Sets the properties from the given table onto the element.

These two functions are useful for copying or templating from already present elements, for example

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")

In this example, the element properties for our new element (EXPL) are copied from WATR

elements.property

object elements.property(number elementID, string property)

Gets the value of an element property

nil elements.property(number elementID, string property, object value)

Sets the value of an element property

Properties

After creating an element, you can modify any of these properties. Be sure to set Name, Description, and MenuVisible at least.

Name
Colour
Color

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
AirLoss
Loss
Collision
Gravity
Diffusion
HotAir
Falldown

0 = solid, gas, or energy particle, 1 = powder, 2 = liquid

Flammable
Explosive

0 = does not explode, 1 = when touching fire, 2 = when touching fire or when pressure > 2.5

Meltable
Hardness
Weight
Temperature

starting temperature, in Kelvin

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

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 element.property either. This means copying all of an elements properties using element.element will not set these for the new element.

Constants

Any of these constants can be accessed with elements.<constant name here>

Element identifiers

All of the default element identifiers are prefixed with DEFAUL_PT_, for example, the identifier for WATR is DEFAULT_PT_WATR. 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 src/simulation/elements/

States

There are just 3 constants for element state (Used in the "State" property of elements)

ST_NONE
Used by some "unusual" elements such as Photons
ST_SOLID
ST_LIQUID
ST_GAS

These values do not alter the physical properties of elements, but instead are used internally for identification. Powders use the solid state.

Types

These are used in the element "Properties" property and can alter some behavior (such as interaction with portals, walls, etc)

TYPE_PART
Describes particulate elements such as powders
TYPE_LIQUID
TYPE_GAS
TYPE_SOLID
TYPE_ENERGY
Energy type particles may pass through others and won't create black holes when stacked.

These values do not alter the physical properties of elements, but instead are used internally for identification

Properties

PROP_CONDUCTS
Allows an element to automatically conduct SPRK, requires PROP_LIFE_DEC
PROP_BLACK
Elements with this property absorb photons of any color
PROP_NEUTPENETRATE
Elements with this property allow neutrons to go through it
PROP_NEUTABSORB
Element will absorb neutrons
PROP_NEUTPASS
Element can be displaced by neutrons (observe behavior of wood with neutrons to see)
PROP_DEADLY
Element will kill stickmen and fighters
PROP_HOT_GLOW
Element will glow red when it approaches it's melting point.
PROP_LIFE
Unused
PROP_RADIOACTIVE
Unused
PROP_LIFE_DEC
The "life" property of particles will be reduced by 1 every frame
PROP_LIFE_KILL
Particles will be destroyed when the "life" property is less than or equal to zero
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
Allows sparks/embers to contact without being destroyed
PROP_NOAMBHEAT
Prevents particles from exchanging heat with the air when ambient heat is enabled.

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

SC_CRACKER and SC_CRACKER2 are not accessible from lua or in the game, but have id numbers of 15 and 16