# Uncaught RuntimeError: memory access out of bounds Error

**URL:** <https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596>\
**Category:** Technical Support\
**Tags:** studio\
**Created:** [November 15, 2024, 7:31am UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596 "2024-11-15T07:31:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 7:31am UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/1 "2024-11-15T07:31:05Z")

</div>

Hey I am getting this error when tracking the velocity of my entity. It is causing things to crash. Here is the state that is causing this error and crash:

```auto
ecs.defineState('moving')
      .onEnter(() => {
        ApplyForce()
      })
      .onTick(() => {
        ecs.physics.getLinearVelocity(world, dataAttribute.cursor(eid).player)
      })
  },

```

Here is the full error message:

Uncaught RuntimeError: memory access out of bounds  
at void std::\_\_2::\_\_tree\_balance\_after\_insert\<std::\_\_2::\_\_tree\_node\_base\<void\*\>_\>(std::\_\_2::\_\_tree\_node\_base\<void_\>_, std::\_\_2::\_\_tree\_node\_base\<void_\>\*) 003e03aa:wasm-function[65]:0x2b11  
at handleObserveEvent 003e03aa:wasm-function[64]:0x29b5  
at flecs\_observer\_invoke 003e03aa:wasm-function[1270]:0x82c52  
at flecs\_multi\_observer\_invoke 003e03aa:wasm-function[1271]:0x83154  
at flecs\_multi\_observer\_builtin\_run 003e03aa:wasm-function[1275]:0x847e2  
at flecs\_observer\_invoke 003e03aa:wasm-function[1270]:0x82c52  
at flecs\_observers\_invoke 003e03aa:wasm-function[1267]:0x82b08  
at flecs\_emit 003e03aa:wasm-function[1261]:0x818ef  
at flecs\_notify\_on\_set 003e03aa:wasm-function[1085]:0x73365  
at ecs\_modified\_id 003e03aa:wasm-function[1118]:0x763be

Is this a bad way to track the velocity? I need to know when my velocity is both past a certain speed and at 0. Those two factors change the state of the 3D model so they are kind of important.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 7:41am UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/2 "2024-11-15T07:41:44Z")

</div>

It may be because I have onTick running in multiple scripts simultaneously. Is there a best practice for this? Here are my 4 current scripts

**Camera Controller**

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

ecs.registerComponent({
  name: 'camera-controller',
  schema: {
    // Add data that can be configured on the component.
    playerEntity: ecs.eid, // this is the Player
  },
  schemaDefaults: {
    // Add defaults for the schema fields.
  },
  data: {
    // Add data that cannot be configured outside of the component.
  },
  stateMachine: ({world, eid, dataAttribute, schemaAttribute}) => {
    ecs.defineState('inGame').initial()
      .onTick(() => {
        const PlayerPositionY = ecs.Position.get(world,
          schemaAttribute.cursor(eid).playerEntity).y
        const cameraPosition = PlayerPositionY + 7
        ecs.Position.mutate(world, eid, (cursor) => {
          cursor.y = cameraPosition
        })
      })
  },
  add: (world, component) => {
    // Runs when the component is added to the world.
  },
  tick: (world, component) => {
    // Runs every frame.
  },
  remove: (world, component) => {
    // Runs when the component is removed from the world.
  },
})

```

**Player Controller**

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

ecs.registerComponent({
  name: 'player-controller',
  schema: {
    // Add data that can be configured on the component.
  },
  schemaDefaults: {
    // Add defaults for the schema fields.
  },
  data: {
    // Add data that cannot be configured outside of the component.
  },
  add: (world, component) => {
    // Runs when the component is added to the world.
  },
  stateMachine: ({world, eid, dataAttribute, schemaAttribute}) => {
    // Active state: When the expereince is active
    ecs.defineState('Standing').initial()
    ecs.defineState('Squating')
    ecs.defineState('Jumping')
    ecs.defineState('Falling')
    ecs.defineState('Laying')
  },
  tick: (world, component) => {
    // Runs every frame.
  },
  remove: (world, component) => {
    // Runs when the component is removed from the world.
  },
})

```

**Jump Controller**

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

