• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

SOLVED Code should work in theory, why is it spawning only 1 or 2 trees per load in?

aristhemage

Member
Hello, I have a create code that goes as follows:
GML:
var wait = 0
for (var i = 0; i < 80; i++){
var number = irandom_range(0,5);
var number2 = irandom_range(0,1);
if wait = 0 {
if number = 2{
    //Tree One
    if number2 = 1{
world[worldArrayLength++] = instance_create_layer(i * 64, 704, "Blocks", block_log);
world[worldArrayLength++] = instance_create_layer(i * 64, 640, "Blocks", block_log);
world[worldArrayLength++] = instance_create_layer(i * 64, 576, "Blocks", block_log);
world[worldArrayLength++] = instance_create_layer(i * 64, 512, "Blocks", block_log);
world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 512, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 512, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 - 128, 448, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 448, "Blocks", block_leaves);world[worldArrayLength++] = instance_create_layer(i * 64, 448, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 +64, 448, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 +128, 448, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 +64, 384, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 , 384, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 -64, 384, "Blocks", block_leaves);
world[worldArrayLength++] = instance_create_layer(i * 64 , 320, "Blocks", block_leaves);
}
 wait++;
  if wait >= 1{
     var number3 = irandom_range(0,3)
     if number3 = 3
     {
         wait++
     }
     if wait = 2
     {
    wait = 0;     
     }
 }
}
}
I want it so that the trees don't spawn right next to each other (I'd prefer them to be at least 4-ish blocks apart)
and I have spammed R (debug thing to restart room) about 500 times, so I know it's not luck.
What should i do?
 

Simon Gust

Member
Interesting code you have there.
Your wait variable can get stuck at 1 and your code is dead at that point. Your first if-clause is cutting you off hard.
Try this
GML:
var wait = 0
for (var i = 0; i < 80; i++)
{
    if (--wait <= 0)
    {
        var chance = random(100);
        if (chance < 10) // ~10% chance
        {
            world[worldArrayLength++] = instance_create_layer(i * 64, 704, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 640, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 576, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 512, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 512, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 512, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 128, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64,      448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 128, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 , 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 , 320, "Blocks", block_leaves);
            
            wait = irandom_range(3, 6);
        }
    }
}
 

aristhemage

Member
Interesting code you have there.
Your wait variable can get stuck at 1 and your code is dead at that point. Your first if-clause is cutting you off hard.
Try this
GML:
var wait = 0
for (var i = 0; i < 80; i++)
{
    if (--wait <= 0)
    {
        var chance = random(100);
        if (chance < 10) // ~10% chance
        {
            world[worldArrayLength++] = instance_create_layer(i * 64, 704, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 640, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 576, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64, 512, "Blocks", block_log);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 512, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 512, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 128, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64,      448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 128, 448, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 + 64, 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 , 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 - 64, 384, "Blocks", block_leaves);
            world[worldArrayLength++] = instance_create_layer(i * 64 , 320, "Blocks", block_leaves);
           
            wait = irandom_range(3, 6);
        }
    }
}
This works just fine! Thank you!
 
randomise() is not a function you use multiple times. Call it once the very first time your game is loaded and that's it. What it does is "randomise" the seed used for the random*, irandom*, etc functions. This ensures that you don't start from the same seed each time you boot your game. If you DID start the game with the same seed each time, every random call would return the same result as it did the previous time the game ran, thus not actually being random.

If you call randomise() multiple times in your game, you will be resetting the seed multiple times and that can lead to unwanted results.
 

Joe Ellis

Member
randomise() is not a function you use multiple times. Call it once the very first time your game is loaded and that's it. What it does is "randomise" the seed used for the random*, irandom*, etc functions. This ensures that you don't start from the same seed each time you boot your game. If you DID start the game with the same seed each time, every random call would return the same result as it did the previous time the game ran, thus not actually being random.

If you call randomise() multiple times in your game, you will be resetting the seed multiple times and that can lead to unwanted results.
I wouldn't say it leads to unwanted results, just calling randomize makes a completely random seed, so calling it several times per game is pointless, but it won't hurt to call it several times, it'd just be pointless cus the results would be just as random either way.
 
@Joe Ellis Depending on what you're doing it can definitely have unwanted results. For instance, let's say you are doing some procedural generation of a map and you don't want to physically store the generated map in memory. You would need each cell to be able to regenerate the same results each time you view it (or whatever). If you are calling randomise multiple times in your game, you are changing your original seed and thus destroying your ability to regenerate the original content for each cell. There's other scenarios where randomise being called more than once can compromise things, but that's the first thing that came to my head.
 

Joe Ellis

Member
@Joe Ellis Depending on what you're doing it can definitely have unwanted results. For instance, let's say you are doing some procedural generation of a map and you don't want to physically store the generated map in memory. You would need each cell to be able to regenerate the same results each time you view it (or whatever). If you are calling randomise multiple times in your game, you are changing your original seed and thus destroying your ability to regenerate the original content for each cell. There's other scenarios where randomise being called more than once can compromise things, but that's the first thing that came to my head.
Yeah but in that case of generating a level, you'd set a particular seed, so randomize would not be able to be used at all, at least not while the level is generating.
 
Top