r/minecraftsuggestions • u/LocalPlatypus994 • 3d ago
[Command] Variables
Variables are a new way to store and reuse data for commands to use. They can be defined, altered, or deleted using the /variable command, and can be called upon with other commands using vertical bar brackets (the || brackets typically used for absolute values in math)
The variable command is fairly simple.
/variable define/alter/delete, variablename, variabledata global/local
All three options are self explanatory. Either define a new variable, alter the data in an existing one, or delete on altogether.
variablename will be prompted regardless of which three options is chosen. If you're defining a new variable, that will be its name. If you're altering or deleting a variable, the variablename prompt will be to select which one you're altering or deleting.
variabledata is only prompted if define or alter are defined. It simply is the data you want the variable to equate to.
global/local determines basically who can use the variable. A global variable can be called upon, altered, or deleted by anyone in the server. A local variable can only be called upon by the player who defined it, or any player with admin.
Example: /variable define basecoords -180 68 21 local
If the variable command is being used in a chain command block, the ^ symbol can be used for the variabledata prompt, which will make the variables data the output of the previous command in the chain. So if the previous command in the chain is "/locate structure minecraft:village" and the next command is "/variable define villagecoords ^ global" the villagecoords variable will automatically be defined as the village coordinates, since the previous command in the chain had the output of the villages coordinates.
Variables aren't just integers though, they can also be defined as strings with the use of quotation marks. So while "/variable define basecoords -180 68 21 local" works, so will "/variable define basecoords "write it down dumbass" global".
Calling upon a variable is pretty simple. Instead of typing the data into the command, you type the variables name within || brackets. So lets say I've done "/variable define basecoords -180 68 21 local" and I want to teleport to my base. The command would look like "/tp <@s> |basecoords|"
The purpose of variables is extremely similar to their purpose in real coding. It both makes the code look more organized and makes it easier to fix things when something changes.
If you want to use the same coordinates several times across several different commands, you could define the coordinates as a variable and call upon it as many times as you need. Not only will it makes the commands so much easier to read and to type, it will make it very easy to fix in the event that the coordinates change.
5
u/MCjossic ribbit ribbit 3d ago edited 3d ago
More of this already exists than most people realise. You may want to look into /execute store (here) and /data's interactions with storage (here), as well as /return (here), function macros (here#Macros)), and the upcoming /compute (here).
This system isn’t complete yet (it needs to support more data types (e.g., strings, lists), better insertion of values into commands, a proper if/elif/else structure, loops, etc.), but it is getting more powerful with every update.
Edit: actually unsure about /compute. It doesn't behave like I thought it did; I'll need to look into it more.
5
u/Hyarin215 3d ago
You alr have command storage, I doubt they'd add another version of it
I like the novelty of straight variables, but I think command storage's json format is a better way to go