Lua scripts that run in Avorion rely heavily on the Avorion Scripting API and thus aren't easy to debug in normal lua environments. Avorion provides a range of methods you can use to debug them more easily though.
'~dev' debug Script
You can add the debug script to your current craft by using the following chat command:
/run Entity():addScript("lib/entitydbg.lua")
This adds a new ~dev interaction to that entity which opens a large menu that allows you to manipulate nearly everything in the sector and your inventory, and, most importantly: It allows you to view and modify variables and scripts of entities, players, sectors and so on.
This script is meant to ease development and debugging of mods in general, so use it!
'print' debugging
An easy way to debug scripts is to add regular calls to print()
or eprint()
while writing your script. You can open the in-game console with a button.
Note: The in-game console isn't configured to a working button by default, you will have to configure it manually yourself.
print()
will print regular output to the consoleeprint()
will print error output to the console
Depending on where the function was executed, the output will have different colors in the console, making it easy to distinguish client-prints from server-prints. Please consult the Scripting API for more information to these functions.
printTable()
Use the printTable()
function to recursively print a table. It's defined in data/scripts/lib/utility.lua.
Logging
Another easy way to debug scripts, mostly while they're being used by players already, is to use logging. You can use the printlog()
function, that behaves similarly to print()
, but prints to the logfile instead of the console. Depending on whether that function is called on the client or server, it will print to the client or server logfile.
type()
Use type()
to find out what kind of object you're dealing with. type()
is a built-in lua function that can display the type of an object.
local a = 2
local b = "foo"
print (type(a))
print (type(b))
Output:
number string
Note: type()
will always display userobject
for Avorion Scripting API objects. See __avoriontype
below.
__avoriontype
All Avorion Scripting API objects have a "secret" __avoriontype
property, that is basically just a string containing their internal avorion type.
You can use this property to easily
- find out if the instance is a Scripting API object
- find out what type the object has exactly
local m = Matrix()
print (type(m))
print (m.__avoriontype)
Output:
userobject Matrix
Pitfalls
Sadly, the Avorion Scripting API and its environment comes with a few pitfalls that you simply have to be aware of to avoid them.
You can read more about Modding Pitfalls here.