WSE/LUA uses a modified version of LuaJIT 2.0.4, which is based on and ABI compatible to Lua 5.1
The game table is a predefined global table that allows you access to the game from lua.
To call operations, use game.operation_name(arg1, arg2, ...)
Example: game.display_message("test string")
String and pos arguments are passed to the game via the games registers (overwritting them), starting from reg[128] and counting downwards.
Notice that the argument type matters. For example, if an operation expects an integer, you cannot pass a string that contains a number.
Return values are:
boolDidNotFail, intError
intResult, intError
boolDidNotFail, intResult, intError
intError
= game.cf_operation(arg1, arg2, ...) for can_fail operations
= game.lhs_operation(lhs_arg, arg1, arg2, ...) for lhs operations
= game.cf_lhs_operation(lhs_arg, arg1, arg2, ...) for operations which are both can_fail and lhs
= game.operation(arg1, arg2, ...) for all other operations
While most Lhs operations don't require an actual value for lhs_arg (in those cases it's strictly a return variable, for example player_get_gold), some (for example val_add) do, so you have to specify it anyway.
Real-world lhs_operation example:
local gold = game.player_get_gold(0, playerId)
Note that in Lua, you don't have to assign all return values. For example, if you don't care about the error code, just omit the corresponding lhs var.
I'm not sure what the error code signalizes, however it should be 0 if there was no error.
To access registers, use game.reg[n] , game.sreg[n] and game.preg[n]
Example:
game.reg[0] = 123
local i = game.reg[0]
game.sreg[0] = "test string"
local s = game.sreg[0]
game.preg[0] = game.pos.new()
local pos = game.preg[0]
As of the current version, registers are copied by value instead of by reference. So doing things like:
game.preg[0]:rotX(30)
Does not change the actual game register. This is likely going to be improved in a future version. What you have to do instead for now is:
local p = game.preg[0]
p:rotX(30)
game.preg[0] = p
For every header_[name].py or ID_[name].py file you copied to your "msfiles" folder (except header_operations.py), a new table will be generated: game.const.[name]
This table will be filled with all the constants from that file.
As of the current version, only lines that match one of the two following regexes (regular expressions) will be accepted:
^(\w+)=(((-)?0x[\da-fA-F]+)|((-)?\d+(\.\d+)?))$
^(\w+)=(\w+)$
This probably looks intimidating to you, but really all it says is that your constant must either be a number, a hex number, or another constant (from the same file, currently).
In other words: constant = num|hexNum|otherConstant
If the match fails, the line will be ignored and a warning message will be triggered which you can safely ignore.
You can omit the [name] part in game.const.[name].[constant], in which case all tables in game.const will be searched for your constant and the first result will be used.
So let's say you want to add a deathmatch trigger for ti_on_agent_hit. Instead of this:
game.addTrigger("mst_multiplayer_dm", -28, 0, 0, condCB)
You could copy header_triggers.py to your msfiles folder and do:
game.addTrigger("mst_multiplayer_dm", game.const.ti_on_agent_hit, 0, 0, condCB)
--or if you like to be specific:
game.addTrigger("mst_multiplayer_dm", game.const.triggers.ti_on_agent_hit, 0, 0, condCB)
To add triggers, use game.addTrigger(strTemplateId, numCheckTime, numDelayTime, numRearmTime, funcConditionsCallback, [funcConsequencesCallback])
These work exactly like module system triggers. Times can be float values or values from header_triggers.py (the numbers, string names are not supported yet), just like you would do it in the MS. The callbacks must return a boolean. If conditionsCallback returns false, consequencesCallback will not be executed.
This function is fairly expensive, as it involves copying the entire array of triggers and then adding the new one.
Example:
function condCB()
doStuff()
return true
end
game.addTrigger("mst_multiplayer_dm", 1, 0, 0, condCB)
addTrigger returns an integer that is the index of the added trigger.
You can also remove triggers by using game.removeTrigger(strTemplateId, intTriggerIndex). If intTriggerIndex is negative, the trigger with (numTriggers + intTriggerIndex) gets removed (e.g. to remove the last trigger, use -1).
To use iterators (try_for_agents, try_for_players, ...), use the provided iterator functions game.partiesI, game.agentsI, game.propInstI, game.playersI
These work exactly like module system iterators and take the same arguments.
Example:
for curAgent in game.playersI(true) do --skip server
foo(curAgent)
end
To work with positions, the following "classes" are provided: game.rotation, game.pos and vector3 (see under miscellaneous)
Keep in mind that these don't need the fixed point multiplier that the module system requires you to use.
The MS multiplier is essentially a way of specifying the current unit of length.
MS Example:
set_fixed_point_multiplier(1),
position_get_x(":x", 0), #get pos0_x in meters
set_fixed_point_multiplier(100),
position_get_x(":x", 0), #get pos0_x in centimeters
Consider the lua counterpart to "always have a fixed point multiplier of 1".
game.rotation
game.rotation.new([obj]) - constructor. obj can be used to specify initial values.
game.rotation.prototype
s = vector3.new({x = 1}) --x axis
f = vector3.new({y = 1}) --forwards/y axis
u = vector3.new({z = 1}) --up/z axis
function getRot(self) --returns vector3.new({z = yaw, x = pitch, y = roll})
function rotX(self, angle) --rotate around x axis, angle in degrees
function rotY(self, angle) --rotate around y axis, angle in degrees
function rotZ(self, angle) --rotate around z axis, angle in degrees
function rotate(self, rotVec3) --rotate around all axis, zxy order, angle in degrees
game.pos
game.pos.new([obj]) - constructor. obj can be used to specify initial values.
game.pos.prototype
o = vector3.new() --position in the world
rot = game.rotation.new() --rotation, or heading of the pos if you like
--see game.rotation.prototype for these
function getRot(self)
function rotX(self, angle)
function rotY(self, angle)
function rotZ(self, angle)
function rotate(self, rotVec3)
Example:
local pos0 = game.preg[0]
local newRot = game.rotation.new(pos0.rot)
local newPos = game.pos.new({rot = newRot})
newPos:rotX(45)
newPos:rotate({x = 90, z = 180})
You can add presentations using game.addPrsnt(tablePrsnt)
tablePrsnt must have the following format
tablePrsnt = {
id = strID,
[flags] = {intFlag1, ...},
[mesh] = intMesh,
triggers = {[numTriggerConst] = func, ...}
}
[flags] can be omited and defaults to no flags at all. If you copied header_presentation.py to your msfiles folder, you can use the flags defined there.
[mesh] can be omited and defaults to 0. If you copied ID_meshes.py to your msfiles folder, you can use the mesh nos defined there.
triggers must have at least one element, the key being a number and the value being a function. If you copied header_triggers.py to your msfiles folder, you can use the trigger values defined there.
Basic example:
--copy header_presentations.py, header_triggers.py and ID_meshes.py to your msfiles folder for this example.
local index = game.addPrsnt({
id = "myPrsnt",
flags = {game.const.prsntf_read_only, game.const.prsntf_manual_end_only},
--you can initialize an array like this, without keys - they don't matter here anyway.
mesh = game.const.mesh_cb_ui_main,
triggers = {
[game.const.ti_on_presentation_load] = function ()
--The const inside the [] declares a number key, similar to keyName = 123 for string keys.
game.presentation_set_duration(9999999)
local overlay = game.create_mesh_overlay(0, game.const.mesh_mp_ingame_menu)
local position = game.pos.new()
position.o.x = 0.3
position.o.y = 0.3
game.overlay_set_position(overlay, position)
position.o.x = 0.5
position.o.y = 0.8
game.overlay_set_size(overlay, position)
end
}
})
game.start_presentation(index)
game.getScriptNo(script_name), use in conjunction with game.call_script(scriptNo, ...)
Example:
local no = game.getScriptNo("game_start")
game.call_script(no)
game.getCurTemplateId(), returns the id/name of the current mission template. Note that no template will be loaded at the time main.lua is executed.
Other WSE LUA API functions - these could be useful for advanced users, but you should get along fine without ever using them.
game.execOperation(strOperationName, arg1, arg2, ....) - see operations
game.getReg(intTypeId, intIndex) typeIds are: 0 = int, 1 = str, 2 = pos
game.setReg(intTypeId, intIndex, val)
vector3.prototype
x = 0
y = 0
z = 0
function len(self) --returns the length of the vector
vector3.new([obj]) - constructor. obj; can be used to specify initial values.
vector3 operators + - * ==
Example:
local vecA = vector3.new({x = 60, y = 30})
local vecB = vector3.new()
game.display_message(tostring(vecA:len())) --67.08...
if vecA == vecB then
--will not happen
end
vecA = vecA * vecB --{60*0, 30*0, 0*0}
if vecA == vecB then
--will happen
end
Changes include:
Disabled package.loadlib, package.cpath, io.popen. os.execute, os.getenv, os.tmpname, ffi library, loading bytecode (can be exploited)
Restrict dofile, loadfile and all IO operations to the lua directory