Difference between revisions of "Lua"
(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
- 'string'
- 'number'
tbl[1] --returns 1 tbl[2] --returns 2 tbl[3] --returns 3 tbl[4] --returns "string"
'string property
The Powder Toy exposes the following methods to the Lua API: