Getting started

Your first plugin

The template plugin, file by file.

Every new plugin starts from the template. It's a working ACR plugin: it plays the build you picked for your character's profession, and has the windows to pick a build, equip it and change its settings. Use it as it is and add your own builds, or keep only the parts you need (see A plugin without the ACR).

Unzip it into scripts, rename the my-plugin folder to your plugin's name, add that name to scripts\plugins.txt and start it from the Lua Plugins window.

lua-plugin-template.zip
A my-plugin folder: the plugin, the ACR framework, four example builds and the window icons.
Download template
scripts/
  my-plugin/
    main.lua             the entry point: hands the callbacks to app.lua
    app.lua              the plugin: runs the build, draws the windows
    config/
      config.lua         the settings and their defaults
      config_api.lua     loads and saves them
    acr/                 the ACR framework: require("acr")
    acrs/                the builds, one file each
    acr_registry.lua     which builds each profession can pick
    acr_skills.lua       checks the skill bar and weapons against the build
    build.lua            equips the build's traits and skills
    ui/                  the ACR, Settings and ACR Status windows, and key binds
    images/              the windows' icons

main.lua, app.lua and config/ are the plugin; everything else is the ACR. ACRs explains the framework and how to add a build.

main.lua

The entry point. It runs once when the plugin starts: it loads the app, initializes it, and defines the callbacks the game calls, each handing off to the app. Add a callback here when you need one.

local app = require("app")

app:init()

function on_stop(reason) app:shutdown(reason) end
function on_tick() app:tick() end
function on_render() app:render() end
function on_damage(event) app:on_damage(event) end
function on_key_pressed(key) return app:on_key_pressed(key) end
function on_plugin_event(name, data) app:on_plugin_event(name, data) end

app.lua

The plugin itself. App is a table with methods: App.new() makes the one app, and every function defined with : gets it as self, so state kept in self (here, the ACR runner) lasts between ticks and frames.

local config_api = require("config.config_api")
local acr = require("acr")
local registry = require("acr_registry")
local build = require("build")
local acr_ui = require("ui")
local keybinds = require("ui.keybinds")
local state = require("ui.state")

local TARGETING_ENABLED_EVENT = "targeting-enabled"

local App = {}
App.__index = App

function App.new()
    return setmetatable({}, App)
end

function App:init()
    config_api.init()
    self.runner = acr.Runner.new(nil, config_api.cfg().acr)
end

function App:shutdown(reason)
    self.runner:shutdown()
end

function App:tick()
    config_api.save()
    build.process_queue()

    -- The build follows the character: another profession, or another pick, swaps it.
    local current = registry.current_acr()
    if current ~= self.runner.acr then
        self.runner:set_acr(current)
    end
    self.runner:tick()
end

function App:on_damage(event)
    self.runner:on_damage(event)
end

function App:render()
    acr_ui.render(self.runner.acr)
end

function App:on_key_pressed(key)
    if game_ui.text_box_has_focus() then
        return false
    end
    return keybinds.on_key_pressed(key)
end

---The targeting plugin says whether it's on; the status window shows it.
function App:on_plugin_event(name, data)
    if name == TARGETING_ENABLED_EVENT then
        state.targeting_enabled = data:byte(1) == 1
    end
end

return App.new()
  • init runs once, when the plugin starts (and again after every reload). It loads the settings and makes the runner, which plays the build. The runner reads config_api.cfg().acr every tick, so the window's checkboxes take effect at once.
  • tick runs every game tick. It saves the settings when they changed, makes the next change of a build being equipped, swaps the build when the character or the pick changed, and runs the build.
  • render runs every frame, and is the only place ui, gfx and gfx3d work. Here it draws the ACR windows. Keep it light and do heavy work in tick or a task.
  • shutdown runs when the plugin stops. Undo any game settings the plugin changed: the runner puts back the ground targeting mode it switched.
  • on_damage lets the runner see casts land, so it knows when a skill has done its work.
  • on_key_pressed gets every key press, for the key binds. Return true to keep the key from the game. The first check lets keys through while the player is typing in chat.
  • on_plugin_event hears the targeting plugin say whether it's on.

