How to choose a random instance every frame?

Discussion in 'Programming' started by grrrvahrrr, Jul 12, 2019.

  1. grrrvahrrr

    grrrvahrrr Member

    Joined:
    May 30, 2019
    Posts:
    5
    Hi! I am trying to recreacte space invaders and i got a bit stuck at properly spawning enemy bullets.
    I want a random enemy object from the bottom row to create 1 bullet with a certain cooldown. I got stuck on choosing a random instance of an enemy object to create a bullet. And i am a bit confused about where to put a random variable, because when i put it in a create event it only randomises once, and putting it in step event doesn't seem to work. Also with my code several enemies from bottom row create bullets, not 1...
    Create event
    Code:
       
    cooldown = 0;
    
    randomise ();
    
    inst = instance_find(obj_enemy, irandom(instance_number(obj_enemy)-1));
    
    step event
    Code:
    with (inst) {
        if !collision_line(x, y+60, x, y+300,obj_enemy,false,false) && (cooldown < 1) {
            instance_create_layer(x,y,"Instances",obj_e_bullet);
            cooldown = 120;
            }   
    }
    
    if !instance_exists(obj_e_bullet) cooldown -= 1;
    
     
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,535
    randomize goes in neither of the two. It belongs in something that happens once when the game starts and is not to be called again afterwards.

    That aside, please post the actual code of when you try this in a Step event, describe what is supposed to happen and what happens instead. Please don't summarize things as "doesn't work".
     
  3. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    295
    inst = instance_find(obj_enemy, irandom(instance_number(obj_enemy)-1)); would need to be in the step event before the with to get it to work. Also, it looks like you only want one bullet on the screen at once. It may make more sense to make the next bullet in the outside room event or when you get rid of the first bullet. It would be a lot less overhead in your game anyways.
     
  4. grrrvahrrr

    grrrvahrrr Member

    Joined:
    May 30, 2019
    Posts:
    5
    With the code i posted what happens is several enemies from the bottom row spawn several bullets at the same time and the very same enemies continue to spawn bullets at cooldown. What i want is a random enemy from a bottom row spawn a bullet, cooldown, another random enemy spawn a bullet and so on.

    Putting inst variable in step event seem to randomise which enemies spawn bullets. In the beginning all enemies from 1st row spawn bullets, then only some of them, sometimes only one them. What do you mean by creatin bullet in the outside room event? I am a noob at programming in general and try to learn as i go)
     
  5. grrrvahrrr

    grrrvahrrr Member

    Joined:
    May 30, 2019
    Posts:
    5
    Found the solution on r/gamemaker. Thanks to x2115. The problem was with cooldown. It wasn't global so replacing.

    Code:
    cooldown = 120;
    
    with

    Code:
    with(obj_enemy){cooldown=120}
    
    made it work as I wanted to)
    And i placed my inst variable into step event and got rid of randomise
     

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