#how to make a mob with the nickname of the player who summoned it
1 messages · Page 1 of 1 (latest)
const entity = dimension.spawnEntity();
entity.nameTag = player.name;
I call with an egg
Name mob and player
What do you mean you call with an egg? You spawn an entity using spawn egg?
Yes
@ashen nova
Sorry I forgot. I'll try to make if what's on my mind will gonna work.
@wary pawn Here it is.
// The event we need to use to detect if player use a spawn egg to summon entity
// BeforeEvents is use to sync it with Promise function
world.beforeEvents.itemUseOn.subscribe((data) => {
const {itemStack, source} = data;
// Check if the item use is any spawn egg
if(!itemStack.typeId.endsWith('_spawn_egg')) return;
system.run(async() => {
// Call the Promise function that detect which entity will spawn
const entity = await getEntitySpawn();
// Setting nameTag of an entity to player's name
entity.nameTag = source.name;
});
});
// A Promise function to detect what entity will spawn
function getEntitySpawn() {
return new Promise((resolve) => {
world.afterEvents.entitySpawn.subscribe(data => {
const {entity, cause} = data;
if(cause === 'Spawned') {
// Resolve the entity so we can retrieve this when calling the function
resolve(entity);
}
});
});
}
Don't forget to unsubscribe from the afterEvent in the getEntitySpawn function
I also thought that, however idk how do I properly unsubscribed. I have never used this. Could you please teach me how?
Yeah, so the subscribe method takes a function as an argument to start the subscription and the unsubscribe actually takes the same argument to remove the subscription. Luckily subscribe also returns the subscription function, so you can store that in a variable and then pass it to unsubscribe when needed.
function getNextSpawnedEntity() {
return new Promise((resolve) => {
const spawnSubscription = world.afterEvents.entitySpawn.subscribe(
(event) => {
if (event.cause === EntityInitializationCause.Spawned) {
world.afterEvents.entitySpawn.unsubscribe(spawnSubscription);
resolve(event.entity);
}
},
);
});
}
I thought the subscribe returns void?
Well, thank you so much, I'll try this.
It works, it unsubscribe properly. I'm just confused because it says it returns void, but it's the arg that return void, right?
Yeah, the subscribe method takes the callback argument, that has type (arg: EntitySpawnAfterEvent) => void, which means "a function that accepts one argument that has the type EntitySpawnAfterEvent that is not guaranteed to return a value (void)" and returns (arg: EntitySpawnAfterEvent) => void
Thanks for explaining, I understand now.
Guys, can I ask you a quick question?
You already did.
....
Just kidding, go ahead.
It's like that and I wanted to know how I could execute a function after pressing a button on the interface
I thought you will ask something relevant to our discussions. Why don't you create a post for that?
Anyway here's a tutorial.
Okay, I'll create it
I did not find