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
  2. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Randomizing Spawn throughout the game [SOLVED]

Discussion in 'Programming' started by Fyre, Oct 15, 2018.

  1. Fyre

    Fyre Member

    Joined:
    Oct 15, 2018
    Posts:
    4
    Hello!
    For my game I'd like to spawn cars in randomized places (either a or b), randomized gaps between the spawns and then also randomized speed (altough I havent written the code for that yet)
    However, I encountered the issue that it is random at the start of the game but then it continues to continue with the same values. (Always spwaning at the same place in the same frequency during the run of the game)

    In the create Event:
    Code:
    randomize();
    {
    alarm[2] = random_range(1,6) * room_speed;
    spawnrate = random_range(60,200);
    alarm[0] = spawnrate;
    spawnplace = irandom_range(1,2)
    }
    
    Alarm[0]
    Code:
    if(spawnplace %2 == 1)
    {
    instance_create_layer(-100, 270 , "CarLayer", obj_car);
    }
    else
    {
    instance_create_layer(-100, 510 , "CarLayer", obj_car);
    alarm[0] = spawnrate;
    }
    
     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,430
    Add a
    Code:
    randomize();
    
    prior to first random_* call for the values to be randomized each run.
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,037
    He does have a randomize() in his create event.

    Curious what is in his alarm[2] since he went through the trouble of randomizing that as well.

    You need to move the alarm[0]=spawnrate OUTSIDE of the brackets.

    Why not just use irandom(1) here?
     
  4. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    423
    You also need to recalculate the value of spawnrate and spawnplace so it can be different each time. Setting it in the create event only runs once when the object is created so it will continuously use the same values.
     
  5. Fyre

    Fyre Member

    Joined:
    Oct 15, 2018
    Posts:
    4
    To be honest, I just noticed its unecessity when I copied my code here, I guess I tried something out and forgot to delete that too. Not sure anymore but its gone now anyway.

    Okay, I did that.

    irandom_range made more sense for me when I've read it up, Im really new to programming. ^^'
     
  6. Fyre

    Fyre Member

    Joined:
    Oct 15, 2018
    Posts:
    4
    Ah, I see!
    Then which event would be suited better?
    I'd say step or draw..
    Im sorry, Im really new to Game Maker and programming in general >_<'
     
    Last edited: Oct 15, 2018
  7. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    423
    Like TheouAegis said put it inside the alarm outside the brackets

    Code:
    if(spawnplace %2 == 1)
    {
    instance_create_layer(-100, 270 , "CarLayer", obj_car);
    }
    else
    {
    instance_create_layer(-100, 510 , "CarLayer", obj_car);
    }
    
    spawnrate = random_range(60,200);
    alarm[0] = spawnrate;
    spawnplace = irandom_range(1,2)
    
    
     
  8. Fyre

    Fyre Member

    Joined:
    Oct 15, 2018
    Posts:
    4
    Yes, it works. Thank you!!
     

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