Difference between revisions of "Lua"

From The Powder Toy
Jump to: navigation, search
(talk)
m (1 revision)
(No difference)

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)