Difference between revisions of "Lua"
(→Lua API) |
m (Reverted edits by Antoine7 (talk) to last revision by RadioActiveLua) |
||
Line 380: | Line 380: | ||
<code>tpt.get_numOfParts()</code> | <code>tpt.get_numOfParts()</code> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 680: | Line 675: | ||
tpt.create(x + i, y, "OIL") | tpt.create(x + i, y, "OIL") | ||
end | end | ||
− | |||
− | |||
− | |||
return false | return false |
Revision as of 15:02, 18 January 2014
You may open the Lua Console by hitting the [`] key. (Also known as the tilde [~] key, or the [¬] key) click here to view key
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 lua#tpt.set_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. If you're a beginner, look at this: http://www.lua.org/pil/ . If more advanced, a list of all the functions is here: 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.
This tutorial shows how to run a script
Contents
- 1 Variable Types
- 2 General Arguments
- 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.3 Drawing
- 3.4 Input/Output
- 3.5 Events
- 3.6 Misc
- 3.1 Game
- 4 Mods with Lua Functions
- 5 Simple Example Code
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" "tmp2" "vx" "vy" "x" "y" "dcolour"
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 It is recommended that you return false from a key event to do the same thing, instead of this
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.active_menu(number 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. Scale defaults to 1, fullscreen defaults to off. Returns a number, 1 indicates 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)
Returns the index of the newly created particle.
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 indices of the particles. (Used to help get particle indices, 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()
index code example:
tpt.start_getPartIndex() while tpt.next_getPartIndex() do local index = tpt.getPartIndex() if tpt.get_property("ctype",index) == 21 then tpt.set_property("ctype","sing",index) end end
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.drawline
Draws a line on the screen (for one frame, only useful in scripts), 3 overloads. The line starts at point (x1, y1) and ends at point (x2,y2).
tpt.drawpixel(number x1, number y1, number x2, number y2)
tpt.drawpixel(number x1, number y1, number x2, number y2, number red, number green, number blue)
tpt.drawpixel(number x1, number y1, number x2, number y2, 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)
Because tpt.fillrect is slightly broken in tpt, the coordinates will be off. It fills the rectangle from (x+1, y+1) to (x+w-1, y+h-1)
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 You may only have 6 step functions registered at once
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. You may only have 6 mouseclick functions registered at once
Your function will also be called when the mouse is released or held, or when the mouse wheel is used. Event equals 1 when the mouse gets pressed, 2 when the mouse gets released, and 3 if it is held. If your function returns false, mouse events in the normal Powder Toy will be ignored.
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. You may only have 6 keypress functions registered at once
Your function will also be called when a key is released. Event equals 1 when a key is pressed, and 2 when it gets released. If your function returns false, key presses in the normal Powder Toy will be ignored.
Passes: (string key, number key, number modifier, number event)
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". Setting it to 1, or an odd number, will display info on the number of particles on the screen. Setting it to 4 or 5 will display useful information when you draw lines using shift. This function is depreciated in tpt++ (and has no replacement yet)
tpt.setdebug(number mode)
tpt.element
returns an element's number. For example, it would return 28 for dmnd
tpt.element(string elementname)
tpt.element_func
allows you to replace or add on to an element's update function. Write a function like normal, and then put its name into this command. Use tpt.element("...") or tpt.el.dust.id for el_number, and make replace true if you want to overwrite the previous function, or false instead.
Passes: (number index, number partx, number party, number surround_space, number nt)
Returns: return 1 from your function if the particle is killed.
tpt.element_func(function newfunction, number el_number)
tpt.element_func(function newfunction, number el_number, boolean replace)
tpt.graphics_func
allows you to replace an element's graphics function. Write a function like normal, and then put its name into this command. Use tpt.el.(name of element to change).id for el_number.
Passes: (number index, number colr, number colg, number colb)
Returns: cache, pixel_mode, cola, colr, colg, colb, firea, firer, fireg, and fireb.
set cache to 1 if you don't want the function to ever be called again, preventing lag. Don't do this if you need the way your element looks to change depending on its properties.
colr/g/b are the red, green, and blue colors of your element. firea/r/g/b set the fire colors, but pixel_mode needs to be set to 0x00022000 for them to work.
There is currently a glitch with this, it may crash the game if the function has an error.
tpt.graphics_func(function newfunction, number el_number)
The pixel mode values you can use are:
PMODE_NONE 0x00000000 --prevents anything from being drawn PMODE_FLAT 0x00000001 --draw a basic pixel, overwriting the color under it. Doesn't support cola. PMODE_BLOB 0x00000002 --adds a blobby effect, like you were using blob (5) display mode PMODE_BLUR 0x00000004 --used in liquids in fancy display mode PMODE_GLOW 0x00000008 --Glow effect, used in elements like DEUT and TRON in fancy display mode PMODE_SPARK 0x00000010 -- used for things such as GBMB at first, dimmer than other modes PMODE_FLARE 0x00000020 --BOMB and other similar elements, brighter than PMODE_SPARK PMODE_LFLARE 0x00000040 --brightest spark mode, used when DEST hits something PMODE_ADD 0x00000080 --like PMODE_FLAT, but adds color to a pixel, instead of overwriting it. PMODE_BLEND 0x00000100 --basically the same thing as PMODE_ADD, but has better OpenGL support PSPEC_STICKMAN 0x00000200 --does nothing, because the stickmen won't get drawn unless it actually is one NO_DECO 0x00001000 --prevents decoration from showing on the element (used in LCRY) DECO_FIRE 0x00002000 --Allow decoration to be drawn on using the fire effect (gasses have this set) FIRE_ADD 0x00010000 --adds a weak fire effect around the element (ex. LAVA/LIGH) FIRE_BLEND 0x00020000 --adds a stronger fire effect around the element, default for gasses EFFECT_GRAVIN 0x01000000 --adds a PRTI effect. Might take some coding in an update function to get it to work properly, PRTI uses life and ctype to create the effects EFFECT_GRAVOUT 0x02000000 --adds a PRTO effect. Might take some coding in an update function to get it to work properly, PRTI uses life and ctype to create the effects
You can combine them in any way you want, you probably need more than one anyway. Radioactive elements default to PMODE_FLAT+PMODE_GLOW, liquids to PMODE_FLAT+PMODE_BLUR, and gasses to FIRE_BLEND+DECO_FIRE, with a firea of 125 and firer/g/b of colr/g/b divided by 2
See this for a picture of what they look like: https://powdertoy.co.uk/Wiki/W/File:Particle_Drawing_Modes.png.html
tpt.screenshot
Takes a screenshot of the current screen, minus the menu and HUD.
tpt.screenshot()
tpt.screenshot(boolean fullscreen)
Mods with Lua Functions
Jacob1's Mod
Jacob1's Mod adds many new lua functions and features besides just new functions, like auto-completion with tab in the console. For a full list and descriptions, see This page
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
These are all in the official version now, except for tpt.set_glow was renamed tpt.set_fire
- tpt.throw_error(string error)
- tpt.getscript(string script)
- tpt.hud(number state)
- tpt.newtonian_gravity(number state)
- tpt.ambient_heat(number state)
- tpt.decorations_enable(number state)
- tpt.heat(number state)
- tpt.active_menu(number menu)
- tpt.display_mode(number display)
- tpt.set_glow(number fireintensity)
Simple Example Code
-- This line is a comment. Anything written after the -- is considered a Comment and will not be read by Lua. -- Comment can be multiline, for this you should write it in --[[ and ]]-- -- 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, "STNE") --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)