Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to push / pop from a list? #112

Open
watch-janick opened this issue Mar 25, 2023 · 1 comment
Open

How to push / pop from a list? #112

watch-janick opened this issue Mar 25, 2023 · 1 comment

Comments

@watch-janick
Copy link

I'm trying to build a component to store effects that can be applied on any character, think of it as a buff / debuff in a RPG game.
How would you manage to add / remove those buffs / debuffs?
Component:

import { defineComponent, Types } from 'bitecs'

const Effect = defineComponent({
  effects: [Types.ui8],
})

export default Effect

System:

import { defineQuery } from 'bitecs'

const effectQuery = defineQuery([Effect])

const effectSystem = (world: IMyWorld) => {
  const entities = effectQuery(world)
  entities.forEach((entity) => {
    Effect.effects[entity].forEach((effect) => {
      // remove effect if some condition is met
    })

    // if some condition is met, add an effect)
    if (true) {
      const effectId = 12
      Effect.effects[entity].push(effectId)
    }
  })
}

Thanks!

@NateTheGreatt
Copy link
Owner

if you don't mind defining a max amount of effects you can do something like this:

const DynamicArrayComponent = defineComponent({
  someDynamicArray: {
    array: [f32, 12],
    cursor: ui8,
  }
})


const push = (darray, eid, value) => { darray.array[eid][darray.cursor[eid]++] = value }

push(DynamicArrayComponent.someDynamicArray, eid, 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants