Spawning Randomly

Discussion in 'Programming' started by Cmi, Jul 9, 2016.

Thread Status:
Not open for further replies.
  1. Cmi

    Cmi Guest

    So this code worked for me but after an update it started messing up. I want to randomly spawn an object with in the room but it spawns in the same pattern every time. here is the code:
    // Spawn pie (random(x),random(y),obj_enemy
    instance_create(random(608),random(-32),obj_enemy)

    that's my code it just spawns in the same pattern every time even when I restart the game. Can you guys help me fix the code or get a better to spawn them better and randomly?

    SOLVED :D

    Create:
    // Wait 2 seconds and than trigger the alarm
    alarm [1] = 60;
    // Randomize the spawn
    randomize();

    Step:
    // Spawns the object
    instance_create(irandom_range(0,608),irandom_range(0,-32),obj_test)
    // Repeat this code
    alarm[1] = 60;
     
    Last edited by a moderator: Jul 12, 2016
  2. anomalous

    anomalous Member

    Joined:
    Jun 29, 2016
    Posts:
    464
    you need to use randomize(); somewhere, look up in manual, default is same seed. Randomize will generate new seed each time.
     
  3. Cmi

    Cmi Guest

    So do I replace the random with randomize?
     
  4. Aura

    Aura Guest

  5. Cmi

    Cmi Guest

    By same pattern I mean they spawn the same every time. Like when I restart the game they spawn in the same locations like they did when I start the game
     
  6. anomalous

    anomalous Member

    Joined:
    Jun 29, 2016
    Posts:
    464
    Just put that line in the create of your controller before any random functions are called. It's worth a quick read, like 30 seconds in the manual.

    Yes, random functions in GMS are defaulted to use the same random seed for debug purposes. You will see the same random on purpose.
    If you instead put randomize(); in an event before that, it will pick a new seed each time, and you'll get more random results.
     
  7. Cmi

    Cmi Guest

    So I did it but they don't spawn within the ranges that I set, I was wondering if you can fix it. it's just am new to GM thats why I may sound like a newbie :(
    here is the link: https://mega.nz/#!7I8mzTjS
     
  8. Cmi

    Cmi Guest

    Do you mind taking a look at it and fixing if you can?
    Link: https://mega.nz/#!7I8mzTjS
     
  9. Aura

    Aura Guest

    Sadly I (most of us) don't have enough time to download a completely unknown file to inspect for helping you. But I'm more than happy to help if you post all the code that relates with spawning the instances. For example, what does the script pie() do? So share that sort of information then we'd be able to help. ^^

    Also: Doesn't calling randomize() help?
     
  10. Cmi

    Cmi Guest

    Sorry for the late reply I finally figured it out. I will post what I was trying to do.
     
  11. {Anonymouscoder1}

    {Anonymouscoder1} Member

    Joined:
    Nov 22, 2018
    Posts:
    2
    Hi I'm new.

    Every time I have this code in my enemy the game lags a ton what could I do about this?
     
    Last edited: Nov 22, 2018
  12. {Anonymouscoder1}

    {Anonymouscoder1} Member

    Joined:
    Nov 22, 2018
    Posts:
    2


    Hi could you put this into code form please
    also my game starts to lag when I put this code in anything I could do to stop this from happening
     
  13. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,903
    Necro-bumping a 2 year old thread won't get you much help... Please make your own topic and in it explain exactly what the issue you have is, what code you have that you think is causing the issue, and any examples of what you would like to happen over what is happening.
     
Thread Status:
Not open for further replies.

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