Difference between revisions of "Lua"

From The Powder Toy
Jump to: navigation, search
(Created page with "======= The Lua Console ======= You may open the Lua Console by hitting the ''[`]'' key. (Also known as the tilde ''[~]'' key) [[http://img97.imageshack.us/img97/6811/tildekey1.j...")
 
(talk)
Line 1: Line 1:
======= The Lua Console =======
+
<h1> = The Lua Console = </h2>
You may open the Lua Console by hitting the ''[`]'' key. (Also known as the tilde ''[~]'' key) [[http://img97.imageshack.us/img97/6811/tildekey1.jpg|click here to view key]]
+
You may open the Lua Console by hitting the ''[`]'' key. (Also known as the tilde ''[~]'' key) [http://img97.imageshack.us/img97/6811/tildekey1.jpg| click here to view key]
  
 
Or ''[¬]''.
 
Or ''[¬]''.
Line 6: Line 6:
 
----
 
----
  
**BEFORE YOU FRET, YOU CAN STILL USE OLD COMMANDS. JUST PLACE AN '!' (without quotes) BEFORE THE COMMAND IN THE CONSOLE**
+
'''BEFORE YOU FRET, YOU CAN STILL USE OLD COMMANDS. JUST PLACE AN '!' (without quotes) BEFORE THE COMMAND IN THE CONSOLE'''
  
 
''!set type dust metl''  
 
''!set type dust metl''  
Line 12: Line 12:
 
The equivalent command in TPT's Lua is ''tpt.set_property("type", "metl", "dust")'' (see https://powdertoy.co.uk/Wiki.html?id=lua#tptset_property )
 
The equivalent command in TPT's Lua is ''tpt.set_property("type", "metl", "dust")'' (see https://powdertoy.co.uk/Wiki.html?id=lua#tptset_property )
  
But, try to learn the Lua interface. It may be a lot more useful to you than you think. //(This wiki does not teach you the Lua language. This is simply an API. But, you may research Lua on your own http://www.lua.org/manual/5.1/ )//
+
But, try to learn the Lua interface. It may be a lot more useful to you than you think. ''(This wiki does not teach you the Lua language. This is simply an API. But, you may research Lua on your own http:''www.lua.org/manual/5.1/ )//
  
 
----
 
----
Line 20: Line 20:
 
With the ability to script with Lua, users are now able to create simple modifications to the game without editing source code easily.
 
With the ability to script with Lua, users are now able to create simple modifications to the game without editing source code easily.
  
======= Quick Introduction to Scripts =======
+
<h1> = Quick Introduction to Scripts = </h2>
 +
<h2> How to run a script </h3>
 +
To run a script, place the .lua file in the root folder of powder toy (where the powder toy executable is located). Open the console (see below) and type in '''dofile("filename.lua")''' where "filename.lua" is the the script you wish to run.
  
=====How to run a script=====
+
<h2> Variable Types </h3>
To run a script, place the .lua file in the root folder of powder toy (where the powder toy executable is located). Open the console (see below) and type in **dofile("filename.lua")** where "filename.lua" is the the script you wish to run.
+
<ins>The different variable types are:</ins>
  
=====Variable Types=====
 
__The different variable types are:__
 
  
 +
* ''''string''''
  
  * ''//string//''
+
Represents a word, character, or phrase. ''''string'''' variables must begin and end with double-quotes. (''"'')
  
Represents a word, character, or phrase. ''//string//'' variables must begin and end with double-quotes. (''"'')
+
'''Example:''' ''local str = "This is a string variable."''
 
 
**Example:** ''local str = "This is a string variable."''
 
  
 
----
 
----
  
  * ''//number//''
+
* ''''number''''
  
 
Represents a number. Numbers may be floating-point or fixed-point types (meaning they may have decimals [floating-point] or may be numbers with no fraction [fixed-point].)
 
Represents a number. Numbers may be floating-point or fixed-point types (meaning they may have decimals [floating-point] or may be numbers with no fraction [fixed-point].)
  
**Example:** ''local num = 1234''
+
'''Example:''' ''local num = 1234''
  
 
----
 
----
  
  * ''//boolean//''
+
* ''''boolean''''
  
 
Represents a switch that is either on (true) or off (false).
 
Represents a switch that is either on (true) or off (false).
  
**Example:** ''local bool = true''
+
'''Example:''' ''local bool = true''
 
----
 
----
  
  * ''//function//''
+
* ''''function''''
  
 
Represents a method or function in lua.
 
Represents a method or function in lua.
  
**Example:** ''function func(arguments)''
+
'''Example:''' ''function func(arguments)''
  
**NOTE:** Functions that use a ''//function//'' as an argument do not include the () at the end. Only the name must be given.
+
'''NOTE:''' Functions that use a ''''function'''' as an argument do not include the () at the end. Only the name must be given.
  
 
----
 
----
  
  * ''//table//''
+
* ''''table''''
  
 
Tables are a group of variables. Tables can also act as an array. Tables can carry any type of variables, and can even mix different types of variables.
 
Tables are a group of variables. Tables can also act as an array. Tables can carry any type of variables, and can even mix different types of variables.
  
**Example:** ''local tbl = {}''
+
'''Example:''' ''local tbl = {}''
  
 
This creates a blank table.
 
This creates a blank table.
Line 72: Line 71:
  
  
**Example:** ''local tbl = {1, 2, 3, "string"}''
+
'''Example:''' ''local tbl = {1, 2, 3, "string"}''
  
 
Creates a table with the elements 1, 2, 3 and "string".
 
Creates a table with the elements 1, 2, 3 and "string".
Line 86: Line 85:
  
  
**Example:**
+
'''Example:'''
  
 
local tbl = {}
 
local tbl = {}
Line 105: Line 104:
 
----
 
----
  
  * ''//object//''
+
* ''''object''''
  
Acts as a wildcard. It can be represented as any of the above, but this does **not** mean it **can** be any type, it depends on the function.
+
Acts as a wildcard. It can be represented as any of the above, but this does '''not''' mean it '''can''' be any type, it depends on the function.
  
=====General Arguments=====
+
<h2> General Arguments </h3>
__The arguments given here are general, here are some examples:__
+
<ins>The arguments given here are general, here are some examples:</ins>
  
  
''//string// **property**''
+
''''string'' '''property'''''
  
This is to specify what property of a particle to change. REMINDER: This is a ''//string//'' variable, meaning it is a word that must begin and end with double-quotes. (''"'')
+
This is to specify what property of a particle to change. REMINDER: This is a ''''string'''' variable, meaning it is a word that must begin and end with double-quotes. (''"'')
 
The different available properties are:
 
The different available properties are:
  
Line 140: Line 139:
 
----
 
----
  
''//object// **value**''
+
''''object'' '''value'''''
  
What you are setting the current property to. Since this is an **object** variable, it's type will depend on the function.
+
What you are setting the current property to. Since this is an '''object''' variable, it's type will depend on the function.
  
 
----
 
----
  
''//string// **type**''
+
''''string'' '''type'''''
  
A ''//string//'' which is the code-name of an element "dust" "watr" "spng".
+
A ''''string'''' which is the code-name of an element "dust" "watr" "spng".
  
 
----
 
----
''//number// **index**''
+
''''number'' '''index'''''
  
 
A specific particle number by it's index.
 
A specific particle number by it's index.
Line 157: Line 156:
 
----
 
----
  
''//number// **state**''
+
''''number'' '''state'''''
  
 
0 or 1, 0 for off, 1 for on.
 
0 or 1, 0 for off, 1 for on.
Line 163: Line 162:
 
----
 
----
  
''//number// **Width**'' and ''//number// **Height**''
+
''''number'' '''Width''''' and ''''number'' '''Height'''''
  
 
Represent a rectangle.
 
Represent a rectangle.
Line 169: Line 168:
 
----
 
----
  
''//number// **x**'' and ''//number// **y**''
+
''''number'' '''x''''' and ''''number'' '''y'''''
  
 
Represents a 2-Dimensional coordinate.
 
Represents a 2-Dimensional coordinate.
Line 175: Line 174:
 
----
 
----
  
''//string// **text**''
+
''''string'' '''text'''''
  
 
Represents text
 
Represents text
Line 181: Line 180:
 
----
 
----
  
''//number// **toggle**''
+
''''number'' '''toggle'''''
  
 
Represents either 1 for on or 0 for off
 
Represents either 1 for on or 0 for off
Line 187: Line 186:
 
----
 
----
  
''//number// **menu**''
+
''''number'' '''menu'''''
  
 
Represents a menu, eg 1 = Walls, 2 = Electronics
 
Represents a menu, eg 1 = Walls, 2 = Electronics
Line 193: Line 192:
 
----
 
----
  
''//number// **display**''
+
''''number'' '''display'''''
  
 
Represents a display mode, eg 4 = Fire, 6 = Heat
 
Represents a display mode, eg 4 = Fire, 6 = Heat
Line 199: Line 198:
 
----
 
----
  
====== Lua API ======
+
<h1>  Lua API </h2>
**The Powder Toy exposes the following methods to the Lua API:**
+
'''The Powder Toy exposes the following methods to the Lua API:'''
===== Game =====
+
<h2>  Game </h3>
====tpt.set_pause====
+
<h3> tpt.set_pause </h4>
 
Set the paused state of the game
 
Set the paused state of the game
==tpt.set_pause(number state)==
+
<h5> tpt.set_pause(number state) </h6>
====tpt.set_console====
+
<h3> tpt.set_console </h4>
 
Set the visibility state of the console
 
Set the visibility state of the console
==tpt.set_console(number state)==
+
<h5> tpt.set_console(number state) </h6>
====tpt.set_shortcuts====
+
<h3> tpt.set_shortcuts </h4>
 
Set whether keyboard shortcuts are enabled
 
Set whether keyboard shortcuts are enabled
==tpt.set_shortcuts(number state)==
+
<h5> tpt.set_shortcuts(number state) </h6>
 
+
<h3> tpt.set_gravity </h4>
 
 
====tpt.set_gravity====
 
 
Sets values on the gravity map, 3 overloads
 
Sets values on the gravity map, 3 overloads
==tpt.set_gravity(number x, number y)==
+
<h5> tpt.set_gravity(number x, number y) </h6>
==tpt.set_gravity(number x, number y, number width, number height)==
+
<h5> tpt.set_gravity(number x, number y, number width, number height) </h6>
==tpt.set_gravity(number x, number y, number width, number height, number value)==
+
<h5> tpt.set_gravity(number x, number y, number width, number height, number value) </h6>
 
+
<h3> tpt.reset_gravity_field </h4>
====tpt.reset_gravity_field====
 
 
Resets regions on the gravity velocity map, 2 overloads
 
Resets regions on the gravity velocity map, 2 overloads
==tpt.reset_gravity_field(number x, number y)==
+
<h5> tpt.reset_gravity_field(number x, number y) </h6>
==tpt.reset_gravity_field(number x, number y, number width, number height)==
+
<h5> tpt.reset_gravity_field(number x, number y, number width, number height) </h6>
 
+
<h3> tpt.set_pressure </h4>
====tpt.set_pressure====
 
 
Sets values on the pressure map, 3 overloads
 
Sets values on the pressure map, 3 overloads
==tpt.set_pressure(number x, number y)==
+
<h5> tpt.set_pressure(number x, number y) </h6>
==tpt.set_pressure(number x, number y, number width, number height)==
+
<h5> tpt.set_pressure(number x, number y, number width, number height) </h6>
==tpt.set_pressure(number x, number y, number width, number height, number value)==
+
<h5> tpt.set_pressure(number x, number y, number width, number height, number value) </h6>
====tpt.reset_velocity====
+
<h3> tpt.reset_velocity </h4>
 
Resets regions on the velocity map, 2 overloads
 
Resets regions on the velocity map, 2 overloads
==tpt.reset_velocity(number x, number y)==
+
<h5> tpt.reset_velocity(number x, number y) </h6>
==tpt.reset_velocity(number x, number y, number width, number height)==
+
<h5> tpt.reset_velocity(number x, number y, number width, number height) </h6>
 
+
<h3> tpt.hud </h4>
====tpt.hud====
 
 
Toggles HUD State
 
Toggles HUD State
==tpt.hud(number toggle)==
+
<h5> tpt.hud(number toggle) </h6>
 
+
<h3> tpt.newtonian_gravity </h4>
====tpt.newtonian_gravity====
 
 
Toggles Newtonian Gravity State
 
Toggles Newtonian Gravity State
==tpt.newtonian_gravity(number toggle)==
+
<h5> tpt.newtonian_gravity(number toggle) </h6>
 
+
<h3> tpt.ambient_heat </h4>
====tpt.ambient_heat====
 
 
Toggles Ambient Heat State
 
Toggles Ambient Heat State
==tpt.ambient_heat(number toggle)==
+
<h5> tpt.ambient_heat(number toggle) </h6>
 
+
<h3> tpt.decorations_enable </h4>
====tpt.decorations_enable====
 
 
Toggles visibility of decorations
 
Toggles visibility of decorations
==tpt.decorations_enable(number toggle)==
+
<h5> tpt.decorations_enable(number toggle) </h6>
 
+
<h3> tpt.heat </h4>
====tpt.heat====
 
 
Toggles Heat Simulation State
 
Toggles Heat Simulation State
==tpt.heat(number toggle)==
+
<h5> tpt.heat(number toggle) </h6>
 
+
<h3> tpt.active_menu </h4>
====tpt.active_menu====
 
 
Changes activated menu
 
Changes activated menu
==tpt.active_menu(number menu)==
+
<h5> tpt.active_menu(number menu) </h6>
 
+
<h3> tpt.display_mode </h4>
====tpt.display_mode====
 
 
Changes activated display mode
 
Changes activated display mode
==tpt.display_mode(number display)==
+
<h5> tpt.display_mode(number display) </h6>
 
+
<h3> tpt.setfpscap </h4>
====tpt.setfpscap====
 
 
Changes the maximum FPS.
 
Changes the maximum FPS.
==tpt.setfpscap(number fpscap)==
+
<h5> tpt.setfpscap(number fpscap) </h6>
 
+
<h3> tpt.setfire </h4>
====tpt.setfire====
 
 
Changes the strength of the games glowing effects. tpt.setfire(1) is default.
 
Changes the strength of the games glowing effects. tpt.setfire(1) is default.
==tpt.setfire(number strength)==
+
<h5> tpt.setfire(number strength) </h6>
 
+
<h3> tpt.setwindowsize </h4>
====tpt.setwindowsize====
 
 
Changes the window settings. Scale is either 1 or 2. Returns a number, probably to indicate success.
 
Changes the window settings. Scale is either 1 or 2. Returns a number, probably to indicate success.
==tpt.setwindowsize(number scale, toggle fullscreen)==
+
<h5> tpt.setwindowsize(number scale, toggle fullscreen) </h6>
 
+
<h2>  Particles </h3>
===== Particles =====
+
<h3> tpt.reset_spark </h4>
====tpt.reset_spark====
 
 
Removes electrified wires from the simulation, resetting to the original material
 
Removes electrified wires from the simulation, resetting to the original material
==tpt.reset_spark()==
+
<h5> tpt.reset_spark() </h6>
====tpt.set_property====
+
<h3> tpt.set_property </h4>
 
Set various properties of particles for given criteria, 8 overloads
 
Set various properties of particles for given criteria, 8 overloads
==tpt.set_property(string property, object value)==
+
<h5> tpt.set_property(string property, object value) </h6>
==tpt.set_property(string property, object value, string type)==
+
<h5> tpt.set_property(string property, object value, string type) </h6>
==tpt.set_property(string property, object value, number index)==
+
<h5> tpt.set_property(string property, object value, number index) </h6>
==tpt.set_property(string property, object value, number index, string type)==
+
<h5> tpt.set_property(string property, object value, number index, string type) </h6>
==tpt.set_property(string property, object value, number x, number y)==
+
<h5> tpt.set_property(string property, object value, number x, number y) </h6>
==tpt.set_property(string property, object value, number x, number y, string type)==
+
<h5> tpt.set_property(string property, object value, number x, number y, string type) </h6>
==tpt.set_property(string property, object value, number x, number y, number width, number height)==
+
<h5> tpt.set_property(string property, object value, number x, number y, number width, number height) </h6>
==tpt.set_property(string property, object value, number x, number y, number width, number height, string type)==
+
<h5> tpt.set_property(string property, object value, number x, number y, number width, number height, string type) </h6>
 
+
<h3> object tpt.get_property </h4>
====object tpt.get_property====
+
Get various properties of a particle. Returns an ''object''
Get various properties of a particle. Returns an //object//
+
<h5> tpt.get_property(string property, number index) </h6>
==tpt.get_property(string property, number index)==
+
<h5> tpt.get_property(string property, number x, number y) </h6>
==tpt.get_property(string property, number x, number y)==
+
<h3> tpt.create </h4>
====tpt.create====
 
 
Create a particle at location.
 
Create a particle at location.
==tpt.create(number x, number y, string type)==
+
<h5> tpt.create(number x, number y, string type) </h6>
====tpt.delete====
+
<h3> tpt.delete </h4>
 
Delete a specific particle, or location.
 
Delete a specific particle, or location.
==tpt.delete(number index)==
+
<h5> tpt.delete(number index) </h6>
==tpt.delete(number x, number y)==
+
<h5> tpt.delete(number x, number y) </h6>
 
+
<h3> tpt.start_getPartIndex </h4>
====tpt.start_getPartIndex====
 
 
Start the iterator for receiving all indecies of the particles. (Used to help get particle indecies, see tpt.next_getPartIndex)
 
Start the iterator for receiving all indecies of the particles. (Used to help get particle indecies, see tpt.next_getPartIndex)
==tpt.start_getPartIndex()==
+
<h5> tpt.start_getPartIndex() </h6>
====boolean tpt.next_getPartIndex====
+
<h3> boolean tpt.next_getPartIndex </h4>
 
Jump to the next available particle index. Returns false if the iterator has reached the end of all particle indecies. Returns true if a new index was available. (Used to help get particle indecies, see tpt.getPartIndex)
 
Jump to the next available particle index. Returns false if the iterator has reached the end of all particle indecies. Returns true if a new index was available. (Used to help get particle indecies, see tpt.getPartIndex)
==tpt.next_getPartIndex()==
+
<h5> tpt.next_getPartIndex() </h6>
====number tpt.getPartIndex====
+
<h3> number tpt.getPartIndex </h4>
 
Get the current index iterator.
 
Get the current index iterator.
==tpt.getPartIndex()==
+
<h5> tpt.getPartIndex() </h6>
====number tpt.get_numOfParts====
+
<h3> number tpt.get_numOfParts </h4>
 
Returns the number of particles currently on the screen.
 
Returns the number of particles currently on the screen.
==tpt.get_numOfParts()==
+
<h5> tpt.get_numOfParts() </h6>
 
+
<h2>  Drawing </h3>
===== Drawing =====
+
<h3> number tpt.textwidth </h4>
====number tpt.textwidth====
+
Measures (in pixels) the width of a given ''string''. Returns a ''number''
Measures (in pixels) the width of a given //string//. Returns a //number//
+
<h5> tpt.textwidth(string text) </h6>
==tpt.textwidth(string text)==
+
<h3> tpt.drawtext </h4>
 
 
====tpt.drawtext====
 
 
Draw text to the screen (for one frame, only useful in scripts), 3 overloads
 
Draw text to the screen (for one frame, only useful in scripts), 3 overloads
==tpt.drawtext(number x, number y, string text)==
+
<h5> tpt.drawtext(number x, number y, string text) </h6>
==tpt.drawtext(number x, number y, string text, number red, number green, number blue)==
+
<h5> tpt.drawtext(number x, number y, string text, number red, number green, number blue) </h6>
==tpt.drawtext(number x, number y, string text, number red, number green, number blue, number alpha)==
+
<h5> tpt.drawtext(number x, number y, string text, number red, number green, number blue, number alpha) </h6>
 
+
<h3> tpt.drawpixel </h4>
====tpt.drawpixel====
 
 
Draws a pixel on the screen (for one frame, only useful in scripts), 3 overloads
 
Draws a pixel on the screen (for one frame, only useful in scripts), 3 overloads
==tpt.drawpixel(number x, number y)==
+
<h5> tpt.drawpixel(number x, number y) </h6>
==tpt.drawpixel(number x, number y, number red, number green, number blue)==
+
<h5> tpt.drawpixel(number x, number y, number red, number green, number blue) </h6>
==tpt.drawpixel(number x, number y, number red, number green, number blue, number alpha)==
+
<h5> tpt.drawpixel(number x, number y, number red, number green, number blue, number alpha) </h6>
 
+
<h3> tpt.drawrect </h4>
====tpt.drawrect====
 
 
Draws a rectangle on the screen (for one frame, only useful in scripts), 3 overloads
 
Draws a rectangle on the screen (for one frame, only useful in scripts), 3 overloads
==tpt.drawrect(number x, number y, number width, number height)==
+
<h5> tpt.drawrect(number x, number y, number width, number height) </h6>
==tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue)==
+
<h5> tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue) </h6>
==tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)==
+
<h5> tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue, number alpha) </h6>
 
+
<h3> tpt.fillrect </h4>
====tpt.fillrect====
 
 
Draws a filled in rectangle on the screen (for one frame, only useful in scripts), 3 overloads
 
Draws a filled in rectangle on the screen (for one frame, only useful in scripts), 3 overloads
==tpt.fillrect(number x, number y, number width, number height)==
+
<h5> tpt.fillrect(number x, number y, number width, number height) </h6>
==tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue)==
+
<h5> tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue) </h6>
==tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)==
+
<h5> tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue, number alpha) </h6>
 
+
<h2>  Input/Output </h3>
===== Input/Output =====
+
<h3> tpt.log </h4>
====tpt.log====
 
 
Log a message to the console
 
Log a message to the console
==tpt.log(string text)==
+
<h5> tpt.log(string text) </h6>
====tpt.message_box====
+
<h3> tpt.message_box </h4>
 
Display an OK-Only message box with a title and message.
 
Display an OK-Only message box with a title and message.
==tpt.message_box(string title, string message)==
+
<h5> tpt.message_box(string title, string message) </h6>
====string tpt.input====
+
<h3> string tpt.input </h4>
 
Ask the user to input some text. Returns a string of what ever the user says. The argument "text" is pre-entered text (optional).
 
Ask the user to input some text. Returns a string of what ever the user says. The argument "text" is pre-entered text (optional).
==tpt.input(string title, string message)==
+
<h5> tpt.input(string title, string message) </h6>
==tpt.input(string title, string message, string text)==
+
<h5> tpt.input(string title, string message, string text) </h6>
 
+
<h2>  Events </h3>
===== Events =====
+
<h3> tpt.register_step </h4>
====tpt.register_step====
 
 
Register a function to be run on every frame
 
Register a function to be run on every frame
==tpt.register_step(function func)==
+
<h5> tpt.register_step(function func) </h6>
====tpt.unregister_step====
+
<h3> tpt.unregister_step </h4>
 
Unregister a previously registered function
 
Unregister a previously registered function
==tpt.unregister_step(function func)==
+
<h5> tpt.unregister_step(function func) </h6>
====tpt.register_mouseclick====
+
<h3> tpt.register_mouseclick </h4>
 
Register a function to be run every time the mouse clicks\\ \\  
 
Register a function to be run every time the mouse clicks\\ \\  
Passes: (//number mousex, number mousey, number button, number event//)
+
Passes: (''number mousex, number mousey, number button, number event'')
==tpt.register_mouseclick(function func)==
+
<h5> tpt.register_mouseclick(function func) </h6>
====tpt.unregister_mouseclick====
+
<h3> tpt.unregister_mouseclick </h4>
 
Unregister a previously registered function
 
Unregister a previously registered function
==tpt.unregister_mouseclick(function func)==
+
<h5> tpt.unregister_mouseclick(function func) </h6>
====tpt.register_keypress====
+
<h3> tpt.register_keypress </h4>
 
Register a function to be run every time a key is pressed\\ \\  
 
Register a function to be run every time a key is pressed\\ \\  
Passes: (//string key, number key, number modifier//)
+
Passes: (''string key, number key, number modifier'')
==tpt.register_keypress(function func)==
+
<h5> tpt.register_keypress(function func) </h6>
====tpt.unregister_keypress====
+
<h3> tpt.unregister_keypress </h4>
 
Unregister a previously registered function
 
Unregister a previously registered function
==tpt.unregister_keypress(function func)==
+
<h5> tpt.unregister_keypress(function func) </h6>
 
+
<h2>  Misc </h3>
===== Misc =====
+
<h3> string tpt.get_name </h4>
====string tpt.get_name====
+
Get the current username, returns a ''string''
Get the current username, returns a //string//
+
<h5> tpt.get_name() </h6>
==tpt.get_name()==
+
<h3> tpt.throw_error </h4>
 
 
====tpt.throw_error====
 
 
Displays an error message
 
Displays an error message
==tpt.throw_error(string text)==
+
<h5> tpt.throw_error(string text) </h6>
 
+
<h3> tpt.setdebug </h4>
====tpt.setdebug====
 
 
Sets the "debug mode". Only odd numbers seem to do anything for now.
 
Sets the "debug mode". Only odd numbers seem to do anything for now.
==tpt.setdebug(number mode)==
+
<h5> tpt.setdebug(number mode) </h6>
 
+
<h1> Mod's with Lua Functions </h2>
======Mod's with Lua Functions======
+
<h3> Me4502's Mod </h4>
 
+
<h4> NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER </h5>
====Me4502's Mod====
+
<h4> String Property Additions </h5>
===NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER===
 
===String Property Additions===
 
 
 
 
  "collision"
 
  "collision"
 
  "airdrag"
 
  "airdrag"
Line 411: Line 380:
 
  "actas"
 
  "actas"
  
===New Functions===
+
<h4> New Functions </h5>
==tpt.throw_error("")==
+
<h5> tpt.throw_error("") </h6>
==tpt.getscript("")==
+
<h5> tpt.getscript("") </h6>
 
For the following, 1 in the ()'s is on 0 is off
 
For the following, 1 in the ()'s is on 0 is off
==tpt.hud()==
+
<h5> tpt.hud() </h6>
==tpt.newtonian_gravity()==
+
<h5> tpt.newtonian_gravity() </h6>
==tpt.ambient_heat()==
+
<h5> tpt.ambient_heat() </h6>
==tpt.decorations_enable()==
+
<h5> tpt.decorations_enable() </h6>
==tpt.heat()==
+
<h5> tpt.heat() </h6>
 
For the following the number of the value you want goes into the ()'s
 
For the following the number of the value you want goes into the ()'s
==tpt.active_menu()==
+
<h5> tpt.active_menu() </h6>
==tpt.display_mode()==
+
<h5> tpt.display_mode() </h6>
==tpt.set_glow()==
+
<h5> tpt.set_glow() </h6>
 
+
<h1>  Simple Example Code </h2>
====== Simple Example Code ======
 
 
 
 
 
-- This line is a comment. Anything written after the -- is considered a Comment and will not be read by Lua.
 
-- This line is a comment. Anything written after the -- is considered a Comment and will not be read by Lua.
 
 

Revision as of 19:24, 28 September 2011

= The Lua Console = </h2> You may open the Lua Console by hitting the [`] key. (Also known as the tilde [~] key) click here to view key Or [¬].

BEFORE YOU FRET, YOU CAN STILL USE OLD COMMANDS. JUST PLACE AN '!' (without quotes) BEFORE THE COMMAND IN THE CONSOLE

!set type dust metl

The equivalent command in TPT's Lua is tpt.set_property("type", "metl", "dust") (see https://powdertoy.co.uk/Wiki.html?id=lua#tptset_property )

But, try to learn the Lua interface. It may be a lot more useful to you than you think. (This wiki does not teach you the Lua language. This is simply an API. But, you may research Lua on your own http:www.lua.org/manual/5.1/ )//


The new Lua Console now provides the ability to create scripts using Lua, a very simple scripting language.

With the ability to script with Lua, users are now able to create simple modifications to the game without editing source code easily.

<h1> = Quick Introduction to Scripts = </h2>

How to run a script </h3> To run a script, place the .lua file in the root folder of powder toy (where the powder toy executable is located). Open the console (see below) and type in dofile("filename.lua") where "filename.lua" is the the script you wish to run. <h2> Variable Types </h3> The different variable types are:
  • 'string'
Represents a word, character, or phrase. 'string' variables must begin and end with double-quotes. (") Example: local str = "This is a string variable."
  • 'number'

Represents a number. Numbers may be floating-point or fixed-point types (meaning they may have decimals [floating-point] or may be numbers with no fraction [fixed-point].)

Example: local num = 1234


  • 'boolean'

Represents a switch that is either on (true) or off (false).

Example: local bool = true


  • 'function'

Represents a method or function in lua.

Example: function func(arguments)

NOTE: Functions that use a 'function' as an argument do not include the () at the end. Only the name must be given.


  • 'table'

Tables are a group of variables. Tables can also act as an array. Tables can carry any type of variables, and can even mix different types of variables.

Example: local tbl = {}

This creates a blank table.


Example: local tbl = {1, 2, 3, "string"}

Creates a table with the elements 1, 2, 3 and "string".

You can call these variables in this manner:


tbl[1] --returns 1 tbl[2] --returns 2 tbl[3] --returns 3 tbl[4] --returns "string"


Example:

local tbl = {} tbl.x = 7 tbl.y = 5 tbl.str = "this is a string" tbl.sqr = function(v) return v * v end

Creates a table with the variables x, y, str, and the function sqr.

You can call these variables in this manner:

tbl.x --returns 7 tbl.y --returns 5 tbl.str --returns "this is a string" tbl.sqr(2) --returns 4 (remember this is a function that takes 'v' and squares it. )


  • 'object'

Acts as a wildcard. It can be represented as any of the above, but this does not mean it can be any type, it depends on the function.

<h2> General Arguments </h3> The arguments given here are general, here are some examples:


'string property

This is to specify what property of a particle to change. REMINDER: This is a 'string' variable, meaning it is a word that must begin and end with double-quotes. (") The different available properties are:

"type"

"life"

"ctype"

"temp"

"tmp"

"vx"

"vy"

"x"

"y"

Any other given strings will result with an invalid property error.


'object value

What you are setting the current property to. Since this is an object variable, it's type will depend on the function.


'string type

A 'string' which is the code-name of an element "dust" "watr" "spng".


'number index

A specific particle number by it's index.


'number state

0 or 1, 0 for off, 1 for on.


'number Width and 'number Height

Represent a rectangle.


'number x and 'number y

Represents a 2-Dimensional coordinate.


'string text

Represents text


'number toggle

Represents either 1 for on or 0 for off


'number menu

Represents a menu, eg 1 = Walls, 2 = Electronics


'number display

Represents a display mode, eg 4 = Fire, 6 = Heat


<h1> Lua API

The Powder Toy exposes the following methods to the Lua API:

Game </h3>

tpt.set_pause </h4> Set the paused state of the game

tpt.set_pause(number state) </h6> <h3> tpt.set_console </h4> Set the visibility state of the console <h5> tpt.set_console(number state) </h6> <h3> tpt.set_shortcuts </h4> Set whether keyboard shortcuts are enabled <h5> tpt.set_shortcuts(number state) </h6> <h3> tpt.set_gravity </h4> Sets values on the gravity map, 3 overloads <h5> tpt.set_gravity(number x, number y) </h6> <h5> tpt.set_gravity(number x, number y, number width, number height) </h6> <h5> tpt.set_gravity(number x, number y, number width, number height, number value) </h6> <h3> tpt.reset_gravity_field </h4> Resets regions on the gravity velocity map, 2 overloads <h5> tpt.reset_gravity_field(number x, number y) </h6> <h5> tpt.reset_gravity_field(number x, number y, number width, number height) </h6> <h3> tpt.set_pressure </h4> Sets values on the pressure map, 3 overloads <h5> tpt.set_pressure(number x, number y) </h6> <h5> tpt.set_pressure(number x, number y, number width, number height) </h6> <h5> tpt.set_pressure(number x, number y, number width, number height, number value) </h6> <h3> tpt.reset_velocity </h4> Resets regions on the velocity map, 2 overloads <h5> tpt.reset_velocity(number x, number y) </h6> <h5> tpt.reset_velocity(number x, number y, number width, number height) </h6> <h3> tpt.hud </h4> Toggles HUD State <h5> tpt.hud(number toggle) </h6> <h3> tpt.newtonian_gravity </h4> Toggles Newtonian Gravity State <h5> tpt.newtonian_gravity(number toggle) </h6> <h3> tpt.ambient_heat </h4> Toggles Ambient Heat State <h5> tpt.ambient_heat(number toggle) </h6> <h3> tpt.decorations_enable </h4> Toggles visibility of decorations <h5> tpt.decorations_enable(number toggle) </h6> <h3> tpt.heat </h4> Toggles Heat Simulation State <h5> tpt.heat(number toggle) </h6> <h3> tpt.active_menu </h4> Changes activated menu <h5> tpt.active_menu(number menu) </h6> <h3> tpt.display_mode </h4> Changes activated display mode <h5> tpt.display_mode(number display) </h6> <h3> tpt.setfpscap </h4> Changes the maximum FPS. <h5> tpt.setfpscap(number fpscap) </h6> <h3> tpt.setfire </h4> Changes the strength of the games glowing effects. tpt.setfire(1) is default. <h5> tpt.setfire(number strength) </h6> <h3> tpt.setwindowsize </h4> Changes the window settings. Scale is either 1 or 2. Returns a number, probably to indicate success. <h5> tpt.setwindowsize(number scale, toggle fullscreen) </h6> <h2> Particles </h3> <h3> tpt.reset_spark </h4> Removes electrified wires from the simulation, resetting to the original material <h5> tpt.reset_spark() </h6> <h3> tpt.set_property </h4> Set various properties of particles for given criteria, 8 overloads <h5> tpt.set_property(string property, object value) </h6> <h5> tpt.set_property(string property, object value, string type) </h6> <h5> tpt.set_property(string property, object value, number index) </h6> <h5> tpt.set_property(string property, object value, number index, string type) </h6> <h5> tpt.set_property(string property, object value, number x, number y) </h6> <h5> tpt.set_property(string property, object value, number x, number y, string type) </h6> <h5> tpt.set_property(string property, object value, number x, number y, number width, number height) </h6> <h5> tpt.set_property(string property, object value, number x, number y, number width, number height, string type) </h6> <h3> object tpt.get_property </h4> Get various properties of a particle. Returns an object <h5> tpt.get_property(string property, number index) </h6> <h5> tpt.get_property(string property, number x, number y) </h6> <h3> tpt.create </h4> Create a particle at location. <h5> tpt.create(number x, number y, string type) </h6> <h3> tpt.delete </h4> Delete a specific particle, or location. <h5> tpt.delete(number index) </h6> <h5> tpt.delete(number x, number y) </h6> <h3> tpt.start_getPartIndex </h4> Start the iterator for receiving all indecies of the particles. (Used to help get particle indecies, see tpt.next_getPartIndex) <h5> tpt.start_getPartIndex() </h6> <h3> boolean tpt.next_getPartIndex </h4> Jump to the next available particle index. Returns false if the iterator has reached the end of all particle indecies. Returns true if a new index was available. (Used to help get particle indecies, see tpt.getPartIndex) <h5> tpt.next_getPartIndex() </h6> <h3> number tpt.getPartIndex </h4> Get the current index iterator. <h5> tpt.getPartIndex() </h6> <h3> number tpt.get_numOfParts </h4> Returns the number of particles currently on the screen. <h5> tpt.get_numOfParts() </h6> <h2> Drawing </h3> <h3> number tpt.textwidth </h4> Measures (in pixels) the width of a given string. Returns a number <h5> tpt.textwidth(string text) </h6> <h3> tpt.drawtext </h4> Draw text to the screen (for one frame, only useful in scripts), 3 overloads <h5> tpt.drawtext(number x, number y, string text) </h6> <h5> tpt.drawtext(number x, number y, string text, number red, number green, number blue) </h6> <h5> tpt.drawtext(number x, number y, string text, number red, number green, number blue, number alpha) </h6> <h3> tpt.drawpixel </h4> Draws a pixel on the screen (for one frame, only useful in scripts), 3 overloads <h5> tpt.drawpixel(number x, number y) </h6> <h5> tpt.drawpixel(number x, number y, number red, number green, number blue) </h6> <h5> tpt.drawpixel(number x, number y, number red, number green, number blue, number alpha) </h6> <h3> tpt.drawrect </h4> Draws a rectangle on the screen (for one frame, only useful in scripts), 3 overloads <h5> tpt.drawrect(number x, number y, number width, number height) </h6> <h5> tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue) </h6> <h5> tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue, number alpha) </h6> <h3> tpt.fillrect </h4> Draws a filled in rectangle on the screen (for one frame, only useful in scripts), 3 overloads <h5> tpt.fillrect(number x, number y, number width, number height) </h6> <h5> tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue) </h6> <h5> tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue, number alpha) </h6> <h2> Input/Output </h3> <h3> tpt.log </h4> Log a message to the console <h5> tpt.log(string text) </h6> <h3> tpt.message_box </h4> Display an OK-Only message box with a title and message. <h5> tpt.message_box(string title, string message) </h6> <h3> string tpt.input </h4> Ask the user to input some text. Returns a string of what ever the user says. The argument "text" is pre-entered text (optional). <h5> tpt.input(string title, string message) </h6> <h5> tpt.input(string title, string message, string text) </h6> <h2> Events </h3> <h3> tpt.register_step </h4> Register a function to be run on every frame <h5> tpt.register_step(function func) </h6> <h3> tpt.unregister_step </h4> Unregister a previously registered function <h5> tpt.unregister_step(function func) </h6> <h3> tpt.register_mouseclick </h4> Register a function to be run every time the mouse clicks\\ \\ Passes: (number mousex, number mousey, number button, number event) <h5> tpt.register_mouseclick(function func) </h6> <h3> tpt.unregister_mouseclick </h4> Unregister a previously registered function <h5> tpt.unregister_mouseclick(function func) </h6> <h3> tpt.register_keypress </h4> Register a function to be run every time a key is pressed\\ \\ Passes: (string key, number key, number modifier) <h5> tpt.register_keypress(function func) </h6> <h3> tpt.unregister_keypress </h4> Unregister a previously registered function <h5> tpt.unregister_keypress(function func) </h6> <h2> Misc </h3> <h3> string tpt.get_name </h4> Get the current username, returns a string <h5> tpt.get_name() </h6> <h3> tpt.throw_error </h4> Displays an error message <h5> tpt.throw_error(string text) </h6> <h3> tpt.setdebug </h4> Sets the "debug mode". Only odd numbers seem to do anything for now. <h5> tpt.setdebug(number mode) </h6> <h1> Mod's with Lua Functions </h2> <h3> Me4502's Mod </h4>

NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER </h5> <h4> String Property Additions </h5> "collision" "airdrag" "flammable" "weight" "falldown" "gravity" "explosive" "meltable" "hardness" "name" "airloss" "loss" "hotair" "actas" <h4> New Functions </h5> <h5> tpt.throw_error("") </h6> <h5> tpt.getscript("") </h6> For the following, 1 in the ()'s is on 0 is off <h5> tpt.hud() </h6> <h5> tpt.newtonian_gravity() </h6> <h5> tpt.ambient_heat() </h6> <h5> tpt.decorations_enable() </h6> <h5> tpt.heat() </h6> For the following the number of the value you want goes into the ()'s <h5> tpt.active_menu() </h6> <h5> tpt.display_mode() </h6> <h5> tpt.set_glow() </h6> <h1> Simple Example Code </h2> -- This line is a comment. Anything written after the -- is considered a Comment and will not be read by Lua. -- Set the console's state to 0. This will hide the console. tpt.set_console(0) -- Here we define our main function for the script function ClassicPowder() local ox = 125 -- This will be our offset for the different elements we will create. local y = 4 -- where on the y (vertical) axis where we will create our elements. local x = ox -- where on the x (horizontal) axis where we will create our elements. we will start the x value with what ever ox is above. for i=0, 10 do -- this is a for loop. everything between the do and end will loop until i hits 10. i increases by 1 every loop. tpt.create(x + i, y, "DUST") --create a dust particle end x = x + ox -- increase the x axis value by the offset x (ox) value above. for i=0, 10 do tpt.create(x + i, y, "WATR") --create a water particle end x = x + ox for i=0, 10 do tpt.create(x + i, y, "SALT") end x = x + ox for i=0, 10 do tpt.create(x + i, y, "OIL") end return false end -- Register the step function ClassicPowder. This will make the ClassicPowder function run every tick of Powder Toy. tpt.register_step(ClassicPowder)