- Home /
how to get the Id stored at x,y,z in a file
Hello ! I have been working on it for a while and I just cant find an efficient way to make my script working basically, im storing ID of tile at a postion X,Y,Z in a file. I want the player to load all the ID close to him, and the only way Ive found is to iterate trough all the tile in the file and then I check if its close enough to the player to be spawned. This way worked well for me when my map has only a few tile... but when I made a desert of 1000x1000 tile) it was one million of tiles to iterate trough, and was very very laggy, was playing at 1 or 2 fps... The position of the player is known, so im searching a way to directly return the id of the tile at position x,y,z . The tile are all sorted, so I don't want a sorting algorithm :P I really need a method to directly refer to the node of my file Thx for helping me!
Definitely time to look into a "chunk" approach to data management: Separating data into logical chunks, then only loading those chunks you currently need.
I think to get a more specific answer, you'll have to explain what kind of file you're working with, and what types of data you're storing in those files.
well, im using a map editor, that store (X - 32 bits , Y - 8 bits Z - 32 bits and ID of the tile : 32 bits)
for each tile that I add, it store the data like this. Ive made a structure and I can refer to these data. I do not need the chunk approach, cause it will still result in the same issue, ill have to do a for loop trough all the chunk to find out which chunk to spawn and then spawn do a for loop in the chunk to find out which tile in the chunk to spawn... I need another method to directly get the id stored at tile x= 1004, y 0 and z 1040) as an example..... Is there a solution to it?
Remember to post comments as comments and not as answers. This has been converted on your behalf.
I'm having difficulty understanding your situation. What kind of file are we talking about? Literally what is the file extension of the file?
I would strongly encourage you to contact the original author of the map editor you're using. It's rather unlikely that our volunteers will be familiar with the inner workings of third-party assets.
I did the map editor :P its just a simple script that save a position and an ID. the file has currently no extension, its only a binary file, I also made a script that generate an xml file, but I don't use it anymore, In unity I got 2 Csharp script that can read both type of file actually, I can explain my problem better, imagine you've got a big bitmap file, and you want to check the pixel at a defined position x and y, how would you simply return only the color of the pixel without iterating trough all the file? its hard for me to explain better....
Answer by ElijahShadbolt · Dec 28, 2016 at 07:10 AM
I hope your x, y, and z are integers; floats muddy the waters a lot. If the data in the file is sorted (eg by X, then by Z, then by Y), you could use an efficient Search algorithm (eg binary search, but something more efficient) to locate the specific tile in a much shorter time than a linear search (foreach).
As @AlwaysSunny was saying, a chunk system would work beautifully in this situation. You would load only the few tiles that are directly nearest to the player (within a radius of, say, 3 tiles), then loop through those to find the closest tile. Then when the player moves to a different tile, load new tiles around that tile and unload the tiles that are farthest away. This means you only have to loop through something like 30 tiles at any one time, instead of all 1,000,000.
Hello :) thx for the answer, I might resign myself to use the chunk approach even though I do not like it because I do not find any other solution, I have heard that dictionaries could be use with more than 1 arguments , that mean I could have a definition for each tile, but I doubt it will be fast enough ... !
Your answer
Follow this Question
Related Questions
How to write files to the streaming asset folder in Android at runtime 1 Answer
Writing binary files on Android 1 Answer
do something if a method cant be executed 1 Answer
WEB game how in the dozens of songs quickly switch? There is a good way to do 1 Answer
Web radio playing inside unity? 2 Answers