#how to synchronise 2 events

1 messages · Page 1 of 1 (latest)

vital forge
#

import { world} from "@minecraft/server";
import {
ActionFormData,
MessageFormData,
ModalFormData
} from "@minecraft/server-ui";

let r;

world.events.itemUse.subscribe(async function(evt){
if(evt.item.typeId === "ar:speer")
{
r = evt.source.rotation
}
})

world.events.entityCreate.subscribe((evt)=>{
if(evt.entity.typeId === "ar:thrown_speer")
{
evt.entity.setRotation(r.x,r.y)
}
})

when the item "ar:speer" is used it's spawn an entity called "ar:thrown_speer", what i want to do is set the rotation of "ar:thrown_speer" to the rotation of the player when it spawns but it doesn't work the way i did,can someone tell how to do it?

reef laurel
#

try check if entity is spawned on that postition where u click

#

and unsubscribe after u sure that entity is spawned

vital forge
#

or did i not understand you correctly?

patent halo
#

@vital forge how do you spawn the entity, is it within the item code itself?

vital forge
#

yes

#

with the projectile component

patent halo
#

i see

#

why do you need to set the rotation @vital forge , the projectile should automatically go in the correct direction, is it an esthetical problem?

vital forge
#

yeah and now that i think about,i think i could do that through animations

patent halo
#

possible, what i know is that then you could try to get the velocity vector of the projectile and rotate it based on that

#

you would need to only listen to entity create event and then maybe wait for a tick if the velocity isnt present in the spawn tick already

#

then using some goniometry you should get the correct angle using the vectors

#

essentially if you would want it the way of actually syncing the events, the most important part is timing

#

you should do some testing and trial and error to see what tick does which event occur on and in which order, then try to connect it using some data structure or variables that both callbacks have access to

vital forge
#

using runcommandasnyc i found out that itemUse executes before entityCreate,i tried to make a variable that would share the data between these 2 events but it didn't work no matter what order,i thought one could maybe do it through some async and await magic but i don't know how to use those

reef laurel
#

ya, like P@vel said entityCreate fires before projectile is in movement so u should wait one tick for velocity apply

fallow glenBOT
reef laurel
#

Here is some documentations for async/await magic