Difference between revisions of "Lua"
(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: | ||
− | + | <h1> = The Lua Console = </h2> | |
− | You may open the Lua Console by hitting the ''[`]'' key. (Also known as the tilde ''[~]'' 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''' | |
''!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. | + | 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. | ||
− | + | <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. | ||
− | + | <h2> Variable Types </h3> | |
− | + | <ins>The different variable types are:</ins> | |
− | |||
− | |||
+ | * ''''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].) | 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). | Represents a switch that is either on (true) or off (false). | ||
− | + | '''Example:''' ''local bool = true'' | |
---- | ---- | ||
− | + | * ''''function'''' | |
Represents a method or function in lua. | 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. | 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. | This creates a blank table. | ||
Line 72: | Line 71: | ||
− | + | '''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:''' | |
local tbl = {} | local tbl = {} | ||
Line 105: | Line 104: | ||
---- | ---- | ||
− | + | * ''''object'''' | |
− | Acts as a wildcard. It can be represented as any of the above, but this does | + | 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> | |
− | + | <ins>The arguments given here are general, here are some examples:</ins> | |
− | '' | + | ''''string'' '''property''''' |
− | This is to specify what property of a particle to change. REMINDER: This is a '' | + | 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''''' |
− | What you are setting the current property to. Since this is an | + | 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 '' | + | A ''''string'''' which is the code-name of an element "dust" "watr" "spng". |
---- | ---- | ||
− | '' | + | ''''number'' '''index''''' |
A specific particle number by it's index. | A specific particle number by it's index. | ||
Line 157: | Line 156: | ||
---- | ---- | ||
− | '' | + | ''''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''''' |
Represent a rectangle. | Represent a rectangle. | ||
Line 169: | Line 168: | ||
---- | ---- | ||
− | '' | + | ''''number'' '''x''''' and ''''number'' '''y''''' |
Represents a 2-Dimensional coordinate. | Represents a 2-Dimensional coordinate. | ||
Line 175: | Line 174: | ||
---- | ---- | ||
− | '' | + | ''''string'' '''text''''' |
Represents text | Represents text | ||
Line 181: | Line 180: | ||
---- | ---- | ||
− | '' | + | ''''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''''' |
Represents a menu, eg 1 = Walls, 2 = Electronics | Represents a menu, eg 1 = Walls, 2 = Electronics | ||
Line 193: | Line 192: | ||
---- | ---- | ||
− | '' | + | ''''number'' '''display''''' |
Represents a display mode, eg 4 = Fire, 6 = Heat | Represents a display mode, eg 4 = Fire, 6 = Heat | ||
Line 199: | Line 198: | ||
---- | ---- | ||
− | + | <h1> Lua API </h2> | |
− | + | '''The Powder Toy exposes the following methods to the Lua API:''' | |
− | + | <h2> Game </h3> | |
− | + | <h3> tpt.set_pause </h4> | |
Set the paused state of the game | Set the paused state of the game | ||
− | + | <h5> tpt.set_pause(number state) </h6> | |
− | + | <h3> tpt.set_console </h4> | |
Set the visibility state of the console | 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 | 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 | 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 | 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 | 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 | 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 | Toggles HUD State | ||
− | + | <h5> tpt.hud(number toggle) </h6> | |
− | + | <h3> tpt.newtonian_gravity </h4> | |
− | |||
Toggles Newtonian Gravity State | Toggles Newtonian Gravity State | ||
− | + | <h5> tpt.newtonian_gravity(number toggle) </h6> | |
− | + | <h3> tpt.ambient_heat </h4> | |
− | |||
Toggles Ambient Heat State | Toggles Ambient Heat State | ||
− | + | <h5> tpt.ambient_heat(number toggle) </h6> | |
− | + | <h3> tpt.decorations_enable </h4> | |
− | |||
Toggles visibility of decorations | Toggles visibility of decorations | ||
− | + | <h5> tpt.decorations_enable(number toggle) </h6> | |
− | + | <h3> tpt.heat </h4> | |
− | |||
Toggles Heat Simulation State | Toggles Heat Simulation State | ||
− | + | <h5> tpt.heat(number toggle) </h6> | |
− | + | <h3> tpt.active_menu </h4> | |
− | |||
Changes activated menu | Changes activated menu | ||
− | + | <h5> tpt.active_menu(number menu) </h6> | |
− | + | <h3> tpt.display_mode </h4> | |
− | |||
Changes activated display mode | Changes activated display mode | ||
− | + | <h5> tpt.display_mode(number display) </h6> | |
− | + | <h3> tpt.setfpscap </h4> | |
− | |||
Changes the maximum FPS. | 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. | 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. | 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 | 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 | 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'' | |
− | Get various properties of a particle. Returns an | + | <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. | 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. | 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) | 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) | 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. | Get the current index iterator. | ||
− | + | <h5> tpt.getPartIndex() </h6> | |
− | + | <h3> number tpt.get_numOfParts </h4> | |
Returns the number of particles currently on the screen. | 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'' | |
− | Measures (in pixels) the width of a given | + | <h5> tpt.textwidth(string text) </h6> |
− | + | <h3> tpt.drawtext </h4> | |
− | |||
− | |||
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) </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 | 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 | 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 | 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 | 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. | 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). | 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 | 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 | 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\\ \\ | Register a function to be run every time the mouse clicks\\ \\ | ||
− | Passes: ( | + | 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 | 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\\ \\ | Register a function to be run every time a key is pressed\\ \\ | ||
− | Passes: ( | + | Passes: (''string key, number key, number modifier'') |
− | + | <h5> tpt.register_keypress(function func) </h6> | |
− | + | <h3> tpt.unregister_keypress </h4> | |
Unregister a previously registered function | 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'' | |
− | Get the current username, returns a | + | <h5> tpt.get_name() </h6> |
− | + | <h3> tpt.throw_error </h4> | |
− | |||
− | |||
Displays an error message | 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. | 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> | |
− | + | <h4> NOTICE: THESE FUNCTIONS ARE AVAILABLE IN VERSION 3.0b11 OR LATER </h5> | |
− | + | <h4> String Property Additions </h5> | |
− | |||
− | |||
− | |||
"collision" | "collision" | ||
"airdrag" | "airdrag" | ||
Line 411: | Line 380: | ||
"actas" | "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 | 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 | 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. | -- 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 Powder Toy exposes the following methods to the Lua API: