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

ArraySchema not retaining value between functions #533

Closed
PandaPalumbo opened this issue Aug 14, 2022 · 4 comments
Closed

ArraySchema not retaining value between functions #533

PandaPalumbo opened this issue Aug 14, 2022 · 4 comments

Comments

@PandaPalumbo
Copy link

PandaPalumbo commented Aug 14, 2022

Steps to Reproduce (for bugs) relevent code is below

  1. Execute a command to update state
  2. command calls a function (generateRoll) that returns a 'Roll' schema
  3. Adding breakpoint on return of generateRoll shows 'dice' ArraySchema having a value/members (pic attached)
  4. Adding a breakpoint after the variable the return of generateRoll was assigned to shows 'dice' ArraySchema being empty.

^3 pic
image
^4 pic
image

RollDice Command

import {Command} from "@colyseus/command";
import {GameRoom} from "../index";
import {generateRoll} from "./helpers";
export class RollCommand extends Command<GameRoom, {
    length: number
}> {
    execute({length} = this.payload) {
        let currentRound = this.state.currentRound
        let lastRoll = currentRound.currentRoll;
        let roll = generateRoll(length);
        currentRound.busted = roll.isBust
        if (lastRoll) {
            this.state.currentRound.rollHistory.push(lastRoll)
        }
        this.state.currentRound.currentRoll = roll
        this.state.currentRound.busted = roll.isBust
    }
}

.helpers/generateRoll

export const generateRoll = (length: number = 5): Roll => {
    let dice = new ArraySchema<number>();
    let isBust: boolean;
    let uuid: string = uuid4();

    for (let i = 0; i < length; i++) {
        const roll = generateRandom(MIN_ROLL, MAX_ROLL)
        dice.push(roll)
    }
    console.log(dice.toJSON())
    isBust = determineBust(dice)
    return new Roll({
        dice,
        isBust,
        length,
        uuid
    })
}

relevent schema

export class Round extends Schema {
    @type(Roll) currentRoll: Roll;
    @type(PlayerState) currentPlayer: PlayerState;
    @type(SetAside) setAside: SetAside;
    @type([Roll]) rollHistory = new ArraySchema<Roll>();
    @type([SetAside]) setAsideHistory = new ArraySchema<SetAside>();
    @type("number") score: number;
    @type("number") roundNumber: number;
    @type("boolean") busted: boolean;
}

export class Roll extends Schema {
    @type(["number"]) dice = new ArraySchema<number>();
    @type("boolean") isBust: boolean;
    @type("number") length: number;
    @type("string") uuid: string
}

Context

This is currently keeping me from having dice rolls generated in my game server which is the main functionality of the game. I may be able to get around with by using the functionality in generateRoll in my command rather than call a function, but I would rather keep it compartmentalized.
The rest of my Roll schema keeps its value assigned in generateRoll.

Your Environment

  • Colyseus Version: ^0.14.13",
  • Node.js Version: 16.3.2
    Operating System and version: Windows 10
@endel
Copy link
Member

endel commented Aug 15, 2022

Hi @PandaPalumbo, it would save us some time if you can provide a repository that we can quickly reproduce. Not sure what may be wrong in your code, my only suggestion would be to use new Roll().assign({...}) instead of new Roll({...}). Cheers

@mdpalumbo
Copy link

@endel Thanks a ton, let me try that and get back to you with the repo if needed.

@PandaPalumbo
Copy link
Author

Holy cow I feel silly, that worked for me. Thank you so much @endel

@endel
Copy link
Member

endel commented Aug 15, 2022

Nice, glad it worked out!

@endel endel closed this as completed Aug 15, 2022
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

3 participants