Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

World Map : Problem with UnityGUI and storing data

Discussion in 'Editor & General Support' started by Claws, Nov 9, 2010.

  1. Claws

    Claws

    Joined:
    Dec 30, 2009
    Posts:
    5
    Hi there,

    This is a slightly mixed question. Firstly, I'm trying to make a world map. My game scans sectors around the player's current sector. This scan returns each planet and it's position/size etc. I then need to plot these planets' positions onto my world map (which should be a GUI element). The player can move whilst the map is open, so the planets need to constantly update their position on the map relative to the player's position.

    First I tried to run all these functions inside OnGUI. This was a mistake as the FPS dropped suddenly to 3. My plan now is to calculate all the planets positions/sizes etc. when the player opens the map. This data should then be stored in some kind of array which will be looped through to update all the GUI elements positions.

    The part I'm having trouble with is storing the data. I need some kind of 2d array, that has no set size, that I can append planets to the end of the array. I need to be able to loop through the array using the loopindex. I also need to be able to wipe the array and completely re-populate it every time the map is opened.

    Any advice? Please bear in mind i've been using Unity for about 6 days and am not an experienced programmer. I'm using C# btw!

    Thanks in advance,
    Mr Claws
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    What exactly does it mean to append an element to the 'end' of a 2-d array? I think clarifying that aspect of things would be a good first step.
     
  3. Claws

    Claws

    Joined:
    Dec 30, 2009
    Posts:
    5
    Sorry, perhaps i'm using the wrong terminology.

    Each time the loop that gets each planet's info runs, I want this info to be added to some kind of storage. So after 4 loops we'd get something like this-

    planetNumber | xPox | yPos | size | population
    1 250 340 200 16,000
    2 322 100 100 50
    3 -100 10 150 0
    4 -200 -200 44 0
    [edit - the forum didn't maintain the formatting for this table! Sorry, i hope its still readable].


    And every other loop would add the details the loop returns to the end of the array(?).
    This is surely a 2d array, because we have planet number as the index (perhaps it would be 0 based), and stats about the planet along the other axis.
    I need to make this information in an array like this so that I don't have to run these loops inside the OnGUI function.
    The OnGUI function should loop through this array, and position a GUI image of a planet on the world map, taking the xPos and yPos of the planet and the position of the player into consideration.

    I hope i've been more clear.
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You can use formatting tags (e.g. 'code' tags) to preserve formatting for stuff like that (although it's clear what you mean).

    This isn't something you'd typically use a 2-d array for. Although you *could* use a 2-d array for it, it wouldn't be the most sensible or straightforward solution.

    A typical solution for this would be to use a user-defined type such as a struct or class with the following members:

    - Planet number (if it's not implicit in the record's position in the container)
    - A 2-d vector representing the planet's position
    - Planet size
    - Planet population

    You would then store objects of this type (or references to them) in a container, such as the generic List container.
     
  5. Claws

    Claws

    Joined:
    Dec 30, 2009
    Posts:
    5
    Thanks Jesse, that definitely sounds like a good solution. Can you (or anyone) help me implement it?

    In my world map script I have my standard class (the class for the script)-
    I'm just not sure of the correct syntax. I want each instance of the planet class to put a reference to itself in a list (like you suggested). This way I can loop through the list and find the appropriate info I'm looking for.

    Thanks for your help. I'm just not sure how to do this!
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Quick note: using 'code' tags rather than 'quote' tags when posting code will preserve the code formatting and make it easier to read.

    Also, I'd recommend using Unity's Vector2 struct rather than separate 'xPos' and 'yPos' variables for representing 2-d vectors and points.

    Beyond that, I think the best recommendation I could offer would be to work through some introductory materials (books, tutorials, etc.) on C#. The topics most relevant to your question might be the difference between structs and classes (since that's something that usually needs to be taken into consideration when creating a user-defined type), and the generic List container.

    What you have so far looks ok. I'd recommend giving your class or struct a constructor, and also making the data private unless there's a good reason not to, but what you have will work.

    Creating and setting up an instance of your Planet class might look like this:

    Code (csharp):
    1. Planet planet = new Planet();
    2. planet.xPos = ...;
    3. planet.yPos = ...;
    Creating a container of 'planets' and adding a new element might look like this:

    Code (csharp):
    1. List<Planet> planets = new List<Planet>();
    2. planets.Add(planet);
    Note that List is in the System.Collections.Generic namespace (IIRC).