Skip to main content
ExoGuid
Honored Contributor
May 3, 2025
Solved

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.

 

ExoGuid
ExoGuidAnswer
Honored Contributor
May 3, 2025

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 Default

What ultimately fixed my issue was changing my script execution from Local to Default.  yay emote?

3 replies

ExoGuid
ExoGuidAuthorAnswer
Honored Contributor
May 3, 2025

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 Default

What ultimately fixed my issue was changing my script execution from Local to Default.  yay emote?

InaCentaur
Inspiring
May 5, 2025

i think local only applies to leaderboards and camera... spawning objects i believe has to be non-local? i'm still trying to understand the local vs default jargon myself, so hopefully someone can explain this if that's not correct

ExoGuid
ExoGuidAuthor
Honored Contributor
May 5, 2025

AFAIK local scripts run on the headset and default runs on the network.  So yes, cameras, HUDs and bits that run onUpdate should be local.   I made the switch not realizing it would jank up the rest of the code.