Skip to content

Variables ​

Variables store program data. var declares a variable that you can assign again. Write its type after its name.

mcfpp
func main() {
    var lives: int = 3
    var bonus = 2
    const maxLives = 10
    external score: int = 0

    lives += bonus
    score += lives
    print(score)
}

lives: int declares an integer. bonus = 2 infers int from the initial value. For a later assignment, declare the type and initialize the variable before reading it.

Common types ​

TypePurposeExample
intIntegers and scoreboard arithmetic42
floatFloating-point arithmetic1.5
boolConditionstrue
stringStrings"hello"
textMinecraft chat text"hello"
nbtGeneral NBT data{count: 2}
list<T>Ordered elements[1, 2, 3]
dict<T>String keys and values{red: 1, blue: 2}
map<T>A mapping with accessible keysSee Map
entityA game entityObtained through entity APIs
selectorA Minecraft selector@a

Use data for named fields. Use ScoreRef and NBTRef<T> for game data addresses.

const and external ​

const permits one assignment. Use it for fixed settings and address references. A fixed reference can still update its target through set.

external requires runtime storage. The example's score uses runtime operations even with an initial value of 0.

The compiler can calculate known ordinary values:

mcfpp
func main() {
    var total = 5
    total += 7
    print(total)
}

The compiler can calculate 12 here. Runtime input causes it to generate commands for the calculation. const describes assignment count. A known value describes what the compiler knows.