Search Unity

Loading objects within certain range

Discussion in 'Scripting' started by Noxbuds, Jul 6, 2015.

  1. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    I am making a space game with procedural universe generation. I plan to have infinite galaxies with around 1 million stars in them.
    This is how I want it to function:

    - There will be a universe map. You can focus on 1 galaxy at a time, but you can zoom out to see more and focus on others
    - It will be a rough view of the galaxy. You can zoom in on different parts to view more stars in detail
    - Stars within range will be loaded into memory. This is going to be a small chunk of a galaxy that you can view at one time
    - When you get further away from that chunk, it'll load the new chunk and unload the previous chunk to save memory
    - When you go back to that chunk, it'll be exactly the same as before (I know how to do that part)
    - Different levels of detail depending on how close you are to the star/planet (will be travelling in a spaceship)
    - Planets, eventually (but I will follow same procedure as the stars in the galaxies)

    I am unsure on how to do loading/unloading. I have put in a galaxy count because I am unsure how I would make infinite galaxies. It is OK if infinite can't be done - at least up to about 1,000 galaxies would be nice.

    This is what I have done so far:

    - You give the generator 2 seeds. 1 for the galaxy positioning, 1 for the galaxy name (this was fun to look at!)
    - It will use the Fibonacci sequence to generate the positions:
    - The first number is 0 plus the seed
    - The second number is the seed + the first
    - The third is the first + second
    - Continues like the one above
    - Similar process for the name
    - Instantiates a sphere at the positions and gives it its name

    Should I do this using Instantiate when close to a chunk (instantiate the whole chunk) and then destroy the previous one?

    Also, I am using arrays - is it better to use Lists?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Lists<> are arrays with a pretty dress. If you ever want to change the number of elements in your collection, use Lists. If not, it doesn't really matter.

    If your chunks are large, doing Instantiate will cause a noticeable hickup in your game. You should probably instantiate the new stuff over time, instead of trying to make it all appear at once. But otherwise, yes, instantiate things that are close and destroy things that are far away.

    For large space games, note that floating point errors becomes a large issue. In essence, if you're a couple of (tens of) thousand units away from the origin (0,0,0), your coordinates start to be less accurate than close to the origin. This will cause a bunch of weirdness. If you run into that, a common trick is to move stuff about to keep the player close to the origin, instead of placing the planets and whatever at their "correct" coordinates.

    Good luck!
     
  3. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    Thank you! I will try this. I know about the problems with large units so I was thinking of making things really small to avoid weirdness (obviously from the player's perspective everything will be very big)
     
  4. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    Also wondering how to do this for multiplayer. I was thinking of retrieving basic data from the server and loading/unloading as mentioned but doing it clientside?
    Possibly then just storing the co-ordinates of the player on the server.