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

Deserializing Grid is not returning CustomHex #99

Closed
arvalaan opened this issue May 5, 2023 · 2 comments
Closed

Deserializing Grid is not returning CustomHex #99

arvalaan opened this issue May 5, 2023 · 2 comments
Labels

Comments

@arvalaan
Copy link

arvalaan commented May 5, 2023

Hey there, I'm trying to deserialize a previously generated grid however am unable to do so as my CustomHex class gets lost.

Here you can find my steps to reproduce:

My custom class definition

export class CustomHex extends defineHex({ dimensions: 4, origin: 'topLeft' }) {
    static create(
        coordinates: HexCoordinates,
        elevation: number,
        moisture: number,
        id: string
    ) {
        const hex = new CustomHex(coordinates)
        hex.elevation = elevation
        hex.moisture = moisture
        hex.id = id
        return hex
    }

    id!: string
    moisture!: number
    elevation!: number
}

My function to convert a square noise grid into a hexagon grid:

export default function convertGrid(mapSize?: number) {
    const generatedMap = generateMap(mapSize)

    const hexes = [...generatedMap].map((hex) =>
        CustomHex.create(
            squareToHex(hex.coordinates[0], hex.coordinates[1]),
            hex.elevation,
            hex.moisture,
            hex.id
        )
    )

    const grid = new Grid(CustomHex, hexes)

    // Return the grid in JSON format in the console
    console.log(grid.toJSON())

    return grid.toJSON()
}

I then try to deserialize in another function however the output is not as expected.

Here's a test function I put together to try and see what I was doing wrong:

const hexes = [
        [0, 0],
        [1, 0],
        [0, 1],
    ].map((coordinates) => CustomHex.create(coordinates, 0, 0, 'id'))
    const grid1 = new Grid(CustomHex, hexes)
    const serializedGrid = JSON.stringify(grid1)

    const deserializedGrid = JSON.parse(serializedGrid)

    // this returns a grid with the same Hex class and hexes as grid1
    const grid2 = Grid.fromJSON(deserializedGrid)

    grid2.forEach(console.log)

Am I missing something?

@flauwekeul
Copy link
Owner

Hi. Thanks for your question. I can reproduce your problem and the cause is in Grid's fromJSON() method. It looks like this:

  static fromJSON({ hexSettings, coordinates }: GridAsJSON): Grid<Hex> {
    const HexClass = defineHex(hexSettings)
    return new Grid(
      HexClass,
      coordinates.map((_coordinates) => new HexClass(_coordinates)),
    )
  }

As you can see, it creates hexes by simply passing the coordinates to the constructor function, ignoring any custom properties you may have. I'll add an optional hexFactory argument to fromJSON() so that you can do something like this:

const hexFactory = ({ q, r, elevation, moisture, id }: CustomHex) => CustomHex.create({ q, r }, elevation, moisture, id)
const grid2 = Grid.fromJSON(deserializedGrid, hexFactory)

github-actions bot pushed a commit that referenced this issue May 13, 2023
# [v4.1.0](v4.0.1...v4.1.0) (2023-05-13)

## ✨ New Features
- [`2af4707`](2af4707)  add optional 2nd argument to Grid.fromJSON() (Issues: [`#99`](#99))

# [4.1.0](v4.0.1...v4.1.0) (2023-05-13)
@github-actions
Copy link

🎉 This issue has been resolved in version 4.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

2 participants