Passing 1 or 2 more values in Dispatch and addlisten

What am I doing wrong?

I am trying to dispatch 2 variables, the ‘playerIndex’ and ‘selection’.
===My dispatch code snippet===
world.events.dispatch(world.events.globalId, ‘listens’, {
playerIndex,
selection,
})

In another script, I cannot receive the ‘playerIndex’ and ‘selection’
===My add listen code snippet===
world.events.addListener(world.events.globalId, ‘listens’, (playerIndex, selection) => {

  if (schemaAttribute.get(eid).playerID === playerIndex) {
          console.log(selection)
    }

})

How do I addlisten for 2 variables?

The listener passes an object which has a “Data” property that contains your variables. Change your listener to something like this and check the console to see the entire event object:

world.events.addListener(world.events.globalId, ‘listens’, (e) => {
    console.log(e)
})

This is the solution

world.events.dispatch(world.events.globalId, ‘playerSelectsModel’, {
playerIndex,
selection,
})
How to listen to more than 1 variable in addlisten
// change body type when game starts
world.events.addListener(world.events.globalId, ‘playerSelectsModel’, (e) => {
console.log(‘Player Index:’, e.data.playerIndex)

If you wanted to use multiple variables you can do so like this

world.events.addListener(world.events.globalId, ‘playerSelectsModel’, (e) => {
    console.log(`Player Index: ${e.data.playerIndex}`)
    console.log(`Selection: ${e.data. selection}`)
})

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