- Home /
Efficient method of storing coordinate values sequentially in an array
This might be difficult to explain haha, I'll try my best: My game is grid-based and naturally I need a way of storing all possible coordinates and tile properties (walkable, type, coordinates etc). My current idea is to create a C# class, let's say TileIdentify which stores it's position, type, etc. I was hoping to use a multi-dimensional array like so:
TileIdentify[x][z][y] - which should allow me to make calculations at x,y and z. If I wanted to attack characters at adjacent tiles and whatnot.
So here's the problem:
I have a script (used prior to game start) which obtains x,y,z coordinates of a blender model in order to figure out which part of the map a character can access and creates a TileIdentify object for each 1x1 area. It does this through Editor input of max x/z lengths and it figures out the elevations using RayCasts. The issue is that I want to be able to use this code on multiple maps and they will have varying (x,y,z) values. So these scenarios might arise leading to issues in identifying tiles:
TileIdentify[0][0][3], let's say a pillar at (x = 0, z = 0) of height 3 is here but
TileIdentify[0][0][2] won't exist since it's not a walkable surface (part of pillar structure) and thus does not have a tile.
or
TileIdentify[2][1][0] exists but both TileIdentify[0 and 1][1][0] does not (could be a cliff with empty space beside it)
My Questions: Can I still use arrays to identify tiles at specific coordinates, if so, how?
Can I just create a NaN array of TileIdentify[maxX][maxZ][maxY] and just fill in the values that I need - will this impact efficiency since I might potentially create scores of TileIdentify objects?
Other there other better methods I should look into?
Thanks in advance, let me know if you want to see how my code looks for a better idea or if clarity is needed!
I kinda glanced over what you wrote. Two words:
Struct
List
Sorry if sound naive, I just started C# recently (co$$anonymous$$g from beginner level Java and $$anonymous$$ATLAB) but how can I implement structs and lists to perform this task? I can't use Lists within Struct since I will need to initialize everything within the Struct since it is immutable and can't reference anything. $$anonymous$$y second thought is using List(Struct) but how is that any different from List(Object) and how will it enable me to perform calculations along three dimensions since List indexes along a single dimension?
Am I looking at this problem the wrong way?
This is a somewhat hard data structures problem. 2nd-year Computer Science students study it. The easy answer is: if an array works, why not keep using it?
If you have few "blanks," most people make the entire array, and use -999 for the blanks.
If you have lots of blanks (70%?) often called a "sparse" array, most people try to make a searchable list of just the active items, with a clever look-up. Each item in the list now needs the coords stored with it.
A single sorted list with a binary search works, or a sorted list for each row. Some people might suggest a Dictionary with a clever key... .
Your answer
Follow this Question
Related Questions
2D/Orthographic Tile Alignment Issue 0 Answers
optimizing game objects - deciding what way to organize my level. 0 Answers
How can I access to a tile properties in a Tilemap? 0 Answers
How do you draw from tilepallet to tilemap while the game is running? 0 Answers
Changing tile collider shape 1 Answer