In command block scripting, do arrays (or even variables for that matter) exist?
I am unfamiliar with command block scripts in Minecraft. However, I hear command blocks merely execute commands that users can do in the chat window (which I am mildly familiar with). Therefore, I must ask:
In the context of computer science terms, do variables and arrays exist in the command block programming language, or has anyone implemented them if they didn't exist originally?
The reason why I ask is because the existence of the ability to create arrays of variable size gives one the power to create an array implementation of the heap (dynamic memory allocator) along with many other useful data structures in a programming sense (trees, lists, sets, mathematical functions, etc.).
In fact, that would essentially give one the ability to implement many things in a pure data structure sense. With that context, one would merely need to implement the "physics" and "graphics" of an entity.
Having this feature of arrays and knowing the syntax for indexing arrays would give me great power in a matter of seconds to change the nature of scripting as I understand by simply writing libraries of functions for people to use.
No, unfortunately there is nothing really quite like "variables" in Minecraft. However, there is the scoreboard, which you can use for certain things. If there's something specific you'd like to do, try asking that as well; you'll be surprised by the number of ways people have gotten around the absence of variables.
Here's a quick walk through of what you can do with the scoreboard:
/scoreboard objectives add name dummy - Adds objective name
/scoreboard players set|add|subtract entity objective value NBT Tag - Modifies objective score of entity, if entity's NBT data matches NBT Tag. NBT tag is optional.
/scoreboard players reset entity objective - Resets objective score if entity. Leave objective blank to reset all scores for this entity
Now for target selector arguments:
score_objective=value - Entities with a maximum objective score of value
score_objective_min=value - Entities with a minimum objective score of value
Hope this helped! Make sure to check out the Minecraft Wiki when in doubt. Here's a link to page on Scoreboards: Scoreboard - Official Minecraft Wiki
Minecraft commands do have a form of variables, but, unfortunately, they can only store numeric values. This is called the scoreboard, and you can add and access variables by using the /scoreboard
command. A list of all the functions you can preform is available here.
In the german Minecraft technic wiki you find "Arrays" with one dimension in this article, it is german, but the commands are listed:
https://technic-de.gamepedia.com/Schaltnetze/Direktzugriffsspeicher_(Befehle)
As of Minecraft Java Edition 1.15, storage
has now been introduced. This implements the concept of variables into Minecraft, though in a much peculiar way:
As you all know, the Named Binary Tag (NBT) format is used to store pretty much all data of your Minecraft world in its save files. The addition of storage
allows us to store arbitrary NBT in a safe location in our world. It's almost like an NBT sandbox where you can construct whatever NBT you would like!
To work effectively with NBT in storage, you should first have an understanding of /data modify
.
In NBT, arrays do exist, and you can make one like so:
data modify storage test:main MyList set value []
There are operations like append
, prepend
, and insert
which will allow you to modify your list in various ways:
{MyList: ["Hello"]}
data modify storage test:main MyList append value "World"
{MyList: ["Hello", "World"]}
{MyList: ["World"]}
data modify storage test:main MyList prepend value "Hello"
{MyList: ["Hello", "World"]}
{MyList: [1, 2, 4, 5]}
data modify storage test:main MyList insert 2 value 3
{MyList: [1, 2, 3, 4, 5]}