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

[Bug]: Uncaught Error: ChangeTree: missing index for field "undefined" #689

Open
WeylonSantana opened this issue Jan 31, 2024 · 0 comments
Labels
🧮 @colyseus/schema Problems related to Schema Serializer 🐛 bug
Milestone

Comments

@WeylonSantana
Copy link

WeylonSantana commented Jan 31, 2024

Context

I'm getting this uncaught error on client side, caused by a Map Schema (or a map inside another map maybe)

Bug description

obtain instances of schemas through map get, and modify their values (or in my case, specifically the name: string or revealed:boolean value, causes an error on the client side, it is not even necessary to send a packet to the client, the error happens instantly when modifying the property

Uncaught Error: ChangeTree: missing index for field "undefined"
    at ChangeTree2.assertValidIndex (colyseus__js.js?v=5fb6481a:1048:21)
    at ChangeTree2.touch (colyseus__js.js?v=5fb6481a:956:18)
    at ChangeTree2.touchParents (colyseus__js.js?v=5fb6481a:965:39)
    at ChangeTree2.change (colyseus__js.js?v=5fb6481a:952:18)
    at _.set [as name] (colyseus__js.js?v=5fb6481a:1840:31)
    at Schema2.decode (colyseus__js.js?v=5fb6481a:2928:34)
    at SchemaSerializer.patch (colyseus__js.js?v=5fb6481a:4344:27)
    at _Room.patch (colyseus__js.js?v=5fb6481a:3698:25)
    at _Room.onMessageCallback (colyseus__js.js?v=5fb6481a:3681:16)

Reproduction

i have this code

async changePlayerCards(reveals: boolean = false, targets?: Array<Target>) {
    const { selectedTargets } = this.state;
    if (!selectedTargets?.length && !targets) return;
    const realTargets = targets ?? selectedTargets;

    const deckTarget = realTargets.find((t) => t.playerId === 'deck');
    if (deckTarget) {
      const target = realTargets.find((t) => t.playerId !== 'deck');
      if (!target) return;
      const targetPlayer = this.state.GetPlayerById(target.playerId);
      const targetCard = targetPlayer?.cards.get(target.cardId);
      if (!targetCard) return;

      if (reveals) await this.killHero(target);

      this.state.deck.push(targetCard.name);
      targetCard.name = this.state.deck.shift()  // #### <<===== error happens in this line
      this.state.addTask(new Task(TaskType.ChangeCard, { sourceId: target.cardId, targetId: 'deck' }));
    }

//rest of code
}

Environment & Versions

Colyseus.js: 0.15.17

Workarounds

When obtaining the instance of schema through get

do not change its values

instead
const targetCard = targetPlayer?.cards.get(target.cardId);
targetCard.name = this.state.deck.shift()!

do a re-set of value again, my solution:
targetPlayer?.cards.set(target.cardId, new Card(this.state.deck.shift()!, false));

with that i have no problems for now.

@endel endel added 🧮 @colyseus/schema Problems related to Schema Serializer and removed 👀 triage labels Jan 31, 2024
@github-actions github-actions bot added the Stale label Mar 3, 2024
@endel endel removed the Stale label Mar 4, 2024
@endel endel added this to the 1.0 milestone Mar 4, 2024
@colyseus colyseus deleted a comment from github-actions bot Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧮 @colyseus/schema Problems related to Schema Serializer 🐛 bug
Projects
None yet
Development

No branches or pull requests

2 participants