#How to summon mob with custom name from NBT storage?

1 messages · Page 1 of 1 (latest)

halcyon hullBOT
#

I'm using Minecraft 1.21.1 and trying to summon a villager with custom name from NBT storage (I'm trying to use it as constant registry and reuse the custom name later in other places)

Here's the commands I've tried to use:

data modify storage example:storage CustomName set value {"text":"Villager","color":"gold"}

summon minecraft:villager ~ ~ ~ {CustomName:'{"storage":"example:storage","nbt":"CustomName","interpret":true}',NoAI:true,PersistenceRequired:true}

It summons a villager but the name is empty. Am I missing something?

It works fine with the tellraw command:

data modify storage example:storage CustomName set value {"text":"Villager","color":"gold"}

tellraw @s ["§7[",{"storage":"example:storage","nbt":"CustomName","interpret":true},"§7]: ","Hello"]

Also, is it a good idea to use the storage for reusing this kind of data or there are better ways?

grave vector
#

yea text components don't get resolved in an entity name. If you want a constant registry, you might be looking for a tag. Add Tags:[example_tag] to your entity and then you can select your entity using @e[type=<entity_type>,tag=example_tag] (note that the type is not strictly necessary here but it's best to write it for performance reasons)

wintry kettle
grave vector
#

Oooh so you want to make the villager's name dynamically colorable/changeable through ingame events?

wintry kettle
#

Not really, I'm just making a villager as an NPC which is summoned once and who will tell something, but I don't want to repeat the color everywhere and I want to put it in some kind of constant. Maybe I'm overengineering here, since mcfunction is quite limited compared to regular programming languages xD

grave vector
#

It's best to always hardcode as much as possible in mcfunction. Using a storage here can cause some performance issues. Instead you can use a pre-compile like for example beet

#

this allows you to use python to generate mcfunction code

#

that way you can use variables and insert them wherever needed

wintry kettle
#

Thanks! I figured it'd be better to hardcode it here. I'll take a look at the project, it looks interesting!