Search Unity

General Question About Design Pattern for Randomization

Discussion in 'Scripting' started by Ditofry, Feb 7, 2016.

  1. Ditofry

    Ditofry

    Joined:
    Feb 7, 2016
    Posts:
    2
    I'm new to Unity, I've been looking high and low and I haven't found anything that answers this question in a general sense yet:

    I want "collectable" items and enemies to spawn randomly, but I would like to expose an interface to the art and game design folks to be able to specify options for spawning, like "Once a Day", "every 30 seconds", "High Concentration", "Low Concentration" etc. I would love it if these options were on any prefab that needs to be randomized, so that a game designer could click on the "backpack" prefab, open its spawning options, and select "Once a Day", "Dispersed Infrequent" or whatever.

    At a high level, can someone recommend a design pattern to achieve this? My rough intuition based on my research so far is to build all my "random spawn" entities by extending a class that has the public properties which control spawn rate, and then perhaps a singleton that spawns "registered" gameobjects based on their settings.

    I would really appreciate any general feedback. I'm absolutely LOVING Unity so far, by the way! Thanks so much!
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    How about add a component with enum (or different typed values, depends on you needs) containing all values you need for randomization and attaching it to prefabs? Then when checking you get component on a prefab and check its value. You will still need to keep a list of all prefabs that can be spawned and need to be checked somewhere.
     
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    So basically you want a collection of things that you want to randomly choose, and some kind of scheduler to re-insert things back into the collection? http://www.redblobgames.com/articles/probability/damage-rolls.html This has some things to help you understand how to do the collection. The scheduler is probably a bit more complicated, but it's basically an object pool that pays attention to when the object needs to be re-introduced.
     
  4. Ditofry

    Ditofry

    Joined:
    Feb 7, 2016
    Posts:
    2
    Thanks guys! I'll be taking some advice from both of you. I may come back here eventually to give a rundown of how I end up implementing things to see what the experienced C#'ers and Unity Devs think. I appreciate the help!