GMS 2 The script does not always work [SOLVED]

sinigrimi

Member
Sorry to bother you again, but GML torments me. This time, the script does not always work for me, which should always work if I understand the mechanics correctly.
I specifically shot a video (it is below), code is bellow too. Short description, when I go into the room, "Room" = false" is activated
when the room is false, the "enemy" event is generated, and in the controller if the "enemy" = "BeeRoom" event is used, the BeeRoom () script is used. In practice, it always works for me that it says "enemy" = "BeeRoom", but the script itself does not always work. Help me please. And I'm sorry that a lot of questions, I'm new to this gml topic.


Code:
 if map[? "start"] == false
        {
           
                    randomize();
        var Lvl1 = choose("BeeRoom")//, "RatRoom", "SpiderRoom","RandomRoomOne","RandomRoomTwo","RandomRoomThree");
        map[? "enemy"] ="BeeRoom"// Lvl1;
        show_debug_message(string_letters(Lvl1));

Code:
   var _type = string_letters(map[? "enemy"]);
          //  var _xx = 160 + random(room_width - 320);
            //var _yy = 160 + random(room_height - 320);
                switch(_type)
                    {
                    case "BeeRoom":
                         BeeRoom();
                        break;

Code:
randomize();
            var x1 = 160 + random(room_width - 320);
            var y1 = 160 + random(room_height - 320);
            var x2 = 160 + random(room_width - 320);
            var y2 = 160 + random(room_height - 320);
            var x3 = 160 + random(room_width - 320);
            var y3 = 160 + random(room_height - 320);
            var x4 = 160 + random(room_width - 320);
            var y4 = 160 + random(room_height - 320);
            var x5 = 160 + random(room_width - 320);
            var y5 = 160 + random(room_height - 320);
if irandom(10) == 0{
    instance_create_layer(x1, y1, "Instances", oBee)
    instance_create_layer(x2, y2, "Instances", oBee)
    instance_create_layer(x3, y3, "Instances", oBee)
    instance_create_layer(x4, y4, "Instances", oBee)
    instance_create_layer(x5, y5, "Instances", oBee)
    show_message("0")
}
else if irandom (10) == 1{
    instance_create_layer(x1, y1, "Instances", oBee)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBeeMale)
    instance_create_layer(x4, y4, "Instances", oBee)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("1")
}
else if irandom (10) == 2{
    instance_create_layer(x1, y1, "Instances", oBeeMale)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBeeMale)
    instance_create_layer(x4, y4, "Instances", oBee)
    show_message("2")
}
else if irandom (10) == 3{
    instance_create_layer(x1, y1, "Instances", oBeeHive)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x4, y4, "Instances", oBee)
    show_message("3")
}
else if irandom (10) == 4{
    instance_create_layer(x1, y1, "Instances", oBee)
    instance_create_layer(x2, y2, "Instances", oBeeHive)
    instance_create_layer(x4, y4, "Instances", oBee)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("4")
}
else if irandom (10) == 5{
    instance_create_layer(x1, y1, "Instances", oBeeMale)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBeeMale)
    instance_create_layer(x4, y4, "Instances", oBeeMale)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("5")
}
else if irandom (10) == 6{
    instance_create_layer(x1, y1, "Instances", oBeeHive)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBeeMale)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("6")
}
else if irandom (10) == 7{
    instance_create_layer(x1, y1, "Instances", oBeeHive)
    instance_create_layer(x4, y4, "Instances", oBeeHive)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("7")
}
else if irandom (10) == 8{
    instance_create_layer(x1, y1, "Instances", oBeeHive)
    instance_create_layer(x2, y2, "Instances", oBee)
    instance_create_layer(x3, y3, "Instances", oBee)
    instance_create_layer(x4, y4, "Instances", oBee)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("8")
}
else if irandom (10) == 9{
    instance_create_layer(x1, y1, "Instances", oBeeHive)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBeeMale)
    show_message("9")
}
else if irandom (10) == 10{
    instance_create_layer(x1, y1, "Instances", oBee)
    instance_create_layer(x2, y2, "Instances", oBeeMale)
    instance_create_layer(x3, y3, "Instances", oBee)
    instance_create_layer(x4, y4, "Instances", oBee)
    instance_create_layer(x5, y5, "Instances", oBee)  
    show_message("10")
}
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
You are generating a 0..10 number up to 11 times, so there's a ~35% chance that you will not get a 1/11 chance event through that and thus no enemies.
Store the result of irandom call in a variable and compare that:
Code:
var r = irandom(10);
if (r == 0) {
...
} else if (r == 1) {
...
} else ...
 

Alice

Toolmaker of Bucuresti
Forum Staff
Moderator
What YellowAfterlife said - you are generating a new number for every if, so there's a chance that no if will get the correct result.

Another thing - this code can be greatly simplified. For each irandom value you place up to 5 enemies of specific enemy types. So you might use inline array of enemy types instead, then use a for loop to randomly place enemy of each listed type. To make multiple enemies of the same type, you add the type multiple times.

So, adjusting the logic I presented in another thread.
Code:
let enemiesToPlace = choose(
   [oBee, oBee, oBee, oBee, oBee], // 0th set of enemies
   [oBee, oBee, oBee, oBeeMale, oBeeMale], // 1st set of enemies
   [oBee, oBeeMale, oBeeMale, oBeeMale], // 2nd set of enemies
   [oBee, oBeeMale, oBeeHive], // 3rd set of enemies
   // and so on...
);

for (var i = 0; i < array_length_1d(enemiesToPlace); i++) {
   var enemyType = enemiesToPlace[i];
   var xx = irandom_range(160, room_width - 160);
   var yy = irandom_range(160, room_height - 160);
   instance_create_layer(xx, yy, "Instances",  enemyType);
}
 

sinigrimi

Member
You are generating a 0..10 number up to 11 times, so there's a ~35% chance that you will not get a 1/11 chance event through that and thus no enemies.
Store the result of irandom call in a variable and compare that:
Code:
var r = irandom(10);
if (r == 0) {
...
} else if (r == 1) {
...
} else ...
You are generating a 0..10 number up to 11 times, so there's a ~35% chance that you will not get a 1/11 chance event through that and thus no enemies.
Store the result of irandom call in a variable and compare that:
Code:
var r = irandom(10);
if (r == 0) {
...
} else if (r == 1) {
...
} else ...
Thank you very much, you were right. The variable really fixes this problem


What YellowAfterlife said - you are generating a new number for every if, so there's a chance that no if will get the correct result.

Another thing - this code can be greatly simplified. For each irandom value you place up to 5 enemies of specific enemy types. So you might use inline array of enemy types instead, then use a for loop to randomly place enemy of each listed type. To make multiple enemies of the same type, you add the type multiple times.

So, adjusting the logic I presented in another thread.
Code:
let enemiesToPlace = choose(
   [oBee, oBee, oBee, oBee, oBee], // 0th set of enemies
   [oBee, oBee, oBee, oBeeMale, oBeeMale], // 1st set of enemies
   [oBee, oBeeMale, oBeeMale, oBeeMale], // 2nd set of enemies
   [oBee, oBeeMale, oBeeHive], // 3rd set of enemies
   // and so on...
);

for (var i = 0; i < array_length_1d(enemiesToPlace); i++) {
   var enemyType = enemiesToPlace[i];
   var xx = irandom_range(160, room_width - 160);
   var yy = irandom_range(160, room_height - 160);
   instance_create_layer(xx, yy, "Instances",  enemyType);
}
And thank you very much, I am very grateful for the time you spent, your code works fine :)
 
Top