5

I'm writing a Minecraft Datapack that adds "Abilities", each of which has its own function that's run once per tick. I have a scoreboard objective for each Ability, that determines whether any given player has that Ability unlocked. So, the Ability functions are run like this: execute as @r[scores={AbilityUnlocked=1}] run function datapack:ability (names changed for legibility, obviously).

Within these functions are almost entirely commands that are run with @a[scores={AbilityUnlocked=1}] selectors. My problem is that I don't want to have a new scoreboard objective for every single ability, so I just want to do instead execute as @a run function datapack:ability and change all the internal selectors to @s.

Here is my question: Is it more performance-intensive to do execute as @a run function datapack:ability, and change all the internal selectors to @s, or to do execute as @r[scores={AbilityUnlocked=1}] run function datapack:ability, with all the internal selectors as @a?

4
  • 1
    Is it possible to simply test both? That'd be the tack I take from a software engineering standpoint, anyway -- you're not looking for an answer in the abstract, you're looking for the answer in context to your specific situation. Which is still a fine question to ask, mind, but since you're the one with the code, you're best equipped to discover the comparatives yourself. Commented Apr 24 at 0:43
  • 1
    Yes, definitely. A general answer would probably depend on how efficient e.g. selectors vs. function pre-parsing are, both basically black boxes inside Minecraft's code that might change in any version. Test both in various situations. If you see no difference, just use whatever makes the code more reasonable. Or just start there, write readable and maintainable code first, then you can fix performance issues once you notice them. Commented Apr 24 at 4:55
  • 1
    @FabianRöling Thanks so much! That's what I had figured, so I'm going to try to limit how often I execute a function as multiple entities. I did some research and learned how to do performance profiling, and with the two commands: execute as @e[nbt={OnGround:1b}] run effect give @s glowing1 and effect give @e[nbt={OnGround:1b}] glowing, the first command came in at 1.86% of the total power used, with the second coming in at 1.80%. A small difference, but that makes sense, seeing as it was a single command. A whole function would certainly have a bigger impact.
    – Harth
    Commented Apr 24 at 18:25
  • 1
    @RavenDreamer You're right! I would've tested, but at the time of this post, I wasn't yet aware of performance profiling. Having learned about that, I was able to find some really good results. Thanks!
    – Harth
    Commented Apr 24 at 18:29

0

You must log in to answer this question.