# Listening to global events and setting data values

**URL:** https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628
**Category:** Technical Support
**Tags:** studio
**Created:** [November 18, 2024, 2:46pm UTC](https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628 "2024-11-18T14:46:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Saul\_Pena\_Gamero](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Saul\_Pena\_Gamero](https://forum.8thwall.com/u/Saul_Pena_Gamero)
#### Post date: [November 18, 2024, 2:46pm UTC](https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628/1 "2024-11-18T14:46:44Z")

</div>

Hello,  
I am running into an issue. I have this basic script that detects when my head rotates left or right and on tik fires events for each side. that portion works fine.

I also set up a global event listening to tapping on the screen. I can set up and receive the event that part works fine.

The part that I am having trouble is trying to set the values inside of data. I have this line  
“component.data.touchTriggeredLeft = true” but the value never seems to be set to true. inside the tik it never registers as being true. I tested that I can call the methods directly but even if I try to set the data from within the function it does not work.

I must be missing something that connects the component.data to the one object that set up and received the listeners. I tried printing the eid before and the numbers matched.

I would appreciate any help

//Code

import \* as ecs from ‘[@8thwall](https://forum.8thwall.com/groups/8thwall)/ecs’  
// Function to handle logic for the right side  
function handleRightSide(world, component) {  
console.log(`handleRightSide`)  
}

// Function to handle logic for the left side  
function handleLeftSide(world, component) {  
console.log(`handleLeftSide`)  
}

const faceTracking = ecs.registerComponent({  
name: ‘faceTracking’,  
schema: {  
},  
data: {  
touchTriggeredLeft: ecs.boolean,  
touchTriggeredRight: ecs.boolean,  
},  
add: (world, component) =\> {  
const { eid } = component  
const { scoreDisplay } = component.schema  
component.data.touchTriggeredLeft = false  
component.data.touchTriggeredRight = false

```
// Dispatch global events on touch
world.events.addListener(world.events.globalId, ecs.input.SCREEN_TOUCH_START, (event) => {
  const touchX = event.data?.position?.x
  console.log(`Touch Event Received: X=${component.eid}`)
  if (touchX < 0.5) {
    console.log("Dispatching 'touchLeft' event")
    world.events.dispatch(component.eid, 'touchLeft')
  } else {
    console.log("Dispatching 'touchRight' event")
    world.events.dispatch(world.events.globalId, 'touchRight')
  }
})

// Listen for global touch events within this component
world.events.addListener(component.eid, 'touchLeft', () => {
  console.log(`Touch Left Event Received X=${component.eid}`)
  component.data.touchTriggeredLeft = true
})

world.events.addListener(world.events.globalId, 'touchRight', () => {
  console.log(`Touch Right Event Received X=${component.eid}`)
  component.data.touchTriggeredRight = true
})

```

},  
tick: (world, component) =\> {  
const { killerArea } = component.schema  
const rotation = ecs.Quaternion.get(world, component.eid)

```
if (rotation) {
  const { z } = rotation

  // Handle right-side logic
  if (z > 0.20) {
      handleRightSide(world, component)
  } else if (z < -0.20) {
      handleLeftSide(world, component)
  } else {
    // Reset state when head returns to neutral
  }
}

// Handle touch-triggered logic
if (component.data.touchTriggeredRight) {
  console.log("Tick handling right touch")
  handleRightSide(world, component)
  component.data.touchTriggeredRight = false
}

if (component.data.touchTriggeredLeft) {
  console.log("Tick handling left touch")
  handleLeftSide(world, component)
  component.data.touchTriggeredLeft = false
}

```

},  
remove: () =\> {  
// No specific cleanup required  
},  
})

export { faceTracking }

---

<div class="post-metadata">

### Author: ![GeorgeButler](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/georgebutler/32/1903_2.png) [@GeorgeButler](https://forum.8thwall.com/u/GeorgeButler)
#### Post date: [November 18, 2024, 5:04pm UTC](https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628/2 "2024-11-18T17:04:22Z")

</div>

Component data shouldn’t be called directly inside of events. You could use `dataAttribute`. Our guide here should make it more clear.

> **[Common issues and best practices | 8th Wall](https://www.8thwall.com/docs/studio/guides/components/common-issues-and-best-practices/)**
>
> Common issues and best practices to follow when scripting custom components.

---

<div class="post-metadata">

### Author: ![Saul\_Pena\_Gamero](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Saul\_Pena\_Gamero](https://forum.8thwall.com/u/Saul_Pena_Gamero)
#### Post date: [November 19, 2024, 2:58pm UTC](https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628/3 "2024-11-19T14:58:51Z")

</div>

It works now. thank you, I understand it now 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/x8thwall/original/2X/9/9212a857458f1b03a6accb129e0f8cbea408e3e5.png) [@system](https://forum.8thwall.com/u/system)
#### Post date: [November 23, 2024, 2:59pm UTC](https://forum.8thwall.com/t/listening-to-global-events-and-setting-data-values/3628/4 "2024-11-23T14:59:46Z")

</div>

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