Async SetInterval Issue (spawned asset)
I'm trying to have a loop on a spawned object. When it's spawned it's sent and receives:
const spawnAssetEvent = new CodeBlockEvent<[player: Player]>('setup', [PropTypes.Player]);
I've connected it and stored the player:
this.connectCodeBlockEvent(this.entity, spawnAssetEvent, (player: Player) => {
this.player = player;
console.log(`Player ${this.player.name.get()} received spawnAssetEvent`);
this.initialize();
});
My issue is as follows:
this.async.setInterval(() => {
this.worldtick(); }, 3000);I have several methods using this.player. The setInterval loop when placed in start() will do the loop but because the player isn't yet assigned by the spawnAssetEvent it errors out with:
Error Executing Async Callback:
TypeError: Cannot read properties of undefined
placing the setInterval inside the connectCodeBlockEvent to assure the player is assigned before the loop starts doesn't fire because it's "not part of the lifecycle".
I've checked out the other SetInterval Issue on the forum but am still having issues. Any help would be greatly appreciated.
I created a manager object that does a list of activespawns and only sends the message to items on the list. I changed my code and moved the gettickEvent inside the spawnAssetEvent to assure this.player was set.
start() { this.SPAWNMANAGER = this.props.SPAWNMANAGER!; const spawnAssetEvent = new CodeBlockEvent<[player: Player]>('setup', [PropTypes.Player]); this.connectCodeBlockEvent(this.entity, spawnAssetEvent, (player: Player) => { this.player = player this.initialize(); this.sendCodeBlockEvent(this.SPAWNMANAGER, registerSPAWNED, this.entity); this.connectCodeBlockEvent(this.entity, getTickEvent, this.worldtick.bind(this)); }); ///gives ownership errors on DefaultWhat ultimately fixed my issue was changing my script execution from Local to Default. yay emote?