My boolean does not update inside Tick

I have a simple script with an if statement that if true or false in the tick will determine the outcome. However, once a a function is called to switch the boolean, the tick’s boolean does not change.

The following is my simple script
import * as ecs from ‘@8thwall/ecs’ // Import the ECS library

// Function to change the lock value and update the component’s state
const setLock = (component, value) => {
component.data.lock = value // Update the lock state
}

// Component attached to Object A that will move Object B upon collision
ecs.registerComponent({
name: ‘LockPosition’, // Name of the component
schema: {
targetEntity: ecs.eid, // Entity ID of Object B to be moved
},
data: {
lock: ecs.f32, // Use number instead of boolean for lock state
},

add: (world, component) => {
const {targetEntity} = component.schema

// Initialize lock as 1 when component is added
component.data.lock = 1  // Default to locked state

// Check if the target entity is valid
if (!targetEntity) {
  return
}

// Listen for 'lock' event to set the lock to 1
world.events.addListener(world.events.globalId, 'lock', () => {
  setLock(component, 1)  // Call the global setLock function
})

// Listen for 'unlock' event to set the lock to 0
world.events.addListener(world.events.globalId, 'unlock', () => {
  setLock(component, 0)  // Call the global setLock function
})

},

tick: (world, component) => {
// Log the current value of the lock to ensure it’s being updated

if (component.data.lock === 1) {
  const {targetEntity} = component.schema
  if (targetEntity) {
    world.setPosition(targetEntity, -1.07, 0.441, -5.02)  // Move Object B
    console.log('Moving targetEntity to (0, 0.441, -1.13)')
  } else {
    console.error('Target entity is invalid or missing.')
  }
}

},

remove: (world, component) => {
// Clean up event listeners when the component is removed
world.events.removeListener(world.events.globalId, ‘lock’)
world.events.removeListener(world.events.globalId, ‘unlock’)
},
})

Take a look at this page

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