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

Using multiple lists/arraylists with a potentially large number of indices, on many gameobjects?

Discussion in 'Scripting' started by Jimmy-P, May 27, 2017.

  1. Jimmy-P

    Jimmy-P

    Joined:
    Jul 10, 2014
    Posts:
    59
    So I'm thinking of a solution to a problem I have, and it seems to me it would be very effective to use lists.

    Basically I would assign 12 lists to each gameobject, and these lists would hold a number of very simple objects with some attributes. I could probably achieve everything I want using only bools and enums for the attributes. Every few seconds (5 in the current iteration) some operations are done, mainly adding and removing from each list, but also working with data contained in the lists. Some of these lists would contain large numbers of indices, maybe to the tune of 50-100k, most of them would be much smaller. The number of gameobjects would be at least in the hundreds, possibly in the thousands.

    My knowledge of programming is pretty basic. Would this be extremely resource expensive? My current approach is not too heavy on the resources, but it also doesn't do a very good job of representing what I want it to. I could refine it and make it better but if it 's viable to use lists/arraylists (or some other collection) I would rather do that.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Here's a useful link.
    http://geekswithblogs.net/BlackRabb...tals-choosing-the-right-collection-class.aspx

    You don't need to understand n notation of algorithm time complexity, just read the notes on the right for each collection and it'll give you a rough idea of if a list is best for you.

    In my opinion. Just implement what you think will work and then refactor it later if efficiency is an issue.

    Having simple clean code far outweighs any benifits of predicting and over engineering to solve possible future problems.
     
    Jimmy-P likes this.
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    It'll be extremely expensive in both memory and computation. A little math: Assuming each of those indices takes up a single byte in your collection, you're talking about having hundreds of objects that have a footprint of at least a megabyte in memory each. This is without the implications of manipulating your collections: For example, lists have poor performance when searching for an item, and allocate when removing from the collection.

    I would advise avoiding this.
     
    Jimmy-P likes this.
  4. Jimmy-P

    Jimmy-P

    Joined:
    Jul 10, 2014
    Posts:
    59
    Thanks for your reply, and the link. Skimming it, it appears my concern was well founded. I appreciate the sentiment as well, but I suspect refactoring would be a lot of work, as this process will be quite core to the game I'm trying to make. The alternative I have in mind is a completely different approach and I don't really like it, but at least it's resource effective. I'm sure if I put some more effort towards it I can think of a way to make it represent my idea to a respectable degree.
     
  5. Jimmy-P

    Jimmy-P

    Joined:
    Jul 10, 2014
    Posts:
    59
    Thanks. I should have done that math myself, 1MB per object is a pretty conservative number too, especially since each index would likely be more than one byte, and hundreds of objects would be a bit on the low side for what I expect. Back to the drawing board.