colyseus / colyseus-construct3

⚔ Colyseus Multiplayer SDK for Construct 3
https://docs.colyseus.io/getting-started/construct3-client/
MIT License
22 stars 12 forks source link

TBD: Allow to attach callbacks on deep structures #15

Closed endel closed 1 year ago

endel commented 3 years ago

In pure JavaScript, to be able to catch players.xxxx.gold, for example, you would do this:

state.players.onAdd = (player, id) => {
  player.onChange = (changes) => {
    changes.forEach((change) => {
      change.field // gold
      change.value // 10
    })
  }
}

C3 only seems to allow adding events statically at layout startup, so we can't register events for each player by id.

(if we could register "On Change" for each [id], like entities.fh91h3f.gold it would work, but it doesn't seem to be possible atm)

"@endel How about an action to register a on change path, then a generic on change condition with an expression that identifies the Path that changed, a sub condition could deal with the particular path if needed." - @MikalDev

"Also an action to unregister a on change path" - @MikalDev

MikalDev commented 3 years ago

One idea was to add a register ACE that did the following:

registerOnChange(path)
{
    state[path].onAdd = (entry, id) => {
    entry.onChange = (changes) => {
      changes.forEach((change) => {
        self.lastPreviousValue = change.previousValue;
        self.lastField = change.field // gold
        self.lastValue = change.value // 10
        self.Trigger(C3.Plugins.Colyseus.Cnds.OnSchemaFieldChange);
      })
    }
  }
}

Though this may not work, I can't quite remember how C3 will handle multiple triggers in one tick (e.g. multiple changes in one tick.) I think it will be ok, if it is defined as a regular trigger, not a fake trigger.

endel commented 1 year ago

Thank you @MikalDev, on the latest there is a "On change at" condition that can now handle these! 🙏