• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM randomize enemy fire (solved)

Niels

Member
Hi everyone,

I followed heartbeasts top down space shooter udemy course, and i'm expanding the game with boss fights and extra powerups.

Right now I have a bossfight object that spawns when you reached a certain score.
the bossenemy (see attached image) is suposed to fire lasers at random from his "stalks"...
I know it has to be done with the choose or random_range command, but I have no idea how to put it in GML :(

right now I tried this code but it will crash my game:
Code:
///fire laser
random_range(shoot1,shoot3);

if shoot1
{
instance_create (x-14,y+8,obj_laser_enemy);
}

if shoot2
{
instance_create (x,y+8,obj_laser_enemy);
}
if shoot3
{
instance_create (x+16,y+8,obj_laser_enemy);
}
alarm[LASERALARM] = room_speed;
It's probably a easy thing to code, but i haven't found it yet

thanks in advance.

-Niels
 

Attachments

A

amusudan

Guest
Do this:
///fire laser
shoot = irandom_range(1,3);

if shoot = 1
{
instance_create (x-14,y+8,obj_laser_enemy);
}

if shoot =2
{
instance_create (x,y+8,obj_laser_enemy);
}
if shoot =3
{
instance_create (x+16,y+8,obj_laser_enemy);
}
alarm[LASERALARM] = room_speed;
 
P

Paolo Mazzon

Guest
You're not setting anything with random range.
Code:
///fire laser
var shoot = random_range(shoot1,shoot3);

if (shoot == shoot1)
{
    instance_create (x-14,y+8,obj_laser_enemy);
}
else if (shoot == shoot2)
{
    instance_create (x,y+8,obj_laser_enemy);
}
else if (shoot == shoot3)
{
    instance_create (x+16,y+8,obj_laser_enemy);
}
alarm[LASERALARM] = room_speed;
You're basically just calling random and then testing to see if three numbers are true. Also, if you have multiple if statements in a row like that, use else if.
 

FrostyCat

Redemption Seeker
Did Heartbeast teach you to read the Manual? From the looks of how you used random_range(), either he didn't give a damn about it, or you didn't.

For multiple exact comparisons of a single value like this, the best method is with a switch block. No extra variable, cleaner syntax.
Code:
switch (irandom(2)) {
  case 0:
    instance_create(x-14, y+8, obj_laser_enemy);
  break;
  case 1:
    instance_create(x, y+8, obj_laser_enemy);
  break;
  case 2:
    instance_create(x+16, y +8, obj_laser_enemy);
  break;
}
alarm[LASERALARM] = room_speed;
 

Niels

Member
@ FrostyCat:
No need to get rude...
like I said in my first post, I followed the tutorial (which helped a lot) and I'm trying to implent stuff myself now to learn GML. The whole boss fight wasn't part of the tutorial.

Right now I have added a shield Powerup and 2 new enemies, thats something I wouldn't be able to without the help of youtube tutors like heartbeast

@amusedan

Thanks a lot, your code works perfectly!
 
A

amusudan

Guest
You're not setting anything with random range.
Code:
///fire laser
var shoot = random_range(shoot1,shoot3);

if (shoot == shoot1)
{
    instance_create (x-14,y+8,obj_laser_enemy);
}
else if (shoot == shoot2)
{
    instance_create (x,y+8,obj_laser_enemy);
}
else if (shoot == shoot3)
{
    instance_create (x+16,y+8,obj_laser_enemy);
}
alarm[LASERALARM] = room_speed;
You're basically just calling random and then testing to see if three numbers are true. Also, if you have multiple if statements in a row like that, use else if.
You should use irandom though :)
 
A

amusudan

Guest
Did Heartbeast teach you to read the Manual? From the looks of how you used random_range(), either he didn't give a damn about it, or you didn't.

For multiple exact comparisons of a single value like this, the best method is with a switch block. No extra variable, cleaner syntax.
Code:
switch (irandom(2)) {
  case 0:
    instance_create(x-14, y+8, obj_laser_enemy);
  break;
  case 1:
    instance_create(x, y+8, obj_laser_enemy);
  break;
  case 2:
    instance_create(x+16, y +8, obj_laser_enemy);
  break;
}
alarm[LASERALARM] = room_speed;
Much cleaner yes, you should use this @Niels
 
Top