This annotation is intended for complex cases and is primarily designed to be used by the built-in annotations.
Overview
Some functions put new fields on the table you give them.RegisterMetaTable is one: after the call, the table has MetaID and MetaName. Nothing in your code assigns those fields, so without help GLuaLS reports them as undefined.
adds_field goes on the function’s parameter and names each field the function adds. Every call then gives the passed table those fields, typed and in autocomplete. Go to definition on a field jumps to the argument of the call that added it.
You only annotate the function. Callers write nothing extra.
Syntax
@param of the table that gets the field. Add one line per field.
FieldNameis the name of the new field.typeis optional. It is a built-in type such asinteger,stringorboolean, or a class name. End it with?for a field that may benil. Without it, the field isany.
Example
The official annotations describeRegisterMetaTable like this:
Where the fields go
The fields go on whatever the argument is:- A variable typed as a class (
---@classor---@type) gives the field to that class. Every value of the class sees it, including classes that inherit from it. - A plain table such as
local t = {}gets the field on that table only.
CreateMetaTable registers a battlemechs.Base table, so every class it makes inherits MetaID:
Your own functions
Any function can use it, not only built-in ones:adds_field vs @outparam
Useadds_field when the function creates a field that is not on the table yet.
Use @outparam when the function writes a result into a field the caller already set up, like util.TraceLine filling in output.