Tutorials PartCraft help Reference
Scripts in Lua
Write Script workspace scripts in Lua 5.4: the cs table of built-in functions, arrays as values with indexing and arithmetic, the shared ws workspace, printing and showing results, and what is switched off.
Written for PartCraft 0.3. The same help is inside the app: press F1.
Lua is the second language of the Script workspace. Choose Lua in the language box above the editor (a .lua file opens in Lua by itself). Lua and CraftScript share the same variables and the same built-in functions, so a Lua script can use what a CraftScript script made, and the other way round.
The essentials
-- every built-in function is in the cs table
local K = cs.zeros(3, 3)
K:set(1, 1, 4) -- 1-based, like CraftScript
print(K(1, 1), #K) -- an element; the number of elements
-- the workspace: the same variables every script sees
ws.total = K(1, 1) * 2
print(ws.total)
-- several results from one function
local V, D = cs.call('eig', 2, cs.tensor({{2, 1}, {1, 2}}))
cs.<name>(…)calls any built-in function and returns its first result;cs.call(name, n, …)returnsnresults.cs.tensor({1, 2, 3})makes a row,cs.tensor({{1, 2}, {3, 4}})a matrix.- Arrays, sparse matrices, meshes and fields are values with methods:
t(i, j)ort:get(i, j),t:set(i, j, v),t:shape(),#t,t:T()(transpose),t:totable().+ - * / ^mean what they mean in CraftScript (*is the matrix product);t:times(u)multiplies element by element. - A number that is a 1×1 array arrives as a plain Lua number.
ws.namereads and writes the shared workspace. Global variables your script leaves behind also land there when it ends.print(…)writes to the console;cs.show(f),cs.plot(y)and the other display functions work as in CraftScript.
Switched off
Scripts cannot reach files, the network or the page: io, os (except the clock), require, dofile, loadfile and debug are removed. Use the Script workspace's Open and the variables to bring data in. The step budget and Stop work as for CraftScript.