#okay, for the sake of not making 1
1 messages · Page 1 of 1 (latest)
okay, I will also include screnshots of the game of what I am modifying to provide additional clarity
What is a "modified" tile? One where tileType is 1 or something?
What is the data we're looking for
This is the default grid, all tiles have tileType 1, which is the default
I have drawn red triangles, which are tileType 4
the game recognizes this and adds the modified tiles to tileHolder[x,y], where the x,y are the xy of the object affected
alright and you save that to JSON at some point yes?
when I hit the "export" button, a json is created, and the entire array is gone through, with each object serialized to to a json.map file
I will post the json's contents if needed
A tool for sharing your source code with the world!
use ctrl F and insert tileType":4
you should find 22 instances
now
ok so on to the parsing part
when I hit load(same game instance)
we get an updated grid
(the shapes are different because I just wanted to see if would keep color and change stuff)
as you can see, the log posts the x,y and tiletype of all modded instances
mhmm...
okay
now
I will close the game
and when I reopen and hit load
the X,Y is off... that's new
how is that possible? It literally loaded perfectly same game, is this a memory issue?
Most likely an error in your logic somewhere
is this the same file you shared with me?
- double check the json file hasn't changed
- debug the "parsed" data to see which coordinates should be red
yes I literally opened it and checked it now
still 22 instances
the debug.log has the same amount of posts
ok so after your parsing code - log the coordinates of the tiles that are 4
we want to see the coordinates\
it already does that
I jsut see it logging 4
those coordinates are clearly different from what's in the file
so I think it's likely a parsing issue
also why is it printing 2, 1 twice
and 3, 1 twice
ah because I wanted to show it pre and post towerStorage creation
the first is the parsed string, the 2nd is as a towerstorage object
that is now in the tileHolder array
int x = int.Parse(subby[0].Substring(subby[0].Length-1));
int y = int.Parse(subby[1].Substring(subby[1].Length-1));
int tT = int.Parse(subby[2].Substring(subby[2].Length-1));
int tot = int.Parse(subby[3].Substring(subby[3].Length-1));```
doesn't this logic assume that the number coordinate is only one digit?
oh, rats
So {"x":12,"y":11,"tileType":4 will get parsed as 2, 1
so yeah - again - I implore you to use a real JSON parser lol
okay, yeah, that's my stupid mistake
Although as it is your file is not actually legal json
Would I have to wrap my array as a class so that it can be pushed as an object into json.serialize object?
if you want to use JsonUtility in unity you would basically do:
[Serializable]
public class TileData {
public TileStorage[] allTiles;
}
[Serializable]
class TileStorage
{
public int x;
public int y;
public int tileType;
public int towerType;
// etc.````
then you just do this to parse it:
TileData data = JsonUtility.FromJson<TileData>(jsonString);```
And create the json with:
```cs
TileData data = // create this somehow in code
String json = JsonUtility.ToJson(data);```
and the Json would be able to parse it perfectly and won't just leave me with a bunch of blank brackets?
not sure what you mean by blank brackets, but yes the parser knows how to handle json perfectly
yeah, it's just that a previous issue when pusshing an array to json it just gave me a json of repeating {}{}{}{}
that sounds like you didn't mark your class as [Serializable] or it didn't have any serializable fields
ah, I didn't know that was something I had to do
also 2D arrays like this cannot be directly serialized
But yes Unity's serializer has rules that need to be followed https://docs.unity3d.com/6000.0/Documentation/Manual/script-serialization-rules.html
ah, so if I wrap it in a class will it be okay?
no - 2d array can't be serialized at all
it neees to be flattened into a 1D array
like in this example
ahhhhhh
if there's a flatten is there an unflatten?
it's easy enough to write one sure. But you don't need it in this case since each tile actually stores its own x/y it's just:
foreach (TileStorage ts in myTIleData.allTiles) {
tileHolder[ts.x, tx.y] = ts;
}```
right but if i had to edit a specific tile, how would I go about accessing it through the array?
There's no need to edit a specific tile until it's in your tileHolder array
this whole thing is JUST for reading and writing to the file
once it's in tileHolder you can do tileHolder[x, y] as normal
ahhh you're right
I can have it be a 2d array UNTIL I need it JUST for saving
in which case I'd flatten/unflatten
TileData is just an intermediate format for serialization yes
using System;
but also it's spelled [Serializable]
oh wait you have the spelling right - yeah just using System
adding using System worked
you need it on TileStorage as well btw
okay, added
so now for the save/load process, just convert tileHolder to tileData with flatten
then json serialize
then json deserialize, unflatten, and go
wow what i made was a massive waste of time
how do I make a tileStorage[] into a tiledata? Is there a way to convert or do I have to write a new method for it to convert?
TileData td = new TileData {
allTiles = tiles
};```
or just:
TileData td = new TileData();
td.allTiles = tiles;```
it's not converting just assigning the array as the allTiles field
how did you create the json
and can you show the file
remember:
- flatten your 2D array
- shove it in a TileData
- call ToJson on the TileData
yep the file looks good
you're trying to parse the filepath
not the json data
string jsonString = File.ReadAllText(Path.Combine(path, "Map.json"));
you ned to read the file first^
ahhhh
then TileData tiles = JsonUtiltiy.FromJson<TileData>(jsonString);
no need for creating the new empty one before that
Nice