• 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!

GML Random Enemy generator

U

Uranus2b

Guest
Hello guys!

I am having some problems on creating enemies. I made a code which it creates 5 enemies on random positions. But everytime one of the enemies appear one on top other or something like "glued". What can I do to make they appear randomly and not close to the others?
 

curato

Member
randomize the x and/or y when you created the instance. hard to give a better suggestion without seeing the code.
 
U

Uranus2b

Guest
Without any code example, we cannot help you...
oh sorry, here the code I made

Create event:
createenemy=false;
countdown= 100

///Step Event
if (createenemy)
{
repeat(5)
{
if place_empty(obj_enemy,obj_enemy)
{
instance_create(random(room_width),0, obj_enemy)
}
}
}
if countdown>=1
{
countdown-=1;
}

if countdown ==0{createenemy=true}

this is my code
 
D

Deleted member 13992

Guest
You start off by setting createenemy as false, but your obj_enemy creation code only runs if it's set as true.

EDIT: nevermind I didn't see the last line.

try if round(countdown) == 0 instead.

there are other issues with your code though. that's not how place_empty works. see manual:

 
Last edited by a moderator:
U

Uranus2b

Guest
You start off by setting createenemy as false, but your obj_enemy creation code only runs if it's set as true.

EDIT: nevermind I didn't see the last line.

try if round(countdown) == 0 instead.

there are other issues with your code though. that's not how place_empty works. see manual:

I thought i would works to avoid the enemies get create "glued" on each other. :/
 
D

Deleted member 13992

Guest
this might work better (untested)

GML:
//CREATE

countdown = 100;
create_count = 1;
create_enemy = false;
create_enemy_max = 5;




//STEP EVENT

if round(countdown) == 0 {
    create_enemy = true;
} else {
    countdown--;
}

var create_x = random(room_width);

if !place_meeting(create_x, 0, obj_enemy) && create_count <= create_enemy_max {
    instance_create(create_x, 0, obj_enemy);
    create_count++;
}
It's not as efficient as it could be though. It's just a brute-force approach to random positions. It can be made more efficient by nudging create_x slightly if place_meeting returns true.

Also, try and use the </> forum code tag for code. It's easier to read.
 
U

Uranus2b

Guest
1e6046aa-bdb8-45fb-b8c8-2a940a99860b.jpg well i tried the code, but the enemies still get glued like on the image



this might work better (untested)

GML:
//CREATE

countdown = 100;
create_count = 1;
create_enemy = false;
create_enemy_max = 5;




//STEP EVENT

if round(countdown) == 0 {
    create_enemy = true;
} else {
    countdown--;
}

var create_x = random(room_width);

if !place_meeting(create_x, 0, obj_enemy) && create_count <= create_enemy_max {
    instance_create(create_x, 0, obj_enemy);
    create_count++;
}
It's not as efficient as it could be though. It's just a brute-force approach to random positions. It can be made more efficient by nudging create_x slightly if place_meeting returns true.

Also, try and use the </> forum code tag for code. It's easier to read.
 
Top