2 Variables
Victor Westerlund edited this page 2026-08-22 11:48:43 +02:00

Variables

Variables are named integer values stored as fake score-holders in the _var scoreboard objective. The variable name is passed as k to most var: functions.

function var:set { k: "health_bonus", v: 20 }

Functions

Function Arguments Returns Purpose
var:set k, v void Create or update a variable
var:unset k void Remove a variable
var:isset k bool Test whether a variable exists
var:cp k, v void Copy one variable to another
var:mv k, v void Rename a variable
var:inc k void Increment a variable by 1
var:dec k void Decrement a variable by 1

var:set

Sets k to integer value v.

function var:set { k: "coins", v: 100 }

If the variable already exists, its value is overwritten.


var:unset

Removes k from the _var objective.

function var:unset { k: "coins" }

This removes one exact variable name. Wildcard and prefix matching are not available:

# This targets a literal variable named "example_*", not every example_ variable.
function var:unset { k: "example_*" }

var:isset

Returns true when k has a score in _var.

function var:isset { k: "coins" }
execute if function stdout:true run say coins exists
execute if function stdout:false run say coins is missing

The function returns a boolean result internally and updates mcfstd's boolean result register. The most reliable branch pattern for macro calls is to call var:isset first, then branch with stdout:true or stdout:false.


var:cp

Copies the value from source variable k to destination variable v.

function var:cp { k: "coins", v: "previous_coins" }

The destination is created if it does not already exist.


var:mv

Renames variable k to v.

function var:mv { k: "coins", v: "player_coins" }

Internally this copies the old variable to the new name, then unsets the old name.


var:inc and var:dec

Increment or decrement a variable by one.

function var:inc { k: "coins" }
function var:dec { k: "lives" }

These commands use scoreboard add and remove. If the score-holder does not already exist, vanilla scoreboard behavior creates it from zero before applying the change.


Naming guidance

Use a project prefix for variables that belong to your datapack:

function var:set { k: "arena_round", v: 1 }
function var:set { k: "arena_players_ready", v: 0 }

mcfstd uses internal score-holders beginning with _std_ in _reg. Avoid variable names beginning with _std_ unless you are intentionally interacting with internals.