• 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 [SOLVED]Trying to randomly generate map.

A

azuarf

Guest
I am trying to generate random tiles for my map. But it only makes one instance and thats at the middle of the screen far away from where the first instance should be. Here is the code that i used.

xi = 0
yi = 8000
for (i = 0; i < 2000; i+=1)
{
rand = random_range(1,3);
if (rand = 1)
{
instance_create(xi,yi,obj_floor1);
}
else if (rand = 2)
{
instance_create(xi,yi,obj_floor2);
}
else if (rand = 3)
{
instance_create(xi,yi,obj_floor3);
}
xi += 64;
if (xi >= 1920)
{
yi += 64;
xi = 0;
}
}


This code is in the left mouse button event. Can someone explain why this dose not work? I am an amature. i tried drawing sprites instead of creating instances but it dose somewhat the same thing. a single sprite flickers at where the instance is in the pic and then nothing. i thought the problem might be because of draw event so i tried creating instance. heres the code i used for drawing sprite. its the same as the one above though.

xi = 0
yi = 8000
if(global.adv1_state = 1)
{
for(i = 0; i <2000; i += 1;)
{
if (global.adv1_array = 1)
{
draw_sprite(spr_bg1,0,xi,yi);
}
else if(global.adv1_array = 2)
{
draw_sprite(spr_bg2,0,xi,yi);
}
else if (global.adv1_array = 3)
{
draw_sprite(spr_bg3,0,xi,yi);
}
xi += 64;
if(xi >= 1920)
{
yi += 64;
xi = 0;
}
}
}

the adv1_array is generated in a mouse button event. Am i using arrays in a wrong way? i am fairely new to programing. Thanks for reading this.
 

Attachments

Last edited by a moderator:

Stubbjax

Member
Replace random_range(1, 3) with either irandom_range(1, 3) or choose(1, 2, 3). Random range returns any number between 1 and 3, e.g. 1.232, 2.321. Also, why are you even using a for loop if you're not using i for anything? You can use repeat(2000) instead.
 
A

azuarf

Guest
Replace random_range(1, 3) with either irandom_range(1, 3) or choose(1, 2, 3). Random range returns any number between 1 and 3, e.g. 1.232, 2.321. Also, why are you even using a for loop if you're not using i for anything? You can use repeat(2000) instead.
it worked. thank you very much. was using for loop because "i" was being stored in an array which i was trying to draw the sprites from so the draw event can draw by using the array. i figured if i randomize in the draw event itself the sprites will change every frame.

In the left mb event:

for(i = 0; i >= 2000; i += 1)
{
n = choose(1,2,3); /* I was using random_range here so it didnt work.
global.array = n;
}
global.adv1_state = 1;

In the draw event:

xi = 0
yi = 8000
if(global.adv1_state = 1)
{
for(i = 0; i <2000; i += 1;)
{
if (global.adv1_array = 1)
{
draw_sprite(spr_bg1,0,xi,yi);
}
else if(global.adv1_array = 2)
{
draw_sprite(spr_bg2,0,xi,yi);
}
else if (global.adv1_array = 3)
{
draw_sprite(spr_bg3,0,xi,yi);
}
xi += 64;
if(xi >= 1920)
{
yi += 64;
xi = 0;
}
}
}


Now it works :D
 

Stubbjax

Member
Glad I could help! Just as an aside, you can increase the efficiency by assigning the sprites to an array (in the create event or wherever) which can be accessed using the global array value to draw the respective sprites, like this:
Code:
spr[0] = spr_bg1;
spr[1] = spr_bg2;
spr[2] = spr_bg3;
Then in the draw event you can simply have this code in the loop, instead of all the if-statements:
Code:
draw_sprite(spr[global.adv1_array - 1], 0, xi, yi);
 
A

azuarf

Guest
Glad I could help! Just as an aside, you can increase the efficiency by assigning the sprites to an array (in the create event or wherever) which can be accessed using the global array value to draw the respective sprites, like this:
Code:
spr[0] = spr_bg1;
spr[1] = spr_bg2;
spr[2] = spr_bg3;
Then in the draw event you can simply have this code in the loop, instead of all the if-statements:
Code:
draw_sprite(spr[global.adv1_array - 1], 0, xi, yi);
Code:
spr[1] = spr_bg1;
spr[2] = spr_bg2;
spr[3] = spr_bg3;
if(global.adv1_state = 1)
{
for(i = 0; i < 2000; i+= 1)
{
draw_sprite(spr[global.adv1_array[i]],0,xi,yi)
xi += 64;
if (xi >= 1920)
{
yi += 64;
xi = 0;
}
}
}
thanks. it looks a bit better now. and it works. :D
 
Top