1

I'm making a data pack to help me when localizing projects.

When using the new dialog UI and capturing the input, I cannot figure out how to test this string against a fixed value (or if it is empty or not).

After passing the macro variable from the UI, I want to test whether the variable is blank or if it is a specific variable. I've tried a bunch of variations of the following, but without success:

$execute unless $(translate_key_front)=[blank] anchored eyes positioned ^ ^ ^ if block ~ ~ ~ #minecraft:all_signs run function localizehelper:encode_translate_front {translate_key_front:$(translate_key_front)}

$execute unless $(translate_key_front)="" anchored eyes positioned ^ ^ ^ if block ~ ~ ~ #minecraft:all_signs run function localizehelper:encode_translate_front {translate_key_front:$(translate_key_front)}

Thank you in advance for pointing me to what I'm missing in my syntax.

1

1 Answer 1

2

1.20.2+

function macros are difficult to test since just like macros in actual programming languages, they replace the literal text of the command. Maybe in future Minecraft versions there would be a nice way to check values of macro arguments similar to what you have above (perhaps something like execute if literal <lhs> = <rhs> or some extension to scoreboard if score matches).

However, there is a more roundabout way to test if a macro variable is blank: by mangling a data value using the macro.

In short, we will summon an entity and run a command on it that will suceed with an empty argument and fail in any other case:

# summon a placeholder entity to hold data
summon marker ~ ~ ~ {Tags:["localizehelper_temp"],data:{var:1}}

# the conditional command to run if $(translate_key_front) is not empty
$execute unless data entity @n[tag=localizehelper_temp] data."var$(translate_key_front)" run <the command>

#cleanup
kill @e[tag=localizehelper_temp]

The first command summons a marker with data stored in var. The second command searches for a specific variable in this marker. If translate_key_front is empty, it will look for data.var which was defined. On any other case, the value of translate_key_front is added to the name var, which definitly does not exist, causing the command to return false. That way we got the control flow we want, although at the cost of summoning a temporary marker.

2
  • Awesome. Thank you for this description. I agree. I wish there was more variable testing available without resorting to summoning extra entities just to check information. Commented Sep 13 at 19:15
  • I was still getting a funky syntax error with this command. However, I figured out that the problem I was experiencing was that any variables that might be empty needs to be in quote marks. The parser really, really does not like unquoted, empty variables. Thank you so much for your help!!!! This syntax is really confusing to a newbie like me. Commented Sep 16 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.