Difference between revisions of "Lua"
m (1 revision) |
(talk) |
||
Line 1: | Line 1: | ||
− | <h1> = The Lua Console = </ | + | <h1> = The Lua Console = </h1> |
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] | ||
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. | ||
− | <h1> = Quick Introduction to Scripts = </ | + | <h1> = Quick Introduction to Scripts = </h1> |
− | <h2> How to run a script </ | + | <h2> How to run a script </h2> |
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. | 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 </ | + | <h2> Variable Types </h2> |
<ins>The different variable types are:</ins> | <ins>The different variable types are:</ins> | ||
Line 108: | Line 108: | ||
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. | ||
− | <h2> General Arguments </ | + | <h2> General Arguments </h2> |
<ins>The arguments given here are general, here are some examples:</ins> | <ins>The arguments given here are general, here are some examples:</ins> | ||
Line 198: | Line 198: | ||
---- | ---- | ||
− | <h1> Lua API </ | + | <h1> Lua API </h1> |
'''The Powder Toy exposes the following methods to the Lua API:''' | '''The Powder Toy exposes the following methods to the Lua API:''' | ||
− | <h2> Game </ | + | <h2> Game </h2> |
− | <h3> tpt.set_pause </ | + | <h3> tpt.set_pause </h3> |
Set the paused state of the game | Set the paused state of the game | ||
− | <h5> tpt.set_pause(number state) </ | + | <h5> tpt.set_pause(number state) </h5> |
− | <h3> tpt.set_console </ | + | <h3> tpt.set_console </h3> |
Set the visibility state of the console | Set the visibility state of the console | ||
− | <h5> tpt.set_console(number state) </ | + | <h5> tpt.set_console(number state) </h5> |
− | <h3> tpt.set_shortcuts </ | + | <h3> tpt.set_shortcuts </h3> |
Set whether keyboard shortcuts are enabled | Set whether keyboard shortcuts are enabled | ||
− | <h5> tpt.set_shortcuts(number state) </ | + | <h5> tpt.set_shortcuts(number state) </h5> |
− | <h3> tpt.set_gravity </ | + | <h3> tpt.set_gravity </h3> |
Sets values on the gravity map, 3 overloads | Sets values on the gravity map, 3 overloads | ||
− | <h5> tpt.set_gravity(number x, number y) </ | + | <h5> tpt.set_gravity(number x, number y) </h5> |
− | <h5> tpt.set_gravity(number x, number y, number width, number height) </ | + | <h5> tpt.set_gravity(number x, number y, number width, number height) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.reset_gravity_field </ | + | <h3> tpt.reset_gravity_field </h3> |
Resets regions on the gravity velocity map, 2 overloads | Resets regions on the gravity velocity map, 2 overloads | ||
− | <h5> tpt.reset_gravity_field(number x, number y) </ | + | <h5> tpt.reset_gravity_field(number x, number y) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.set_pressure </ | + | <h3> tpt.set_pressure </h3> |
Sets values on the pressure map, 3 overloads | Sets values on the pressure map, 3 overloads | ||
− | <h5> tpt.set_pressure(number x, number y) </ | + | <h5> tpt.set_pressure(number x, number y) </h5> |
− | <h5> tpt.set_pressure(number x, number y, number width, number height) </ | + | <h5> tpt.set_pressure(number x, number y, number width, number height) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.reset_velocity </ | + | <h3> tpt.reset_velocity </h3> |
Resets regions on the velocity map, 2 overloads | Resets regions on the velocity map, 2 overloads | ||
− | <h5> tpt.reset_velocity(number x, number y) </ | + | <h5> tpt.reset_velocity(number x, number y) </h5> |
− | <h5> tpt.reset_velocity(number x, number y, number width, number height) </ | + | <h5> tpt.reset_velocity(number x, number y, number width, number height) </h5> |
− | <h3> tpt.hud </ | + | <h3> tpt.hud </h3> |
Toggles HUD State | Toggles HUD State | ||
− | <h5> tpt.hud(number toggle) </ | + | <h5> tpt.hud(number toggle) </h5> |
− | <h3> tpt.newtonian_gravity </ | + | <h3> tpt.newtonian_gravity </h3> |
Toggles Newtonian Gravity State | Toggles Newtonian Gravity State | ||
− | <h5> tpt.newtonian_gravity(number toggle) </ | + | <h5> tpt.newtonian_gravity(number toggle) </h5> |
− | <h3> tpt.ambient_heat </ | + | <h3> tpt.ambient_heat </h3> |
Toggles Ambient Heat State | Toggles Ambient Heat State | ||
− | <h5> tpt.ambient_heat(number toggle) </ | + | <h5> tpt.ambient_heat(number toggle) </h5> |
− | <h3> tpt.decorations_enable </ | + | <h3> tpt.decorations_enable </h3> |
Toggles visibility of decorations | Toggles visibility of decorations | ||
− | <h5> tpt.decorations_enable(number toggle) </ | + | <h5> tpt.decorations_enable(number toggle) </h5> |
− | <h3> tpt.heat </ | + | <h3> tpt.heat </h3> |
Toggles Heat Simulation State | Toggles Heat Simulation State | ||
− | <h5> tpt.heat(number toggle) </ | + | <h5> tpt.heat(number toggle) </h5> |
− | <h3> tpt.active_menu </ | + | <h3> tpt.active_menu </h3> |
Changes activated menu | Changes activated menu | ||
− | <h5> tpt.active_menu(number menu) </ | + | <h5> tpt.active_menu(number menu) </h5> |
− | <h3> tpt.display_mode </ | + | <h3> tpt.display_mode </h3> |
Changes activated display mode | Changes activated display mode | ||
− | <h5> tpt.display_mode(number display) </ | + | <h5> tpt.display_mode(number display) </h5> |
− | <h3> tpt.setfpscap </ | + | <h3> tpt.setfpscap </h3> |
Changes the maximum FPS. | Changes the maximum FPS. | ||
− | <h5> tpt.setfpscap(number fpscap) </ | + | <h5> tpt.setfpscap(number fpscap) </h5> |
− | <h3> tpt.setfire </ | + | <h3> tpt.setfire </h3> |
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. | ||
− | <h5> tpt.setfire(number strength) </ | + | <h5> tpt.setfire(number strength) </h5> |
− | <h3> tpt.setwindowsize </ | + | <h3> tpt.setwindowsize </h3> |
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. | ||
− | <h5> tpt.setwindowsize(number scale, toggle fullscreen) </ | + | <h5> tpt.setwindowsize(number scale, toggle fullscreen) </h5> |
− | <h2> Particles </ | + | <h2> Particles </h2> |
− | <h3> tpt.reset_spark </ | + | <h3> tpt.reset_spark </h3> |
Removes electrified wires from the simulation, resetting to the original material | Removes electrified wires from the simulation, resetting to the original material | ||
− | <h5> tpt.reset_spark() </ | + | <h5> tpt.reset_spark() </h5> |
− | <h3> tpt.set_property </ | + | <h3> tpt.set_property </h3> |
Set various properties of particles for given criteria, 8 overloads | Set various properties of particles for given criteria, 8 overloads | ||
− | <h5> tpt.set_property(string property, object value) </ | + | <h5> tpt.set_property(string property, object value) </h5> |
− | <h5> tpt.set_property(string property, object value, string type) </ | + | <h5> tpt.set_property(string property, object value, string type) </h5> |
− | <h5> tpt.set_property(string property, object value, number index) </ | + | <h5> tpt.set_property(string property, object value, number index) </h5> |
− | <h5> tpt.set_property(string property, object value, number index, string type) </ | + | <h5> tpt.set_property(string property, object value, number index, string type) </h5> |
− | <h5> tpt.set_property(string property, object value, number x, number y) </ | + | <h5> tpt.set_property(string property, object value, number x, number y) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h3> object tpt.get_property </ | + | <h3> object tpt.get_property </h3> |
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) </ | + | <h5> tpt.get_property(string property, number index) </h5> |
− | <h5> tpt.get_property(string property, number x, number y) </ | + | <h5> tpt.get_property(string property, number x, number y) </h5> |
− | <h3> tpt.create </ | + | <h3> tpt.create </h3> |
Create a particle at location. | Create a particle at location. | ||
− | <h5> tpt.create(number x, number y, string type) </ | + | <h5> tpt.create(number x, number y, string type) </h5> |
− | <h3> tpt.delete </ | + | <h3> tpt.delete </h3> |
Delete a specific particle, or location. | Delete a specific particle, or location. | ||
− | <h5> tpt.delete(number index) </ | + | <h5> tpt.delete(number index) </h5> |
− | <h5> tpt.delete(number x, number y) </ | + | <h5> tpt.delete(number x, number y) </h5> |
− | <h3> tpt.start_getPartIndex </ | + | <h3> tpt.start_getPartIndex </h3> |
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) | ||
− | <h5> tpt.start_getPartIndex() </ | + | <h5> tpt.start_getPartIndex() </h5> |
− | <h3> boolean tpt.next_getPartIndex </ | + | <h3> boolean tpt.next_getPartIndex </h3> |
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) | ||
− | <h5> tpt.next_getPartIndex() </ | + | <h5> tpt.next_getPartIndex() </h5> |
− | <h3> number tpt.getPartIndex </ | + | <h3> number tpt.getPartIndex </h3> |
Get the current index iterator. | Get the current index iterator. | ||
− | <h5> tpt.getPartIndex() </ | + | <h5> tpt.getPartIndex() </h5> |
− | <h3> number tpt.get_numOfParts </ | + | <h3> number tpt.get_numOfParts </h3> |
Returns the number of particles currently on the screen. | Returns the number of particles currently on the screen. | ||
− | <h5> tpt.get_numOfParts() </ | + | <h5> tpt.get_numOfParts() </h5> |
− | <h2> Drawing </ | + | <h2> Drawing </h2> |
− | <h3> number tpt.textwidth </ | + | <h3> number tpt.textwidth </h3> |
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) </ | + | <h5> tpt.textwidth(string text) </h5> |
− | <h3> tpt.drawtext </ | + | <h3> tpt.drawtext </h3> |
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 | ||
− | <h5> tpt.drawtext(number x, number y, string text) </ | + | <h5> tpt.drawtext(number x, number y, string text) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.drawpixel </ | + | <h3> tpt.drawpixel </h3> |
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 | ||
− | <h5> tpt.drawpixel(number x, number y) </ | + | <h5> tpt.drawpixel(number x, number y) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.drawrect </ | + | <h3> tpt.drawrect </h3> |
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 | ||
− | <h5> tpt.drawrect(number x, number y, number width, number height) </ | + | <h5> tpt.drawrect(number x, number y, number width, number height) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h3> tpt.fillrect </ | + | <h3> tpt.fillrect </h3> |
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 | ||
− | <h5> tpt.fillrect(number x, number y, number width, number height) </ | + | <h5> tpt.fillrect(number x, number y, number width, number height) </h5> |
− | <h5> 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) </h5> |
− | <h5> 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) </h5> |
− | <h2> Input/Output </ | + | <h2> Input/Output </h2> |
− | <h3> tpt.log </ | + | <h3> tpt.log </h3> |
Log a message to the console | Log a message to the console | ||
− | <h5> tpt.log(string text) </ | + | <h5> tpt.log(string text) </h5> |
− | <h3> tpt.message_box </ | + | <h3> tpt.message_box </h3> |
Display an OK-Only message box with a title and message. | Display an OK-Only message box with a title and message. | ||
− | <h5> tpt.message_box(string title, string message) </ | + | <h5> tpt.message_box(string title, string message) </h5> |
− | <h3> string tpt.input </ | + | <h3> string tpt.input </h3> |
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). | ||
− | <h5> tpt.input(string title, string message) </ | + | <h5> tpt.input(string title, string message) </h5> |
− | <h5> tpt.input(string title, string message, string text) </ | + | <h5> tpt.input(string title, string message, string text) </h5> |
− | <h2> Events </ | + | <h2> Events </h2> |
− | <h3> tpt.register_step </ | + | <h3> tpt.register_step </h3> |
Register a function to be run on every frame | Register a function to be run on every frame | ||
− | <h5> tpt.register_step(function func) </ | + | <h5> tpt.register_step(function func) </h5> |
− | <h3> tpt.unregister_step </ | + | <h3> tpt.unregister_step </h3> |
Unregister a previously registered function | Unregister a previously registered function | ||
− | <h5> tpt.unregister_step(function func) </ | + | <h5> tpt.unregister_step(function func) </h5> |
− | <h3> tpt.register_mouseclick </ | + | <h3> tpt.register_mouseclick </h3> |
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'') | ||
− | <h5> tpt.register_mouseclick(function func) </ | + | <h5> tpt.register_mouseclick(function func) </h5> |
− | <h3> tpt.unregister_mouseclick </ | + | <h3> tpt.unregister_mouseclick </h3> |
Unregister a previously registered function | Unregister a previously registered function | ||
− | <h5> tpt.unregister_mouseclick(function func) </ | + | <h5> tpt.unregister_mouseclick(function func) </h5> |
− | <h3> tpt.register_keypress </ | + | <h3> tpt.register_keypress </h3> |
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'') | ||
− | <h5> tpt.register_keypress(function func) </ | + | <h5> tpt.register_keypress(function func) </h5> |
− | <h3> tpt.unregister_keypress </ | + | <h3> tpt.unregister_keypress </h3> |
Unregister a previously registered function | Unregister a previously registered function | ||
− | <h5> tpt.unregister_keypress(function func) </ | + | <h5> tpt.unregister_keypress(function func) </h5> |
− | <h2> Misc </ | + | <h2> Misc </h2> |
− | <h3> string tpt.get_name </ | + | <h3> string tpt.get_name </h3> |
Get the current username, returns a ''string'' | Get the current username, returns a ''string'' | ||
− | <h5> tpt.get_name() </ | + | <h5> tpt.get_name() </h5> |
− | <h3> tpt.throw_error </ | + | <h3> tpt.throw_error </h3> |
Displays an error message | Displays an error message | ||
− | <h5> tpt.throw_error(string text) </ | + | <h5> tpt.throw_error(string text) </h5> |
− | <h3> tpt.setdebug </ | + | <h3> tpt.setdebug </h3> |
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. | ||
− | <h5> tpt.setdebug(number mode) </ | + | <h5> tpt.setdebug(number mode) </h5> |
− | <h1> Mod's with Lua Functions </ | + | <h1> Mod's with Lua Functions </h1> |
− | <h3> Me4502's Mod </ | + | <h3> Me4502's Mod </h3> |
− | <h4> NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER </ | + | <h4> NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER </h4> |
− | <h4> String Property Additions </ | + | <h4> String Property Additions </h4> |
"collision" | "collision" | ||
"airdrag" | "airdrag" | ||
Line 380: | Line 380: | ||
"actas" | "actas" | ||
− | <h4> New Functions </ | + | <h4> New Functions </h4> |
− | <h5> tpt.throw_error("") </ | + | <h5> tpt.throw_error("") </h5> |
− | <h5> tpt.getscript("") </ | + | <h5> tpt.getscript("") </h5> |
For the following, 1 in the ()'s is on 0 is off | For the following, 1 in the ()'s is on 0 is off | ||
− | <h5> tpt.hud() </ | + | <h5> tpt.hud() </h5> |
− | <h5> tpt.newtonian_gravity() </ | + | <h5> tpt.newtonian_gravity() </h5> |
− | <h5> tpt.ambient_heat() </ | + | <h5> tpt.ambient_heat() </h5> |
− | <h5> tpt.decorations_enable() </ | + | <h5> tpt.decorations_enable() </h5> |
− | <h5> tpt.heat() </ | + | <h5> tpt.heat() </h5> |
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 | ||
− | <h5> tpt.active_menu() </ | + | <h5> tpt.active_menu() </h5> |
− | <h5> tpt.display_mode() </ | + | <h5> tpt.display_mode() </h5> |
− | <h5> tpt.set_glow() </ | + | <h5> tpt.set_glow() </h5> |
− | <h1> Simple Example Code </ | + | <h1> Simple Example Code </h1> |
-- 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:28, 28 September 2011
Contents
- 1 = The Lua Console =
- 2 = Quick Introduction to Scripts =
- 3 Lua API
- 3.1 Game
- 3.1.1 tpt.set_pause
- 3.1.2 tpt.set_console
- 3.1.3 tpt.set_shortcuts
- 3.1.4 tpt.set_gravity
- 3.1.5 tpt.reset_gravity_field
- 3.1.6 tpt.set_pressure
- 3.1.7 tpt.reset_velocity
- 3.1.8 tpt.hud
- 3.1.9 tpt.newtonian_gravity
- 3.1.10 tpt.ambient_heat
- 3.1.11 tpt.decorations_enable
- 3.1.12 tpt.heat
- 3.1.13 tpt.active_menu
- 3.1.14 tpt.display_mode
- 3.1.15 tpt.setfpscap
- 3.1.16 tpt.setfire
- 3.1.17 tpt.setwindowsize
- 3.2 Particles
- 3.2.1 tpt.reset_spark
- 3.2.2 tpt.set_property
- 3.2.2.1 tpt.set_property(string property, object value)
- 3.2.2.2 tpt.set_property(string property, object value, string type)
- 3.2.2.3 tpt.set_property(string property, object value, number index)
- 3.2.2.4 tpt.set_property(string property, object value, number index, string type)
- 3.2.2.5 tpt.set_property(string property, object value, number x, number y)
- 3.2.2.6 tpt.set_property(string property, object value, number x, number y, string type)
- 3.2.2.7 tpt.set_property(string property, object value, number x, number y, number width, number height)
- 3.2.2.8 tpt.set_property(string property, object value, number x, number y, number width, number height, string type)
- 3.2.3 object tpt.get_property
- 3.2.4 tpt.create
- 3.2.5 tpt.delete
- 3.2.6 tpt.start_getPartIndex
- 3.2.7 boolean tpt.next_getPartIndex
- 3.2.8 number tpt.getPartIndex
- 3.2.9 number tpt.get_numOfParts
- 3.3 Drawing
- 3.3.1 number tpt.textwidth
- 3.3.2 tpt.drawtext
- 3.3.3 tpt.drawpixel
- 3.3.4 tpt.drawrect
- 3.3.4.1 tpt.drawrect(number x, number y, number width, number height)
- 3.3.4.2 tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue)
- 3.3.4.3 tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)
- 3.3.5 tpt.fillrect
- 3.3.5.1 tpt.fillrect(number x, number y, number width, number height)
- 3.3.5.2 tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue)
- 3.3.5.3 tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)
- 3.4 Input/Output
- 3.5 Events
- 3.6 Misc
- 3.1 Game
- 4 Mod's with Lua Functions
- 5 Simple Example Code
= The Lua Console =
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.
= Quick Introduction to Scripts =
How to run a script
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.
Variable Types
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.
General Arguments
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
Lua API
The Powder Toy exposes the following methods to the Lua API:
Game
tpt.set_pause
Set the paused state of the game
tpt.set_pause(number state)
tpt.set_console
Set the visibility state of the console
tpt.set_console(number state)
tpt.set_shortcuts
Set whether keyboard shortcuts are enabled
tpt.set_shortcuts(number state)
tpt.set_gravity
Sets values on the gravity map, 3 overloads
tpt.set_gravity(number x, number y)
tpt.set_gravity(number x, number y, number width, number height)
tpt.set_gravity(number x, number y, number width, number height, number value)
tpt.reset_gravity_field
Resets regions on the gravity velocity map, 2 overloads
tpt.reset_gravity_field(number x, number y)
tpt.reset_gravity_field(number x, number y, number width, number height)
tpt.set_pressure
Sets values on the pressure map, 3 overloads
tpt.set_pressure(number x, number y)
tpt.set_pressure(number x, number y, number width, number height)
tpt.set_pressure(number x, number y, number width, number height, number value)
tpt.reset_velocity
Resets regions on the velocity map, 2 overloads
tpt.reset_velocity(number x, number y)
tpt.reset_velocity(number x, number y, number width, number height)
tpt.hud
Toggles HUD State
tpt.hud(number toggle)
tpt.newtonian_gravity
Toggles Newtonian Gravity State
tpt.newtonian_gravity(number toggle)
tpt.ambient_heat
Toggles Ambient Heat State
tpt.ambient_heat(number toggle)
tpt.decorations_enable
Toggles visibility of decorations
tpt.decorations_enable(number toggle)
tpt.heat
Toggles Heat Simulation State
tpt.heat(number toggle)
Changes activated menu
tpt.display_mode
Changes activated display mode
tpt.display_mode(number display)
tpt.setfpscap
Changes the maximum FPS.
tpt.setfpscap(number fpscap)
tpt.setfire
Changes the strength of the games glowing effects. tpt.setfire(1) is default.
tpt.setfire(number strength)
tpt.setwindowsize
Changes the window settings. Scale is either 1 or 2. Returns a number, probably to indicate success.
tpt.setwindowsize(number scale, toggle fullscreen)
Particles
tpt.reset_spark
Removes electrified wires from the simulation, resetting to the original material
tpt.reset_spark()
tpt.set_property
Set various properties of particles for given criteria, 8 overloads
tpt.set_property(string property, object value)
tpt.set_property(string property, object value, string type)
tpt.set_property(string property, object value, number index)
tpt.set_property(string property, object value, number index, string type)
tpt.set_property(string property, object value, number x, number y)
tpt.set_property(string property, object value, number x, number y, string type)
tpt.set_property(string property, object value, number x, number y, number width, number height)
tpt.set_property(string property, object value, number x, number y, number width, number height, string type)
object tpt.get_property
Get various properties of a particle. Returns an object
tpt.get_property(string property, number index)
tpt.get_property(string property, number x, number y)
tpt.create
Create a particle at location.
tpt.create(number x, number y, string type)
tpt.delete
Delete a specific particle, or location.
tpt.delete(number index)
tpt.delete(number x, number y)
tpt.start_getPartIndex
Start the iterator for receiving all indecies of the particles. (Used to help get particle indecies, see tpt.next_getPartIndex)
tpt.start_getPartIndex()
boolean tpt.next_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()
number tpt.getPartIndex
Get the current index iterator.
tpt.getPartIndex()
number tpt.get_numOfParts
Returns the number of particles currently on the screen.
tpt.get_numOfParts()
Drawing
number tpt.textwidth
Measures (in pixels) the width of a given string. Returns a number
tpt.textwidth(string text)
tpt.drawtext
Draw text to the screen (for one frame, only useful in scripts), 3 overloads
tpt.drawtext(number x, number y, string text)
tpt.drawtext(number x, number y, string text, number red, number green, number blue)
tpt.drawtext(number x, number y, string text, number red, number green, number blue, number alpha)
tpt.drawpixel
Draws a pixel on the screen (for one frame, only useful in scripts), 3 overloads
tpt.drawpixel(number x, number y)
tpt.drawpixel(number x, number y, number red, number green, number blue)
tpt.drawpixel(number x, number y, number red, number green, number blue, number alpha)
tpt.drawrect
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)
tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue)
tpt.drawrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)
tpt.fillrect
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)
tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue)
tpt.fillrect(number x, number y, number width, number height, number red, number green, number blue, number alpha)
Input/Output
tpt.log
Log a message to the console
tpt.log(string text)
tpt.message_box
Display an OK-Only message box with a title and message.
tpt.message_box(string title, string message)
string tpt.input
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)
tpt.input(string title, string message, string text)
Events
tpt.register_step
Register a function to be run on every frame
tpt.register_step(function func)
tpt.unregister_step
Unregister a previously registered function
tpt.unregister_step(function func)
tpt.register_mouseclick
Register a function to be run every time the mouse clicks\\ \\ Passes: (number mousex, number mousey, number button, number event)
tpt.register_mouseclick(function func)
tpt.unregister_mouseclick
Unregister a previously registered function
tpt.unregister_mouseclick(function func)
tpt.register_keypress
Register a function to be run every time a key is pressed\\ \\ Passes: (string key, number key, number modifier)
tpt.register_keypress(function func)
tpt.unregister_keypress
Unregister a previously registered function
tpt.unregister_keypress(function func)
Misc
string tpt.get_name
Get the current username, returns a string
tpt.get_name()
tpt.throw_error
Displays an error message
tpt.throw_error(string text)
tpt.setdebug
Sets the "debug mode". Only odd numbers seem to do anything for now.
tpt.setdebug(number mode)
Mod's with Lua Functions
Me4502's Mod
NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER
String Property Additions
"collision" "airdrag" "flammable" "weight" "falldown" "gravity" "explosive" "meltable" "hardness" "name" "airloss" "loss" "hotair" "actas"
New Functions
tpt.throw_error("")
tpt.getscript("")
For the following, 1 in the ()'s is on 0 is off
tpt.hud()
tpt.newtonian_gravity()
tpt.ambient_heat()
tpt.decorations_enable()
tpt.heat()
For the following the number of the value you want goes into the ()'s
tpt.display_mode()
tpt.set_glow()
Simple Example Code
-- 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)