STUDIO: create entity and attach script to it

Hey everyone! I’m creating a simple “enemy spawner” using the new Niantic studio. When I create and entity, I can add the properties at the same time, but I’d rather create and entity and attach a new script and within that - populate all the properties to keep it separate.

Any thoughts? Is this even possible yet?

Thanks in advance :slight_smile:

1 Like

Hi Sam!

Thanks for the question! I’m in the process of updating our documentation to clarify this feature along with several others. I’ve included a preview of the upcoming docs below, which should help answer your questions.

Creating an Entity

The following example shows how to create a new entity without any components:

import * as ecs from '@8thwall/ecs'

const eid = world.createEntity()

Deleting an Entity

The following example shows how to delete an existing entity given its id:

import * as ecs from '@8thwall/ecs'

world.deleteEntity(eid)

Adding Components to an Entity

The following example shows how to add a built-in component to an entity at runtime.

const box = world.createEntity()

ecs.BoxGeometry.set(world, box, {
    width: 1,
    height: 1,
    depth: 1,
})

ecs.Material.set(world, box, {
    r: 255,
    g: 255,
    b: 255,
})

Create and Modify Relationships

The following examples show how you can use build-in helper methods to create or change relationships between entities.

// Entities are automatically a child of World.
const foo = world.createEntity()
const bar = world.createEntity()

// Set foo to be a child of bar.
world.setParent(foo, bar)

// Get the parent of bar. (returns an eid where <= 0 is undefined)
world.getParent(bar)

// Get the children of foo. (returns an array of child eids)
world.getChildren(foo)

Adding Custom Components to an Entity

The following example shows how to add a custom Component to an existing entity.

Example

:information_source: Make sure you export the Component before attempting to import or use it anywhere.

import * as ecs from '@8thwall/ecs'

const CustomComponent = ecs.registerComponent({
  name: 'custom-component',
  schema: {
   foo: ecs.string
  },
  schemaDefaults: {
  },
  data: {
  },
  add: (world, component) => {
  },
  tick: (world, component) => {
  },
  remove: (world, component) => {
  },
})

export {CustomComponent}
import CustomComponent from './custom-component' // The name of the file without the file type.

const demo = world.createEntity()

CustomComponent.set(world, demo, {
   foo: 'bar'
})
1 Like

HI @GeorgeButler
About this code

// Get the children of foo. (returns an array of child eids)
world.getChildren(foo)

I checked it’s not an normal array. I can’t get length’s it!
Here my code for check:

console.log(is array? ${Array.isArray(world.getChildren(foo))})

Apologies for the confusion, and thanks for bringing that up!

The return type is actually a Generator.

If you convert it to a standard array using Array.from(), you can work with it in a more familiar way:

Array.from(world.getChildren(eid))

Hope that helps!

1 Like