ecs.registerComponent({
  name: 'jump-controller',
  schema: {
    jumpForce: ecs.f32, // this is the force we use to scale the jump power
    // @label Z Rotation Starting Point (in Degrees)
    zRotationStart: ecs.f32, // this is the starting z place of the rotation
    // @label Z Rotation Finishing Point (in Degrees)
    zRotationFinish: ecs.f32, // this is the starting z place of the rotation
    // @label Z Rotation Duration (in seconds)
    zRotationDuration: ecs.f32, // this is the starting z place of the rotation
    inputController: ecs.eid, // this is the input controller entity
  },
  schemaDefaults: {
    jumpForce: 20, // this is the default value of jump force
    zRotationStart: 30, // this is the default value of jump force
    zRotationFinish: 150, // this is the default value of jump force
    zRotationDuration: 1.5, // this is the default value of jump force

  },
  data: {
    movingLeft: ecs.boolean, // this is for the setting angle tick
    rotationPerMilisecond: ecs.f32, // this is the amount we rotate by mer milisecond
    previousZRotation: ecs.f32, // this is the previous z rotation
    jumpZAngle: ecs.f32, // this is the value of the z jump angle after set
    jumpForceFinal: ecs.f32, // this is the final jump force value
    player: ecs.eid, // this is the player
  },
  stateMachine: ({world, eid, schemaAttribute, dataAttribute}) => {
    const setValue = ecs.defineTrigger()
    const SetAngle = () => {
      dataAttribute.cursor(eid).jumpZAngle = dataAttribute.cursor(eid).previousZRotation
      setValue.trigger()
    }
    const SetPower = () => {
      dataAttribute.cursor(eid).jumpForceFinal = 1 * schemaAttribute.cursor(eid).jumpForce
      setValue.trigger()
    }
    const ApplyForce = () => {
      const horizontalForce = Math.cos(dataAttribute.cursor(eid).jumpZAngle *
      (Math.PI / 180)) * dataAttribute.cursor(eid).jumpForceFinal
      const verticalForce = Math.sin(dataAttribute.cursor(eid).jumpZAngle *
      (Math.PI / 180)) * dataAttribute.cursor(eid).jumpForceFinal
      ecs.physics.applyImpulse(world, dataAttribute.cursor(eid).player,
        horizontalForce, verticalForce, 0)
    }
    ecs.defineState('idle')
    ecs.defineState('settingAngle').initial()
      .onTrigger(setValue, 'settingPower')
      .listen(schemaAttribute.cursor(eid).inputController, 'Submit', SetAngle)
      .onEnter(() => {
        dataAttribute.cursor(eid).previousZRotation =
      schemaAttribute.cursor(eid).zRotationStart
        dataAttribute.cursor(eid).movingLeft = true
      })
      .onTick(() => {
        const {delta} = world.time // Get the time delta for smooth movement
        let newZRotation // this is the z rotation in degrees
        if (dataAttribute.cursor(eid).movingLeft === true) {
          newZRotation = dataAttribute.cursor(eid).previousZRotation +
          (delta * dataAttribute.cursor(eid).rotationPerMilisecond)
          if (newZRotation >= schemaAttribute.cursor(eid).zRotationFinish) {
            dataAttribute.cursor(eid).movingLeft = false
          }
        } else {
          newZRotation = dataAttribute.cursor(eid).previousZRotation -
          (delta * dataAttribute.cursor(eid).rotationPerMilisecond)
          if (newZRotation <= schemaAttribute.cursor(eid).zRotationStart) {
            dataAttribute.cursor(eid).movingLeft = true
          }
        }
        const newQuatRotaion = ecs.math.quat.zDegrees(newZRotation) // this is a Quaternion
        ecs.Quaternion.set(world, eid, newQuatRotaion)
        dataAttribute.cursor(eid).previousZRotation = newZRotation
      })
    ecs.defineState('settingPower')
      .onTrigger(setValue, 'moving')
      .listen(schemaAttribute.cursor(eid).inputController, 'Submit', SetPower)

    ecs.defineState('moving')
      .onEnter(() => {
        ApplyForce()
      })
      .onTick(() => {
        console.log(ecs.physics.getLinearVelocity(world, dataAttribute.cursor(eid).player).y)
      })
  },
  add: (world, component) => {
    component.data.movingLeft = true
    const rotationDistance = component.schema.zRotationFinish - component.schema.zRotationStart
    const actualDuration = component.schema.zRotationDuration * 1000
    const rotationPerMilisecond = rotationDistance / actualDuration
    component.data.rotationPerMilisecond = rotationPerMilisecond
    component.data.player = world.getParent(component.eid)
  },
  tick: (world, component) => {
  },
  remove: (world, component) => {
  },
})

