Search Unity

[Help] Approximate a float array to another float array, step by step?

Discussion in 'Scripting' started by Cellenseres, Feb 7, 2020.

  1. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Hey Guys,

    for a game we're working on, I need to approximate a float array to another (with a dynamic size).
    Both arrays always have the same size as the other one, but the size is dynamic.
    (Sometimes both are float[10]s, sometimes they're both float[5]).

    We have a DesiredPopulationMix of Monsters in percentage
    for example:
    float[] desiredPopulationMix = new float[5]{ 0.27f, 0.13f, 0.30f, 0.15f, 0.15f }
    and at the beginning of the Game, there's a float[] called currentPopulationMix.
    float[] currentPopulationMix = new float[5] {0f, 0f, 0f, 0f, 0f}

    My Task is now to create a function that:
    - spawns one of the five Monster types first
    - the currentPopulationMix gets updated to {0f, 1f, 0f, 0f, 0f}
    - after that, another random monster should be spawned (for example monster type 5)
    - the currentPopulationMix gets updated to {0f, 0.5f, 0f, 0f, 0.5f}

    This should be repeated over and over again until we've approximated currentPopulationMix to desiredPopulationMix.

    Sometimes Monsters will despawn if you're too far away from them which also updates the currentPopulationMix.

    Our plan is to maintain a certain number of certain monster types.

    I've coded for some time now but this is something new to me.

    Any idea how to accomplish that?


    Best regards
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    So this is a ton easier if you set a target amount of total monsters. Then you can figure out how many you want of each type through :
    Code (csharp):
    1. wantedCount[0] = desiredPopulationMix[0] * targetMonsterCount
    Then you can just check each monster type, figure out which one is lacking the most monsters, and spawn the one with the most lacking.

    If you want to go with the currentPopulationMix approach, it's pretty much the same thing, you calculate the mix by
    currentCountOfType[i] / totalCountMonsters
    , and figure out which value's the furthest away from it's desired value. The problem with this is that it's harder to read the code, and that the approximation might not ever finish. So I'd go with pre-calculating the count and then filling that up.
     
  3. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    First, an array is one of the collection types in Unity, and arrays are not dynamic - that is you can't change their size at run-time. You can create a new array of a different size. Lists are perhaps what you want to use, or dictionaries, because those can shrink and grow.

    Which you use depends: if the order of elements in the collection are important (it seems like it is arbitrary, from your description, and you just want to match the proper element in one collection with the other). Are both collections the same size or not? If not, I think you want to use dictionaries.

    It's not clear exactly what you need help with: the dynamic nature of the collection, meaning how to handle adding and removing elements, or the function that maps one to the other. If it's the latter we'd need more detailed information on the mapping. In your current scheme, each monster type has a place in the list? And you just want to know when all of the different type category counts match close enough between the collections?
     
  4. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Thank you for the answers!
    I've tried an approach that seems to work (haven't added the despawning yet).