How to keep data between Add and Tick?

I am a beginner to JavaScript and wonder how to transfer data between the Add and Tick functions in Studio.
I thought that having something in the data schema would work, but when it changes in the VPS Found method in Add the data does not transfer to Tick. How to do this properly? To give some context, I want to run the tick whenever the VPS location is found.

Script:

// This is a component file. You can use this file to define a custom component for your project.
// This component will appear as a custom component in the editor.

import * as ecs from '@8thwall/ecs'  // This is how you access the ecs library.

ecs.registerComponent({
  name: 'LinearMovement',
  schema: {
    movableObject: ecs.eid,
    pointA: ecs.eid,
    pointB: ecs.eid,
    duration: ecs.f32,
    // Add data that can be configured on the component.
  },
  schemaDefaults: {
    duration: 20,
    // Add defaults for the schema fields.
  },
  data: {
    elapsedTime: ecs.f32,
    VPSFound: ecs.boolean,
    // Add data that cannot be configured outside of the component.
  },
  add: (world, component) => {
    component.data.VPSFound = false
    component.data.elapsedTime = 0
    // Runs when the component is added to the world.
    // component.schema
    const {pointA, movableObject} = component.schema
    const posA = ecs.Position.get(world, pointA)
    world.setPosition(movableObject, posA.x, posA.y, posA.z)

    // This runs when we find a VPS location
    const locationFound = () => {
      // Always false
      console.log(component.data.VPSFound)
      component.data.VPSFound = true

      console.log('VPS Location Found')
      // Always true
      console.log(component.data.VPSFound)
    }
    world.events.addListener(world.events.globalId, 'reality.locationfound', locationFound)
  },
  tick: (world, component) => {
    console.log(component.data.elapsedTime)

    // Always false
    console.log(component.data.VPSFound)

    if (component.data.VPSFound === false) {
      return
    }
    console.log('Okay, getting there')

    // Runs every frame.
    const {duration, pointA, pointB, movableObject} = component.schema
    // Retrieve position of pointA and pointB
    const posA = ecs.Position.get(world, pointA)
    const posB = ecs.Position.get(world, pointB)
    // Calculate interpolation factor (0 to 1)
    const t = Math.min(component.data.elapsedTime / duration, 1)

    // Interpolate each coordinate
    const newX = posA.x + (posB.x - posA.x) * t
    const newY = posA.y + (posB.y - posA.y) * t
    const newZ = posA.z + (posB.z - posA.z) * t
    console.log(newX)
    console.log(t)
    // Set the new position for movableObject
    world.setPosition(movableObject, newX, newY, newZ)

    component.data.elapsedTime += world.time.delta
  },
  remove: (world, component) => {
    // Runs when the component is removed from the world.
  },
})

Blockquote

Please have a look here:

You should not use component in a nested function directly:
β€œWhen your component is passed (world, component) to add , tick , or remove callbacks, it is not always safe to reference β€œcomponent” in a nested function and use it after the callback has returned.”

2 Likes

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.