#okay, for the sake of not making 1

1 messages · Page 1 of 1 (latest)

dark wyvern
#

Let's make a thread

spare cloak
#

okay, I will also include screnshots of the game of what I am modifying to provide additional clarity

dark wyvern
#

What is a "modified" tile? One where tileType is 1 or something?

#

What is the data we're looking for

spare cloak
#

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

dark wyvern
#

alright and you save that to JSON at some point yes?

spare cloak
#

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

dark wyvern
#

can we see the saved json file with the 4s

#

yeah that would be good

spare cloak
#

use ctrl F and insert tileType":4

#

you should find 22 instances

dark wyvern
#

yep

#

i see it

spare cloak
#

now

dark wyvern
#

ok so on to the parsing part

spare cloak
#

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

dark wyvern
#

mhmm...

spare cloak
#

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?

dark wyvern
#

Most likely an error in your logic somewhere

#

is this the same file you shared with me?

#
  1. double check the json file hasn't changed
#
  1. debug the "parsed" data to see which coordinates should be red
spare cloak
#

yes I literally opened it and checked it now

#

still 22 instances

#

the debug.log has the same amount of posts

dark wyvern
#

ok so after your parsing code - log the coordinates of the tiles that are 4

#

we want to see the coordinates\

spare cloak
#

it already does that

dark wyvern
#

I jsut see it logging 4

spare cloak
#

how do I exporty a debug.log?

dark wyvern
#

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

spare cloak
#

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

dark wyvern
# spare cloak 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?

spare cloak
#

oh, rats

dark wyvern
#

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

spare cloak
#

okay, yeah, that's my stupid mistake

dark wyvern
#

Although as it is your file is not actually legal json

spare cloak
#

Would I have to wrap my array as a class so that it can be pushed as an object into json.serialize object?

dark wyvern
#

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);```
spare cloak
#

and the Json would be able to parse it perfectly and won't just leave me with a bunch of blank brackets?

dark wyvern
#

not sure what you mean by blank brackets, but yes the parser knows how to handle json perfectly

spare cloak
#

yeah, it's just that a previous issue when pusshing an array to json it just gave me a json of repeating {}{}{}{}

dark wyvern
spare cloak
#

ah, I didn't know that was something I had to do

dark wyvern
spare cloak
#

ah, so if I wrap it in a class will it be okay?

dark wyvern
#

it neees to be flattened into a 1D array

spare cloak
#

ahhhhhh

dark wyvern
spare cloak
#

if there's a flatten is there an unflatten?

dark wyvern
#

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;
}```
spare cloak
#

right but if i had to edit a specific tile, how would I go about accessing it through the array?

dark wyvern
#

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

spare cloak
#

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

dark wyvern
#

TileData is just an intermediate format for serialization yes

spare cloak
#

do I need to add a using for the [Serializeable]attribute?

dark wyvern
#

but also it's spelled [Serializable]

#

oh wait you have the spelling right - yeah just using System

spare cloak
#

adding using System worked

dark wyvern
#

you need it on TileStorage as well btw

spare cloak
#

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

dark wyvern
#

you learned a thing or two

#

so not a waste

spare cloak
#

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?

dark wyvern
#

or just:

TileData td = new TileData();
td.allTiles = tiles;```
#

it's not converting just assigning the array as the allTiles field

spare cloak
#

ahh

#

I'm getting an error when converting back to TileData

dark wyvern
#

and can you show the file

spare cloak
#

ah I'm using a differnt converter

#

let me use toJson

dark wyvern
#

remember:

  • flatten your 2D array
  • shove it in a TileData
  • call ToJson on the TileData
spare cloak
#

new json file

dark wyvern
#

yep the file looks good

spare cloak
#

am I doing this part wrong?

#

thats where the error is coming from

dark wyvern
#

not the json data

#

string jsonString = File.ReadAllText(Path.Combine(path, "Map.json"));

#

you ned to read the file first^

spare cloak
#

ahhhh

dark wyvern
#

then TileData tiles = JsonUtiltiy.FromJson<TileData>(jsonString);

#

no need for creating the new empty one before that

spare cloak
#

yeah, fixed

#

it can save load outside of game closing now

#

thanks for all ur help

dark wyvern
#

Nice