I try to handle game's computer side inputs through an array. Each element in the array will be set as the state 'currentTarget' to trigger the animation in child components. I think that my approach is probably wrong, anyway I wonder if there is a mean to use the setTimeout on each element of my IAInputsQueue state.
This is my non-working code :
Parent logic :
useEffect(()=>{
if(!isPlayerTurn){
IAInputsQueue.forEach((element, index) => {
window.setTimeout(() =>{
setCurrentTarget && setCurrentTarget(element) //Typescript checking
},index *1000)
});
return () => IAInputsQueue.forEach(clearTimeout)
}
},[isPlayerTurn, IAInputsQueue, setCurrentTarget])
Child logic:
useEffect(() => {
if(currentTarget === uid){
animeButton()
musicPlay()
}
},[currentTarget, uid, musicPlay, animeButton])