```

**Input Controller**

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

// Register the InputController component
ecs.registerComponent({

  name: 'input-controller',
  schema: {
    // UI buttons for various actions
    // submitButton: ecs.eid, // Entity ID for UI button submit
  },
  data: {
    buttonClicked: ecs.boolean, //
  },

  // Define the state machine for handling input during different game phases
  stateMachine: ({world, eid, dataAttribute, schemaAttribute}) => {
    // Function to handle button input
    const handleClick = (buttonName) => {
      if (dataAttribute.cursor(eid).buttonClicked === false) {
        world.events.dispatch(eid, buttonName) // Dispatch button Clicked event
        dataAttribute.cursor(eid).buttonClicked = true
        console.log(`clicked ${buttonName}`)
      }
    }

    // Active state: When the expereince is active
    ecs.defineState('inGame').initial()
      .onTick(() => {
        if (world.input.getAction('Submit')) handleClick('Submit')
        else dataAttribute.cursor(eid).buttonClicked = false
      })
      // .listen(schemaAttribute.cursor(eid).submitButton,
      // ecs.input.SCREEN_TOUCH_START, () => handleClick('Submit'))
  },
})

```

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 8:03am UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/3 "2024-11-15T08:03:04Z")

</div>

Ok, after removing the tick lifecycle method from all of my scripts I stopped getting that error. I would still be interested in a potential best practice for tick mins and maxes.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 8:09am UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/4 "2024-11-15T08:09:45Z")

</div>

Never mind, it still occurs. Not every time but it does still happen.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 6:21pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/5 "2024-11-15T18:21:18Z")

</div>

@christoph_nia maybe you have some input?

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 9:13pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/6 "2024-11-15T21:13:40Z")

</div>

If anyone else wants to look at this here is a link to the project in its current state. Beware, the project is currently broken.

> **[Rage Game Playground | DCLN | 8th Wall](https://www.8thwall.com/dcln/rage-game-playground)**
>
> Work In Progress!!!

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 9:28pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/7 "2024-11-15T21:28:33Z")

</div>

I did figure out a little work around. I now only check velocity every .1 seconds instead of on every tick. that seems to help. I think it has to do with the frequency that I am checking velocity. that is a good temp work around.

---

<div class="post-metadata">

**Author:** ![christoph\_nia](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/christoph_nia/32/83_2.png) [@christoph\_nia](https://forum.8thwall.com/u/christoph_nia)\
**Post date:** [November 15, 2024, 9:30pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/8 "2024-11-15T21:30:41Z")

</div>

Using `.cursor` to read data is not exactly proper. I would recommend `dataAttribute.get`.

Ticks do not run simultaneously since Javascript is single-threaded.

From the stack trace you posted, it’s not clear which function call is failing.

Something like:

```auto
try {
  const {player} = dataAttribute.get(eid)
  try {
     ecs.physics.getLinearVelocity(world, player)
  } catch (err) {
    console.error('Failed to get velocity', err)
  }
} catch (err) {
  console.error('Failed to access data', err)
}

```

Will help narrow things down.

If that doesn’t have the error, maybe try switching back to `.cursor(eid)` to see if that does.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 9:39pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/9 "2024-11-15T21:39:18Z")

</div>

> [@christoph\_nia](#):
>
> ```auto
> try {
> const {player} = dataAttribute.get(eid)
> try {
> ecs.physics.getLinearVelocity(world, player)
> } catch (err) {
> console.error('Failed to get velocity', err)
> }
> } catch (err) {
> console.error('Failed to access data', err)
> }
> 
> ```

I tried that out with get and cursor and it was not catching but the same error was popping up.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 9:43pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/10 "2024-11-15T21:43:20Z")

</div>

here is my current fix that seems to be helping. I haven’t had an error since adding it.

