1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Methods to spawning an object semi-consistently?

Discussion in 'Game Design, Development And Publishing' started by Calvert, Sep 27, 2019.

  1. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    Suppose you want a random/semi-random system that somewhat steadily spawns a type of object.

    For example, spawning meteors that a player must deal with in an arcade style game: Meteors should be unpredictable, but generally there shouldn't be too many or too little at a given time.

    How would you go about it? I am considering multiple different methods.
     
  2. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    421
  3. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    I consider this method as a good base to begin with, but doesn't it sound quite predictable on it's own?

    [Unfortunately I am not in a position to check the linked tutorial at the moment]
     
  4. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    421
    not really, you use the alarm to control how many are on the screen at once and you can use irandom to have the system gice you random positions for them to start so they wouldn't be the same all the time.
     
    Calvert likes this.
  5. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    This is good, but I am considering something less consistent than that.

    Suppose you don't want to control how many [objects] are on the screen at once with such sovereignty. Perhaps you would want anywhere from 1 to 3, but without giving yourself the ability to predict which it will be. What then?
     
  6. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    In your code that runs when the alarm triggers, randomize the length of the next alarm.
     
    Calvert likes this.
  7. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    421
    then just add a random number from 1 to 3 and have a loop create however many you want or you could randomize the alarm interval too.
     
    Calvert likes this.
  8. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    I think randomizing the alarm interval is the key I was looking for, thanks.

    Either way, this topic is still ground for further discussion.
     
  9. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,607
    Randomizing the alarm interval gives a definite bound on the frequency, and in certain cases this can still look too predictable. If you want to be even less consistent than that, use the properties of expected value on a uniform distribution.

    Let P be the number of occurrences you want per unit time divided by the length of a unit of time. Then run this in the Step event:
    Code:
    if (random(1) <= P) {
      // Action
    }
    
    An example of P is 1/room_speed to approximate one occurrence per second.
     
    Calvert likes this.
  10. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    Thanks, this seems to hold a more desirable amount of consistency for me.

    This method has the potential to omit the use of alarms, am I correct?

    Using your example, how would you set an approximation of [less than] 1 occurrence per second? How would you set approximately 5 occurrences per minute?
     
  11. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,607
    The expected value method doesn't need alarms. Alarms are for when you need exact control over the interval.

    Don't you understand this line?
    Code:
    Let P be the number of occurrences you want per unit time divided by the length of a unit of time.
    Your number of occurrences is 5. Your unit of time is 1 minute (i.e. 60*room_speed). Then P is 1/(12*room_speed).
     
    Calvert likes this.
  12. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    421
    room_speed is the reference time for one second so just multiply by how many seconds you need.
     
  13. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,460
    I was a little confused by that line actually.
    Perhaps something clearer would be:
    Let P be 1 over the expected time between occurrences in frames.

    So if you want 5 per minute, you would expect 60/5 seconds between occurrences, or 12 seconds.
    12 seconds is (12 * room_speed) frames.
    So P is 1/(12 * room_speed)
     
    Calvert likes this.
  14. Calvert

    Calvert Member

    Joined:
    Dec 24, 2017
    Posts:
    133
    Thank you. This, along with GMWolf's and curato's explanations make perfect sense.

    I understood your line. I didn't understand the code behind your line.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice