Recipes
The first plugin hooks game code, which needs addresses you have to find
in eu4.exe. You can do a lot without that.
A plugin with no game addresses
Section titled “A plugin with no game addresses”A plugin without symbols.toml uses no game addresses of its own, so it needs no porting: when
EU4 updates, it works again as soon as Anvil does. What it knows about the game comes from the
standard plugin anvil-game: the date, the session, the main menu,
each frame and, from Anvil 1.11, provinces, countries and wars. One that
only reads, like this one, does not change the game, so it cannot put a multiplayer game out of
sync.
This one writes one line per game month to anvil/data/month-log/months.txt and logs each
session. From the template:
-
In
plugin.toml, removesymbolsandsettings, name the plugin as usual, and add the dependency.^1.6.0is the firstanvil-gamewith the date:anvil = ">=1.7.0"[[dependencies]]id = "anvil-game"version = "^1.6.0" -
Delete
symbols.toml,symbols.lockandsettings.toml. -
Replace
src/plugin.cpp:#include <anvil/anvil.hpp>#include <anvil/anvil_game.h>#include <format>#include <fstream>namespace {struct MonthLog : anvil::Plugin {const AnvilGameService* game = nullptr;std::filesystem::path file;int last_year = 0;unsigned last_month = 0;void load(anvil::Host& host) override {game = host.service<AnvilGameService>(ANVIL_GAME_SERVICE, ANVIL_GAME_SERVICE_VERSION);file = host.data_dir() / "months.txt";host.subscribe(ANVIL_GAME_EVENT_SESSION, [host](std::string_view, const void* payload) {const auto* session = static_cast<const AnvilGameSession*>(payload);if (session->change != ANVIL_GAME_SESSION_STARTED) return;host.info("session {}: ironman {}, multiplayer {}", session->number, session->ironman != 0, session->multiplayer != 0);});// The frame event runs on the main thread, so keep it short.host.subscribe(ANVIL_GAME_EVENT_FRAME, [host, this](std::string_view, const void*) {AnvilGameDate date{};if (!game->date(&date)) return; // the main menuif (date.year == last_year && date.month == last_month) return;last_year = date.year;last_month = date.month;std::ofstream(file, std::ios::app) << std::format("{}-{:02}\n", date.year, date.month);host.info("month {}-{:02}", date.year, date.month);});}};} // namespaceANVIL_PLUGIN(MonthLog) -
Build and deploy as in your first plugin.
-checkthen saysno symbols.toml (runs on every game build): OK, and CI needs nosymbols.lock. The dependency still ties it to the buildsanvil-gameknows; withoutanvil-game, a plugin that uses only the Windows API runs on every build.
affects_gameplay stays false, and Anvil leaves the plugin on in ironman and multiplayer.
The Windows API is yours to use too, for example to copy a file or watch a folder.
A window with your country in it
Section titled “A window with your country in it”From Anvil 1.11 a plugin with no game addresses can read the campaign and show a window. This
one shows the player’s country, refreshes each month and notes the latest war. It needs no mod
and no symbols.toml, and the game’s checksum stays the same.
The whole plugin is in the template, in
examples/campaign-panel:
-
Copy its
plugin.tomlover the template’s and deletesymbols.toml,symbols.lockandsettings.toml. It depends on both standard plugins:anvil = ">=1.11.0"[[dependencies]]id = "anvil-game"version = "^1.11.0"[[dependencies]]id = "anvil-overlay"version = "^1.11.0" -
Copy its
src/plugin.cpp:// The player's country in a window, with no game addresses: no symbols.toml, so// nothing to port when the game updates. It reads the campaign through// anvil-game, hears the month and the wars, and hands rows to the overlay.#include <anvil/anvil.hpp>#include <anvil/anvil_game.h>#include <anvil/anvil_panel.h>#include <format>#include <iterator>#include <string>namespace {struct CampaignPanel : anvil::Plugin {const AnvilGameWorldService* world = nullptr; // NULL on a game build whose port left the world outconst AnvilPanelService* panels = nullptr;AnvilPanel* panel = nullptr;std::string last_war = "none";// In a handler of an anvil-game event, or in a click: the only moments the// world answers.void refresh() {if (world == nullptr) {const AnvilPanelRow row{ANVIL_PANEL_TEXT, 0, "Campaign data is not available on this game version.", nullptr, 0};panels->set_rows(panel, &row, 1, sizeof(AnvilPanelRow));return;}AnvilGameTag me{};AnvilGameCountry country{};country.size = sizeof(country);if (world->player(&me) != ANVIL_OK || world->country(me.text, &country) != ANVIL_OK) return;// A port may leave the wars out alone: 0 would be a wrong answer.uint32_t wars = 0;const bool has_wars = world->war_ids(nullptr, 0, &wars) == ANVIL_OK;const std::string treasury = std::format("{:.1f}", country.treasury / 1000.0);const std::string income = std::format("{:.1f}", country.monthly_income / 1000.0);const std::string stability = std::format("{:+}", country.stability / 1000);const std::string provinces = std::to_string(country.province_count);const std::string running = has_wars ? std::to_string(wars) : "not on this game version";const AnvilPanelRow rows[] = {{ANVIL_PANEL_TEXT, 0, "Country", me.text, 0},{ANVIL_PANEL_TEXT, 0, "Treasury", treasury.c_str(), 0},{ANVIL_PANEL_TEXT, 0, "Income", income.c_str(), 0},{ANVIL_PANEL_TEXT, 0, "Stability", stability.c_str(), 0},{ANVIL_PANEL_TEXT, 0, "Provinces", provinces.c_str(), 0},{ANVIL_PANEL_SEPARATOR, 0, nullptr, nullptr, 0},{ANVIL_PANEL_TEXT, 0, "Wars in the world", running.c_str(), 0},{ANVIL_PANEL_TEXT, 0, "Latest", last_war.c_str(), 0},{ANVIL_PANEL_BUTTON, 0, "Refresh", nullptr, 1},};panels->set_rows(panel, rows, static_cast<uint32_t>(std::size(rows)), sizeof(AnvilPanelRow));}void load(anvil::Host& host) override {world = host.service<AnvilGameWorldService>(ANVIL_GAME_WORLD_SERVICE, ANVIL_GAME_WORLD_SERVICE_VERSION);panels = host.service<AnvilPanelService>(ANVIL_PANEL_SERVICE, ANVIL_PANEL_SERVICE_VERSION);if (panels == nullptr) {host.warn("no anvil-overlay.panel: this plugin needs Anvil 1.11 or newer");return;}if (world == nullptr) host.warn("no anvil-game.world: the port of anvil-game to this game build left it out");if (panels->create("My country", &panel) != ANVIL_OK) return;// A click runs after an outer frame, so it may read the world.panels->on_click(panel, [](void* self, uint64_t) { static_cast<CampaignPanel*>(self)->refresh(); }, this);// The window belongs to a campaign: in the main menu it would stand empty.host.subscribe(ANVIL_GAME_EVENT_SESSION, [this](std::string_view, const void* payload) {const bool started = static_cast<const AnvilGameSession*>(payload)->change == ANVIL_GAME_SESSION_STARTED;if (started) {last_war = "none";refresh();}panels->show(panel, started ? 1 : 0);});host.subscribe(ANVIL_GAME_EVENT_MONTH, [this](std::string_view, const void*) { refresh(); });host.subscribe(ANVIL_GAME_EVENT_WAR, [this](std::string_view, const void* payload) {const auto* war = static_cast<const AnvilGameWarChange*>(payload);last_war = std::format("{} {}", war->change == ANVIL_GAME_WAR_STARTED ? "began:" : "ended:", war->name);refresh();});}};} // namespaceANVIL_PLUGIN(CampaignPanel) -
Build and deploy as in your first plugin. The SDK in
CMakeLists.txtmust be Anvil 1.11 or newer; the template’s is.
Three rules keep it safe:
- The world answers only in the right moment: in a handler of an
anvil-gameevent, or in a panel’s click. Anywhere else it answersANVIL_ERROR_WRONG_PHASE. The game is in the middle of a day on any other thread. - Frames differ between the machines of a multiplayer game. These events are for showing and logging. They are not a clock for anything that must match between players.
- The service shows everything: provinces under terra incognita, and any country. In multiplayer that is more than a player sees, so a plugin that shows such values there does not enter the Plugins page.
The panel is drawn in Anvil’s overlay, not in the game’s own interface, so it looks like Anvil and not like EU4, and it is not drawn on the OpenGL renderer. Text is UTF-8 in any language; Chinese, Japanese and Korean come from the fonts Windows ships.
Where to go next
Section titled “Where to go next”| You want to | See |
|---|---|
| Know when the game is ready, or the main menu comes up | anvil-game events |
| Read provinces, countries and wars | The world |
| Show a window | Windows |
| React to a key or a mouse button the player picks | Keys (Anvil 1.18) |
| Show a value per province on the map | Map modes (Anvil 1.16) |
| Colour the map from a mod’s flags, variables or modifiers, or by development, with no code | Map modes from mod files (Anvil 1.26) |
| Have the player’s armies drill, merge or split, or choose an event’s option | Commands (Anvil 1.17) |
| Give mods an effect of your own, or run once a game day | Script effects and the day end (Anvil 1.16 and 1.18) |
| Keep files between starts | host.data_dir(), anvil/data/<plugin-id>/ |
| Let the player change something | settings.toml; the Anvil panel shows it |
| Change what the game does | Your first plugin and the Host API |
| Find a function on Steam and GOG | Steam and GOG in the template |
| Say what your plugin does in multiplayer and ironman | affects_gameplay |
| Give it to players | The Plugins page and the Steam Workshop |