config/config.lua

The settings and their defaults. Only settings listed here are saved and loaded; see Settings for how saved values are merged. The template's has three parts:

return function()
    return {
        -- The runner (acr/runner.lua) and the ACR window.
        acr = {
            enabled = false,  -- run the rotation
            cc = true,        -- crowd-control targets whose break bar is up
            status = false,   -- show the status window
            auto_heal = 0,    -- use the heal skill below this health percent; 0 for never
            revive = false,   -- revive downed allies in reach
            builds = {},      -- the picked build per profession: { mesmer = "heal_chrono", ... }
            key_binds = {     -- virtual-key codes held together, up to three
                toggle_acr = {},
                toggle_cc = {},
            },
        },
        windows = {
            main_pos = Vec2(300, 200),
            status_pos = Vec2(300, 320),
            status_size = Vec2(0, 0), -- 0: the default size
        },

        -- Each build's settings, by file name (acrs/<name>.lua).
        condition_virtuoso = {
            auto_elite = true,
            sword4 = true,
            -- ...
        },
        power_mechanist = { ... },
        heal_chrono = { ... },
        condition_evoker = { ... },
    }
end
  • acr is what the runner and the windows read: whether the rotation runs, CC, auto heal, revive, the build picked for each profession and the key binds.
  • windows remembers where the windows are.
  • Every build has its own table under its file name, which the build gets as settings. A new build needs one here, even an empty one.

config/config_api.lua

Loads, returns and saves the settings, on top of the config module. It usually needs no changes.

local defaults = require("config.config")

local config_api = {}

local live

function config_api.init()
    live = config.init(defaults())
end

function config_api.save()
    config.save()
end

function config_api.cfg()
    return live
end

return config_api

Anywhere in the plugin, config_api.cfg() is the settings table: read a field to get a setting and assign one to change it. Changes are saved by themselves.

The windows

  • ACR: the builds for your current profession (the pick is saved per profession), buttons to equip the build's traits and skills, open its settings and open its gw2skills page, and the Enabled, Enabled CC and Status checkboxes. A warning icon lists utility skills on your bar the build doesn't use, an error icon weapons it doesn't play.
  • Settings: key binds to turn the rotation and CC on and off, the auto heal threshold, revive, and the build's own settings.
  • ACR Status: the build's name, and whether the rotation, CC and the targeting plugin are on.
The ACR window's close button hides the Lua plugin host, the way a plugin's own close button hides that plugin. The injector's plugin menu shows it again.

A plugin without the ACR

For a plugin that doesn't fight, keep main.lua and config/ and delete the rest. Then empty config/config.lua (return {} inside the function), drop the on_damage and on_plugin_event lines from main.lua, and start app.lua over from this:

local config_api = require("config.config_api")

local App = {}
App.__index = App

function App.new()
    return setmetatable({}, App)
end

function App:init()
    config_api.init()
end

function App:shutdown(reason)
end

function App:tick()
    config_api.save()
end

function App:render()
end

function App:on_key_pressed(key)
    if game_ui.text_box_has_focus() then
        return false
    end
    return false
end

return App.new()

Adding a window

Draw from App:render. A window with a close button tied to a setting needs two settings in config/config.lua:

return function()
    return {
        show_window = true,
        range = 1200.0,
    }
end

Then, in app.lua:

local cfg = config_api.cfg

function App:render()
    if not cfg().show_window then return end

    ui.set_next_window_size(Vec2(360, 200), ui.Cond.FirstUseEver)
    local open, visible = ui.begin_window(script.name, true)
    if visible then
        ui.text("Hello from " .. script.name)
        local changed
        changed, cfg().range = ui.slider_float("Range", cfg().range, 100, 1500, "%.0f")
    end
    ui.end_window()

    if open == false then
        cfg().show_window = false
    end
end

Save the file and press reload on the plugin's row to see it. Everything a plugin prints with print shows up in the Lua Console.