Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

referencing an array, or sharing data between many scripts? Help

Discussion in 'Scripting' started by Nanako, Feb 21, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Okay, here's what i'm trying to do:

    I have a number of objects (chunks). Arbitrary. might be 10, might be a million. but it's definitely >1

    These objects are laid out in a messy grid of sorts, and each of them is connected to a few others (2-8 connections per object)

    I'm hitting one object and damaging it. And i want that damage to radiate out to the others nearby, falling off with distance. This shouldn't be too hard since each object keeps a list of all those its connected to. I'm not going to iterate through the entire grid because of the potentially infinite size of the grid, and because the radius of damage is often going to be localised.

    So my initial plan is for the first object to iterate through all the ones its connected to, and damage them. And then tell each of them to damage THEIR neighbors too (this will scale with distance from the origin point). So on recursing out until the damage falls off to below some threshold, or hits every object in the grid.

    The problem i see is overlapping. If i just do that as is, almost every object is going to be getting damaged multiple times by all of its neighbors, and that just seems silly.

    So what i'd like to do is keep some array of all the objects that have been hit already, and share it among them. But if i jsut pass the array itself, then there's going to be lots of copies of it floating around each with different information. And i'm also not sure WHERE to make this array, any given chunk is volatile and may stop existing at any moment, so i can't rely on them for storing data that other things need to access.

    i also can't just rely on a single static array somewhere, because the number of object grids that i might have is also arbitrary, and i may end up needing lots of them at once.

    so i need some design help here.
     
    Last edited: Feb 21, 2015
  2. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Gonna watching your thread.
    You can add boolean field isDamaged to chunk, which turns to true when hits once. After explosion ends you will turn back them to false. But it will work only if there is 1 explosion per some time on whole map. How many hits possible at the same time?
    Or you can add damaged chucks ref to an array, and check IsContains before decide to damage. If there a few hits at the same time - 1-10 you can use own array for every hit, but for large amount of chunks your performance gonna drop
    Otherwise you have to store your chunks as array and make some addition arithmetycs
     
    Last edited: Feb 21, 2015
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    arbitrary. This solution won't work.
     
  4. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Arbitrary amount of hits?
     
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Just pass a reference to the first object to the others, as an "origin", and don't allow any more damage from that origin until a certain amount of time has passed or the explosion from that origin is done. I'd keep a List of origin objects on each object, rather than a single reference, so that you can have concurrent damage from multiple origins.
     
  6. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    at the moment i'm leaning towards using a 2D array, where each first element holds an array for explosion data until its done.

    as said, these objects can stop existing at any time so involving the origin it beyond the first stage isn't really a good idea. i'm just going to represent the origin as a position in space instead.
     
  7. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I don't see why. The object can stop existing at any time, but when it does, so does its need for a reference to the origin. If the origin stops existing, same deal.

    Using the point of origin also works. Same concept.