Skip to main content

Overview

@accessorfunc marks a function as an accessor generator. When you call that function, GLuaLS creates GetPropertyName() and SetPropertyName() methods on the target class. This matches GMod’s built-in AccessorFunc().

Syntax

---@accessorfunc
---@accessorfunc N
  • Without N: the first argument of the call is treated as the accessor name
  • N: a 1-indexed position for the argument that provides the accessor name

How it works

When GLuaLS sees a call to a function marked with @accessorfunc, it generates accessor methods on the object:
-- Built-in AccessorFunc is annotated with @accessorfunc 3
AccessorFunc(ENT, "m_bOrient", "Orient", FORCE_BOOL)

-- GLuaLS synthesizes:
-- ENT:GetOrient() -> any
-- ENT:SetOrient(value: any) -> nil
GLuaLS types getter and setter values as any. Future versions will infer stronger types from the backing field.

Custom accessor generators

If you write your own accessor generator, annotate it with @accessorfunc:
---@accessorfunc
function ENT:RegisterProperty(name)
    -- internally sets up getter and setter
end

function ENT:SetupDataTables()
    self:RegisterProperty("Health")   -- synthesizes GetHealth(), SetHealth()
    self:RegisterProperty("Armor")    -- synthesizes GetArmor(), SetArmor()
end

Specifying name argument position

If the name is not the first argument:
---@accessorfunc 3
function ENT:RegisterTypedProperty(varType, slot, name)
    -- varType = "Int", slot = 0, name = "Health"
end

function ENT:SetupDataTables()
    self:RegisterTypedProperty("Int", 0, "Health")  -- synthesizes GetHealth(), SetHealth()
end

Current limitations

  • Synthesized getters return any and setters accept any; GLuaLS does not infer these types from the field name or var type yet
  • Works on any class, not just GMod scripted entities
  • Not gated by gmod.enabled

See also