Getting a Reference to a Custom Component on the Same Entity

Hiyo, so I’m coming from the Unity side of things and one piece that I’m trying to understand is the way to communicate between scripts. Say I have Script 1 and Script 2 and I want to communicate between their instances as they are attached to the same entity. How do I do get the custom component reference of Script 1 in Script 2. Ex.

Script 2{
const component = ecs.Script1.get(world, component.eid)
}
This idea of getting a component causes a error when it’s a custom component so is there another way? Is there a getComponent function?

Thanks!

You would export out the component from where you define it, then import that exported reference into where you want to try to get it.

Take a look at this sample project:

This component is exported:

This component uses the exported one:

So, you’d be able to do MyComponent.has(world, eid) to see if eid has a MyComponent attached.

Hey George, for some reason that shows a error.

import * as ecs from '@8thwall/ecs'  // This is how you access the ecs library.
import * as imageTest from './assets/TestingAssets/backone.png'
import buttonclick from './ButtonClick'

const ButtonAction = ecs.registerComponent({
  name: 'ButtonAction',
  schema: {
    // Add data that can be configured on the component.
    TestImage: ecs.eid,

  },
  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.
    const {TestImage, eid} = component.schema

    const entity = TestImage

    const clicker = buttonclick.has(world, eid) //doesn't work
    console.log(clicker)

    world.events.addListener(world.events.globalId, 'buttonPressed', () => handleButtonPressed(world, entity))
  },
  tick: (world, component) => {
    // Runs every frame.
  },
  remove: (world, component) => {
    // Runs when the component is removed from the world.
  },
})

“buttonclick.has” is the error and buttonclick has been imported


const buttonclick = ecs.registerComponent({
  name: 'ButtonClick',
  schema: {
    Tstring: ecs.string,
    // 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.

    const entity = component.eid

    world.events.addListener(entity, 'click', () => ToPressButton(world, entity))

    // Store the reference to the Press function on the component data
  },
  tick: (world, component) => {
    // Runs every frame.
  },
  remove: (world, component) => {
    // Runs when the component is removed from the world.

  },
})

function ToPressButton(world, entity) {
  console.log('Button pressed')

  world.events.dispatch(entity, 'buttonPressed')
}

export {ToPressButton, pressed, buttonclick}

I’m not sure why it’s showing the error. My hope is to get a reference to Tstring which has text in the editor. Is there something I’m missing? Thanks!

What’s the error you’re seeing?