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

What should work faster?

Discussion in 'Scripting' started by Harter, Sep 12, 2012.

  1. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
    Hi developers!

    I've been creating my own 2D Waypoints system (predominantly for iOS/Android). Now each waypoint's game object destroys in Awake() (to free memory).
    But I thought: "what if I could save their positions in the file inside of Unity Editor and then just load them from a file in game?"

    So my question is: what's faster to do in Awake() for 500 waypoints (game objects)?
    1. Get their position and then destroy objects (as I'm doing now).
    2. Load 1500 lines of text with their position from file.

    I have 'Waypoint.cs' script:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Waypoint {
    4.     public Vector3[] Points;
    5.  
    6.     public Waypoint (Transform PointsParent)
    7.     {
    8.         Points = new Vector3[PointsParent.GetChildCount()];
    9.         for(int i=0; i<PointsParent.GetChildCount(); i++)
    10.             Points[i] = PointsParent.GetChild(i).position;
    11.     }
    12. }
    And 'Waypoints.cs', which loads all waypoints and destroys PointsParent (from 'Waypoint.cs').

    Sorry for my bad English.
    Thanks!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Time wise? You're doing this at load time?

    I don't know which would be faster. Depends on the size. Why don't you do a unit test and see.

    Just time how long each takes, do it several times over different conditions (different number of waypoints, different devices), and see which wins.

    Personally I would go with a file, not because it's faster but for organization.
     
  3. Pia

    Pia

    Joined:
    Feb 16, 2009
    Posts:
    78
    Use a file. It would just contain the information you need, could be read by other applications (i.e. external enemy movement editor) and is certainly more easy to swallow for the garbage collector.

    And I am 99% sure that it would be faster.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It should also be noted that Destroy does not remove items from memory.
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Maybe you can store the waypoint positions as an array of Vector3?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    OP is looking for how to store it on disk, not in memory during gameplay.
     
  7. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    If he's loading them in from disk, he will storing them in memory during gameplay. :p

    To the OP: Given that you're doing this once when the game starts, there are about a billion other features you could add or optimizations you could make before this becomes an issue - just use the easiest or prettiest method and move on.

    I personally would use andeeeee's suggestion and store them all in an array of Vector3s, and write a custom editor script to make it easy to place them. It's easier than a text file and removes the overhead of having 500 gameobjects in your scene.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    But it's not what they're asking about... ::facepalm::

    Just because a car needs gasoline doesn't mean "you should get gasoline" is a good answer to the question "what's a good interest rate for a car?"
     
  9. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Okay, so he was only talking about speed. That doesn't mean he should be ignoring the memory footprint of his method, and flaminghairball was doing him a favor by mentioning it.
     
  10. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Right, and andeeee was proposing a solution for how to store them on disk.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Was he really?

    Context people... context.