```auto
 .onTick(() => {
        dataAttribute.get(eid).timeSinceLastUpdate += world.time.delta
        if (dataAttribute.cursor(eid).timeSinceLastUpdate >= 100) {
          const velocity = ecs.physics.getLinearVelocity(world, dataAttribute.get(eid).player)
          if (velocity.x === 0 && velocity.y < 0.001 && velocity.y >= 0) Grounded()
          dataAttribute.cursor(eid).timeSinceLastUpdate = 0
        }
      })

```

It may not be a long term solution but for now is working.

---

<div class="post-metadata">

**Author:** ![christoph\_nia](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/christoph_nia/32/83_2.png) [@christoph\_nia](https://forum.8thwall.com/u/christoph_nia)\
**Post date:** [November 15, 2024, 9:53pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/11 "2024-11-15T21:53:31Z")

</div>

> That means the error is actually coming from somewhere else

That must mean some other function is throwing the error. I’ll dig a little deeper.

> here is my current fix that seems to be helping

I’m happy it’s working but check out this version:

```auto
const data = dataAttribute.acquire(eid)

data.timeSinceLastUpdate += world.time.delta

if (data.timeSinceLastUpdate >= 100 ) {
  const velocity = ecs.physics.getLinearVelocity(data.player)
  if (velocity.x === 0 && velocity.y < 0.001 && velocity.y >= 0) {
    Grounded()
  }
  data.timeSinceLastUpdate = 0
}

dataAttribute.commit()

```

This version is bit better because we acquire the `data` cursor once, then use it for reads and writes, then commit it. Using this pattern more generally across your code will likely make it more stable.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 10:11pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/13 "2024-11-15T22:11:25Z")

</div>

I thought that adding in const data = dataAttribute.acquire(eid) to a state machine effected the ability to use a component on multiple entities. In the past if i did that it would break a component that was on more than 1 entity simultaneously.

---

<div class="post-metadata">

**Author:** ![christoph\_nia](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/christoph_nia/32/83_2.png) [@christoph\_nia](https://forum.8thwall.com/u/christoph_nia)\
**Post date:** [November 15, 2024, 10:18pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/14 "2024-11-15T22:18:16Z")

</div>

As long as `commit` is always called before the next time a cursor is attempted to be raised is made, it should be safe.

My initial code didn’t handle this exactly right either.

If my next `onEnter` callback when `Grounded()` is called, that might have conflicts. To fix this, I moved the `Grounded()` call after the commit:

```auto
const data = dataAttribute.acquire(eid)

data.timeSinceLastUpdate += world.time.delta

let isGrounded = false

if (data.timeSinceLastUpdate >= 100) {
  const velocity = ecs.physics.getLinearVelocity(world, data.player)
  if (velocity.x === 0 && velocity.y < 0.001 && velocity.y >= 0) {
    isGrounded = true
  }
  data.timeSinceLastUpdate = 0
}

dataAttribute.commit(eid)

// Call this after commit() so that when we switch to the other state, 
// we've already released the cursor
if (isGrounded) {
  Grounded()
}

```

---

<div class="post-metadata">

**Author:** ![christoph\_nia](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/christoph_nia/32/83_2.png) [@christoph\_nia](https://forum.8thwall.com/u/christoph_nia)\
**Post date:** [November 15, 2024, 10:19pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/15 "2024-11-15T22:19:32Z")

</div>

I’ve also located the cause of the memory corruption which will be resolved in the next release (possibly today)

---

<div class="post-metadata">

**Author:** ![christoph\_nia](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/christoph_nia/32/83_2.png) [@christoph\_nia](https://forum.8thwall.com/u/christoph_nia)\
**Post date:** [November 15, 2024, 10:20pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/16 "2024-11-15T22:20:27Z")

</div>

There was nothing actually wrong with your code, calling `getLinearVelocity` as many times as you did just triggered a bad case.

---

<div class="post-metadata">

**Author:** ![Declan\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/declan_johnson/32/2074_2.png) [@Declan\_Johnson](https://forum.8thwall.com/u/Declan_Johnson)\
**Post date:** [November 15, 2024, 10:21pm UTC](https://forum.8thwall.com/t/uncaught-runtimeerror-memory-access-out-of-bounds-error/3596/17 "2024-11-15T22:21:45Z")

</div>

Oh, awesome!

All that you explained makes total sense. I appreciate all the help on this. I’m excited for the release!
