BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
Battlefield 2
On this page

Battlefield 2's HUD: nodes, variables and writers

BF2

The in-game HUD is not Flash. It is a tree of menu nodes (texts, pictures, bars, lists, a map) that the game's own scripts build at start, and every node shows a named variable. Code writes the variables; the nodes draw them. To know why a number on the screen is what it is, find its variable and the function that writes it.

Battlefield 2's HUD: the screen, and who writes what (BF2.exe) chat and kill messages ingameChat ChatManager 0x007a08c0 tickets and the round's time LevelTime 0x00464870 the minimap Minimap Bf2MenuMapNode 0x00780180 capture bar at a control point TimePreCPIsNeutral, TimePreCPIsTaken object markers, name tags, lock boxes 36 variables a marker node health: a percent PlayerHealthString 0x0078a910 stamina, stance, the squad PlayerStaminaShow hud_live.rs the weapon bar: 9 icons PlayerWeaponIcon1..9SelectShow vehicle seats and armor hud_vehicle.rs panel_values ammunition, clips, fire rate PrimaryClipString 0x007a5800 One tree of menu nodes drawn at 800 x 600 and scaled; every text, bar and picture is bound to a named variable.
Where things are, the variable behind each and who writes it. The HUD is laid out at 800 x 600 and scaled to the screen.

How it is built#

BF2HudManager::init (BF2.exe!0x00756ce0):

  1. makes the menu system at 800 x 600 (0x007af010), with its font and the texture atlas;
  2. creates the builder (0x00794e30) and the sub-managers: the spawn screen, the chat, the comm rose;
  3. loads the node file Menu/InGame as the root;
  4. runs Menu/HUD/HudSetup/HudSetupMain.con and the files it names: every HUD script;
  5. runs the name tags' script.

Each frame the renderer calls BF2HudManager::render (0x0074f110) as one of its last steps (the render pipeline).

The scripts are lines of the hudBuilder console object: 118 commands, 101 of them used by the game's own files. A line creates a node (createTextNode, createPictureNode, createBarNode, ...) or binds the node last made (setTextNodeStringVariable, setBarNodeValueVariable, setNodeShowVariable). A mod that moves a HUD element edits these files, not code.

The variables#

HudInformationLayer (0x00468dd0) is the table of variables: bool, int, float, string and wide string, each registered under a name (0x00466240). There are 2,043 of them:

Registered byHow manyExamples
the layer's own constructor425tickets, the round's time, the capture bars
BF2HudManager::init and its sub-objects359ShowIngameHud, BottomRightAlpha, the chat's
nodes, while the scripts run1,25736 for each object marker, 3 for each hover node, 42 for each map node
the engine, before the HUD exists2the blinding light's alpha and show flag

Two rules catch people: the name map keeps the first id registered for a name (a second registration is only logged), and a lookup by name with the wrong type finds nothing, so the builder's line fails.

The parts#

Health, stamina and the weapon panel#

The bottom-left block shows the health bar (hit points over the maximum) and a number that is a percent: the ceiling of the ratio times 100 (0x0078a910). The block hides when the soldier is dead or critically wounded. The bottom-right block shows the kit's items, the rounds in the magazine and the magazines left (0x007a5800), the fire rate's icon and a mounted gun's heat (a fire arm's heat).

Tickets and the round's time#

The ticket numbers blink while a team bleeds: two faders over the teams' bleed flags run every frame (0x00771a30). From their first state the alpha jumps to 1, falls to 0 and rises back within two seconds even without a bleed, then blinks while the team bleeds. The round's time (0x00464870) prints %d: hours when there are any, %02d: minutes and %02d seconds (server rules).

The capture bar#

Inside a control point's radius two bars show the flag's height: one for a point being made neutral, one for a point being taken (a control point's flag).

The minimap#

One node, Minimap (Bf2MenuMapNode, constructor 0x00780180), draws three maps: the minimap at the top right, the big map of the spawn screen, and the commander's map. It draws the control points with their names, the players, vehicles and strategic objects as icons, squad and commander orders, the supply lines, the combat area's shade and the satellite scan. A click on the spawn screen's map selects a spawn group; a click on the commander's mini map is handed to the HUD manager.

Chat and kill messages#

Two chat managers (0x007a08c0) fill two lists: the in-game one and the commander's. The in-game list has 4 rows for chat, 5 for kill messages and 3 and 2 for two more kinds, each kind in its own region; a line lives 15 seconds, a kill message 20. The lines also go to the console.

The scoreboard and the squad screen#

The scoreboard's rows are written by one function (0x007a4c80): per team the players' rows and the sums. The squad screen (0x00792100) lists the squads with their members and has the buttons to create, join and leave one.

The commander screen#

HudCommanderSquadManager (0x00794700) owns the commander's variables: the armour of the UAV, the satellite and the artillery, each asset's reload (the artillery and the supply drop 60 seconds, the vehicle drop by a console setting), the "no artillery / no UAV" marks when an asset is destroyed, and the list of squads to select and order.

How it is tested#

The original's HUD code was run in an emulator and ours beside it:

WhatResult
the builder: every object the scripts make, field for field0 differences, both mods
rendering: the draw calls of 742 HUD states1,151 of 1,152 frames identical; the one that differs reads an uninitialised value in the original
the kit variables of the spawn screen1,500 random template sets, 43,763 string writes equal
screen changes2,450 and 3,675 cases over every pair of screens
name tags5,248 draws equal

On top of that the HUD and menu sweeps play every screen in scenarios (test rigs).

In the code#

WhatWhere
nodes, the builder, rendering, the map, name tagscrates/bf2-hud (native, render, map, nametag, hil)
the writers that need the gamecrates/bf2-client/src/ (hud_live.rs, hud_writers.rs, hud_vehicle.rs, hud_hit.rs, spawn_screen.rs)
the tables of variables and their writersre/data/hud_variables.tsv, re/data/hud_variable_writers.tsv

Notes: notes/subsystems/hud.md. The other games: Battlefield 2142, Heroes.

The names over soldiers' heads are the renderer's, not the HUD's: name tags.

Source: docs/dev/ui/hud-bf2.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.