You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My function to convert a square noise grid into a hexagon grid:
exportdefaultfunctionconvertGrid(mapSize?: number){constgeneratedMap=generateMap(mapSize)consthexes=[...generatedMap].map((hex)=>CustomHex.create(squareToHex(hex.coordinates[0],hex.coordinates[1]),hex.elevation,hex.moisture,hex.id))constgrid=newGrid(CustomHex,hexes)// Return the grid in JSON format in the consoleconsole.log(grid.toJSON())returngrid.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:
consthexes=[[0,0],[1,0],[0,1],].map((coordinates)=>CustomHex.create(coordinates,0,0,'id'))constgrid1=newGrid(CustomHex,hexes)constserializedGrid=JSON.stringify(grid1)constdeserializedGrid=JSON.parse(serializedGrid)// this returns a grid with the same Hex class and hexes as grid1constgrid2=Grid.fromJSON(deserializedGrid)grid2.forEach(console.log)
Am I missing something?
The text was updated successfully, but these errors were encountered:
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:
consthexFactory=({ q, r, elevation, moisture, id }: CustomHex)=>CustomHex.create({ q, r },elevation,moisture,id)constgrid2=Grid.fromJSON(deserializedGrid,hexFactory)
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
My function to convert a square noise grid into a hexagon grid:
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:
Am I missing something?
The text was updated successfully, but these errors were encountered: