In general this was pretty accurate! Although a few notes to make: - clear is actually super laggy, except for your mentioned use of replacing NBT, you should avoid running it every tick. - tellraw is not as efficient as you mentioned, although you're right it's useful and can replace worse alternatives. - sort= is quite inefficient especially if you're using it on @e - The difference between tag= and scores= is negligible
Amazing, I've been looking for a complete general guideline like this for years! Cool tip about clear for detecting items. Also I guess modify storage isn't as bad as I thought, I never used it b/c I assumed it would be just as bad as modify entity. I always knew nbt= was bad, but now I can can replace a couple of my uses of it. nice!
@@Cl0udWolf what if i call a single @e telling every entity to run a function which writes some of their data into a storage location, then take that data and test and modify each entity's data as i wish using recursion on a list, then run commands according to the results of the nbt tests? Would it be better or same or even worse than using @e[nbt=…] for each test?
@@Cl0udWolf concrete example: say i want to copy a mob's UUID to multiple storage locations. Would it be better to target the mob once and then run a function with the copy commands in it, using data modify storage … set from entity @s UUID? Or should I copy it to a storage location once, then copy to the other storage locations using that storage copy? So it would instead become data modify storage … set from storage [specific id] CopiedUUID
I luckily know all of this. Especially predicates are very useful and efficient. You can combine many conditions and use only 1 command to test for it. There are "if" and "unless" in execute but there isn't "or". That's one of the main reasons why I prefer predicates because instead of checking if something is true, I can check from a list if any of them true with "minecraft:alternative" condition
Coming back to this to say: this video led me down a rabbit hole of cleaning up my data packs and making as many things run as effeciently as I could. It's at a point where I only have 2 looping functions (other than the main gameloop which triggers them), and literally everything else is triggered by advancements or another function that's triggered by an advancement.
I would say nbt should be gated as thoroughly as possible. Right, if you want a function to trigger when a mob is hurt, don't do this: execute as @e[nbt={HurtTime:1s}] run function mobs:mob_hurt Instead you should do something like this: execute as @s[tag=reactsToPain, nbt={HurtTime:1s}] run function mobs:mob_hurt And have a separate calling function to filter out entities that aren't mobs using @e and such, using it to do everything related to updating mobs. Try to use @e at the highest possible level, as close to tick.json as you can, branching off into @s from there, and nbt only on entities that absolutely need it and there's no other way.
As @s isn't bad. Sure, if you just do execute as @s run stuff it just does nothing, but if you do execute as @s[some specifier here] that is a lot better. And keep in mind not every command gives you a specifier to play with on its own. For example, function does not let you specify the caller(s). The current executor is the caller and the only parameter that function gives you is what function to execute.
The most efficient way to run a command is obviously to do /execute as @e[type=player, sort=nearest, limit=1, nbt={}] at @s as @s run execute run say hi
summon: find entity by id call constructor (or other function / lambda / method, idk) set nbt (and position) kill: find entity(-ies) call magic-kill-function-which-i-dont-searched-for on its, remove it from entity map and light the callback on entities which often contain loot summoning
This is really helpful. I've been transitioning from using command blocks for years and just using what ever existed. But now that I am using datapacks, it's good to know what to optimize for performance. Thanks for this :D
Would've been interesting to see what you have to say about /spreadplayers. I've noticed that it actually can be very laggy if it can't find a viable space for teleportation.
Ya I believe that spreadplayers does something where it randomizes where u can spawn, check the location, if it’s false re randomize. So if spreadplayers can’t find a valid spot it must go to all spots and check if they r valid which is a time consuming thing
I noticed that while doing my custom structures, underground structures would just freeze the game for a second or so if there're no caves in that chunk.
Note, clear can be laggier than `nbt=` from some testing I've done. Mostly this occurs when you are clearing items with specific nbt! There are instances of storage being faster than scoreboards, but mostly it's slightly slower since usually you are doing large copies.
From what I've seen in testing, tags are better than scores and the method of comparison is MUCH more efficient than "string comparison to everything on a list." My guess would be that there is a tag table and a boolean list but I'm not sure. They used to be part of the scoreboards so it would make sense tags would be ultra-efficient.
I asked this recently in the command discord channel: What is more efficient (for the computer): a. Area effect clouds with custom Duration b. Marker with a countdown scoreboard For example, a spell that summons a buff area for a certain amount of time. And if instead you also have to trigger things after a certain amount of time: - a and check for Age - b and check for scoreboard For example, a spell that summons a charged for a certain amount of time. This ball summons a lightning every second (check age 0,20,40,.. or scoreboard). What would you say the best implementations are?
Scoreboard and markers. With the aec u would still need some trigger command which involves nbt and markers is a trigger command involving scoreboards. Markers r more efficient and scoreboard is more efficient
“predicates require commands to run” pain also: `tellraw` being A tear doesn't seem right, surprisingly it actually has a big overhead if you spam it a little too much, and `teleport` is also quite bad
@@Cl0udWolf I mean, while people see predicates as that command only thing it's just a very broad syntax that's being used in loot tables and in advancements
function need to: find code block from map (as far as i know, datapack loads all the stuff but otherwise it will be searching through the file system which is more expensive) set various things like self selector (@s), position, maybe rotation, dimension and other stuff
I am required to use NBT to detect an item in a player's hand for a game I'm working on, but there are only two potential commands per item-check. Would it be more efficient to just use two NBT checks for the item, or only use one NBT check to add a tag, then run the rest of the commands with the tag? It's two additional commands for adding and subsequently removing a tag, but it seems like it would still be more efficient. If you could confirm I'd really appreciate it, since I need to reuse this mechanism a lot, so the differences will add up.
Nice video, very interesting! I was a bit confused with the difference between scores= and tag= selector, since the tag is really meant for selecting, I assumed it to be more efficient. Also the score needs to check the name of the score AND the value, while the tag only needs to check the name. I decided to go for some testing and have to conclude all off them are too efficient or I don't know how to test it. Using say command, all the load came from the say command itself, so I eventually did testing by changing a score. 15000 marker entities, selecting 1000 of them or all of them -> instant (even with @e[type=marker] 1200 marker armorstands -> my computer slowed down to 1fps, but changing a score on 250 of them took the same time as typing 1 letter in chat...
As I said in the vid idk the diff between them cause they r pretty fast on their own. Scores r typically better in general cause u can check them easier in predicates
Is it better to use an area effect cloud and check his age with nbt=, or use a marker with a scoreboard to have a timer. The advantage of AEC is that i can summon them with a certain duration so i don’t need to kill them
Where would you place /give, /effect, /replaceitem, and /attribute? Edit: what about /playsound and /particle? I'm just going down the list of miscellaneous commands I use all the time lol
oh particle is a good one, i dont use playsound every tick so i dont know and i dont use those others every tick too often so id just put them middle, they arent super inneficient but arent known for their efficeincy.particle tho is quite innefficeint or i would just say laggy cause particle rendering can be laggy on lower end computers
I've always wondered is if there was a difference in performance between the commands /execute if entity @s[] run and /execute as @s[] run . I would always go for "as @s" because its shorter and does the same thing as "if entity @s". Is there a difference in there performance or are they just about the same?
they r probably similar but if entity @s is probably more efficient giving as is meant to change the executor of the command and if entity just returns true or false
ppl that know what theyre doing avoid as many @e and nbt based commands as possible, because theyre performance is very poor. lag doesnt mean i run one command and my game freeze, it means that it scales poorly.
How efficient is the /kill command for players? Just curious since it doesn't have to delete all data, but with different conditions like Keepinventory it would probably vary.
Woah. I guess there's no preprocessor that goes through the functions and loads relevant files based off of function calls. It makes sense that since the function is in a separate file it wouldn't be as readily available in memory without some more direct indication that it should be loaded written into the file the call is being made from. Very interesting. Still a situation where I'd pick organization over performance though.
Great stuff! But you muddled the commands' comparison with the selectors. Any command will be more expensive if you run it multiple times, that is on the selector used not the command itself. We should assume the commands are properly branching to only use @s as soon as possible in the call stack.
wait, when nbt became plain string? it has it's own class and children. But... you still need to find entity, then correct node (selector can contain a lot of stuff like matching ( a[{Slot:109b}] ) ) and parse the input value then edit nbt
Hey I was just watching one of your videos on launching items and I got a little bit confused on how to actually get it to register. Could you help me by any chance?
Actually scoreboard commands are not that great, because every single scoreboard command that changes a score causes the minecraft server to send the updated score to every player that is online. This is especially ineficient since scoreboard operation commands usually come in a large batch in order to calculate some mathematical term; a single update packet at the end of the tick would be sufficient. Also hidden scoreboard player updates get broadcasted. Disclaimer: I checked the code on some snapshot that was release this year, but I don't think the code changed in the meantime.
That’s more going to server, but what is ur alternative? There r no commands that can do calculations and doesn’t get broadcasted to users. Literally the Only thing in Minecraft (which just got added) that is server side only r marker entities.
@@Cl0udWolf There is no alternative. As I understand, that's not really what your video is about, so I figured this fact is worth mentioning. Would you elaborate on your second sentence?
@@Cl0udWolf Obviously yes, but how is that related to anything what I wrote? My entire point was that scoreboard commands send way more packets than necessary.
@@unbekannt5089 every command will be sending extra packets and it’s not more than necessary cause each user could use a scoreboard command and then they need to know the score. I focused on just minecraft single player stuff and mentioning what is important to know when u could use something else instead
This is very informative but makes me feel bad cus i use *summon area effect cloud* + *data modify entity set from* when you pick up items to display how many points you got 😭. Since you cannot summon an entity with scoreboards in their name. Thankfully i don't get lag though
Is there any difference in efficiency with "execute if score @s namespace matches #" vs "execute if entity @s[scores={namespace=#}]"? is one better than the other or does it depend on the context
Regarding "as @s", sometimes when i put a function in a function the executor of the second one doesn't exist, so i have to put as @s at @s to run it, idk why but yeah
No that isn’t necessary at @s does do something and just doing at @s would be enough. As @s does nothing and adds no logic unless it’s on a chain being run from a server and u don’t want players to run it
Oh, I thought I saw on MinecraftCommands at some point limit= will make it break out of the loop and not do any more checks once it found enough. Will a selector check all it's conditions all the time? As in is execute as @e[type=marker, scores={...}, nbt={...}] worse than execute as @e[type=marker, scores={...}] if entity @e[nbt={...}]? #1 would be worse if it checks nbt for literally every entity, but if it filters for type and score first anyways and skips the nbt check for entities not matching the first 2 conditions, I assume they'd be quite similar
depending on what order u put the limit=1. so if u put type=marker,limit=1, it needs to g oto all of them and find the markers, then it will break out of the latter checks. ive discussed the whole order thing before, but @e[tag=test,limit=1] is different from @e[limit=1,tag=test] and the first one is less efficient in terms of what the @ is doing (not factoring how many it ends up picking)
I'm very sad how tags are less efficient than scores. You're only keeping track of a tag, why can't they store tags on a binary tree to search tags more quickly. You don't need to go through the setting up scores, also polluting your /scoreboard list with tons of tag scores. If only limit=1 would just pick from the entity list arbitrarily before creating a list. Like, don't they just have an internal list of loaded entities already? Why do they need to make another list. Even sort=... + Limit=1 Could be optimized as a special case. A bit sad that function has weird overhead. Instead of writing clean code. I feel more like dumping my entire program in a single function file only branching whenever I need to. Instead of clean OOP files... Also, tellraw is such a sweet print debug function as well as just nice text output. Doesn't have to run each tick. But eh doing @a and then tellraw @s will have to parse the command for each player instead of tellraw @a I assume. But in certain cases you're already in that context and it's so tempting. Ugh. Why no JSON text caches Mojang? Very sad Mojang never made these things perform well. Some of it is really useful or necessary in commands but it's cumbersome to use.
It’s all relative. Tags and scores r about the same efficiency for like selectors, but not inside predicates and other places. Functions r still fast just not as fast as they could be
I'm curious if there is any lag created by having mobs with custom NBT but not running a command to change that NBT. When you're talking about NBT I presume you're talking about altering something's NBT if I'm correct?
@@Cl0udWolf So to modulate how many creatures are spawned what would be an efficient method? Considering that sometimes loads may be killed by players and sometimes very few may be
So about the "using_item" trigger for advancements what would be the most efficient way to differ itens (for example an ender eye that is a gun and another that is another gun) and also only apply it to specific players? I'm currently using a custom nbt when giving the item and using tags to mark players but i'm not sure if it will be an efficient way when I add lots of diferent itens, here's an example of what im currently using: { "criteria": { "requirement": { "trigger": "minecraft:using_item", "conditions": { "item": { "items": ["minecraft:ender_eye"], "nbt": "{selected:1b}" }, "player": { "nbt": "{Tags:[\"ready\""]}" } } } }, "rewards": { "function": "test:test" } }
I would check for a nbt tag to know if it’s a gun idk what the tag of ready is for, as for different guns it depends on ur system. My current project uses a diverse stat system so the code doesn’t care if it looks like an AK or an m1911- all that matters is that it reads the intrinsic nbt stats in the command and applies the logic from there
@@Cl0udWolf Im going through a similar route, but i was gonna create a different achievement for each weapon (thats why i used the selected nbt on the item). The ready tag is because there are some specific times when I want the player to not be allowed to shoot, for example when reloading or for a period after he shot once, since i dont want players shooting 20 times a second. Is that a bad way of doing things? The "ready" tag will also be used for another things.
@@duarteferreira8176 I typically just let them shoot and use scoreboard checks to figure out what to do and why they can’t shoot to help give some info to the user why they can’t shoot
I see, well for my case I don't really need to tell the players why they can't shoot cause everytime they can't there will be something visual happening so they'll now. Another question I have is when you say that clear can delete itens that you have already clicked in were you talking about itens that are selected and therefore are not anymore in your inventory? Cause I tested it in 1.17 and it didn't work. Is there a right way of doing it, cause i could really use something like that. Anyway thanks a lot for taking time to answer my questions and for making these awesome videos, they are being very helpful.
@@duarteferreira8176 I still think using a tag is redundant and harder to debug. As for clear it can clear items that u have clicked on to move around in ur inventory
I have a question: how can I disable the possibility of throwing an eye of I have a question: how can I disable the possibility of throwing an eye of ender? (in a normal world)