Haha yeah but I Imagine there is some reason at engine level to do it that way. But I thought that in GMS 1.4 or GM8 that Game Start was before Create.
If that wasn't the case then either then my whole life has been a lie.
At your service - I'm here to ruin your life. Engage the test results!
Task: Output the name of the Create, Room Start and Game Start events as well as instance creation code and room creation code when they fire.
GMS 2.x, runtime 2.2.2.326:
Create
Instance creation code
Game Start
Room creation code
Room Start
GM:S 1.x, 1.4.9999:
Create
Instance creation code
Game Start
Room creation code
Room Start
GM 8.1.141:
Instance creation code
Create
Game Start
Room creation code
Room Start
Conclusion: Don't rely on the event order. Unlike hoping for the event order to play along nicely,
@FrostyCat's proposed solution is foolproof under all circumstances.
I thought randomize doesnt change because the game seed doesnt change, when the game is compiled into an .exe then randomize is diffrent everytine
Unless you run
randomize, the seed will be the same every time you run the game. Whether the game is compiled or not makes no difference either way - you either randomize it or it's always the same under any circumstances.
Wait so you guy's are saying your supposed to use the create event with "randomize()"? I was always told to use Game Start with "randomize()" or the room creation code. And I was told to not put it in the creation event. Was it different in GMS 1.4?
Well it is in fact working now that I put it in the creation event. I could have sworn in the past you weren't supposed to do that. Thank you both for your help.
The phrase "at game start" (which is a carbon copy from the manual) which I like to use when
randomize is the context doesn't necessarily mean "in the Game Start event". It means "once, and
only once during the game's run time, typically at the start and not at any time in between start and end". Allow me to elaborate.
Putting it in the Create event is technically wrong, but may be producing results of
the same nature as what you expect. Given that you're expecting anything but predictable results, it's easy to mistake non-identical results as properly randomized.
That still makes it wrong, though, and it's the kind of wrong that will go unnoticed until it bites you in the back.
If you run
randomize multiple times, the seed will be set to a new value multiple times. The frame of the PRNG (read: the "position" in the list of pseudo-random numbers that will be generated with the given seed) will also be reset.
Let's examine this simple piece of code:
Code:
repeat(4)
{
randomize();
show_debug_message(irandom(100));
}
Result: (manually formatted for easier readability because I didn't want to over-complicate the code with proper output formatting)
(The 11 will be important in a moment.)
This will randomize the seed every time before generating a number. The results of these are not replicable - at least
not fully, since what you are really getting here is the following:
[S1F1] [S2F1] [S3F1] [S4F1]
(S: seed, F: frame)
Using the following code after the first one, you can replicate the last generated number, and
only the last one. It will be the first number to be output, as the seed you are copying is the one that was used to generate the last one.
Code:
var rnd = random_get_seed();
random_set_seed(rnd);
repeat(4)
{
show_debug_message(irandom(100));
}
Result:
Which is actually:
[S4F1] [S4F2] [S4F3] [S4F4]
You can verify that this is the case by using the first piece of code and generating three further numbers after its execution:
Code:
repeat(4)
{
randomize();
show_debug_message(irandom(100));
}
repeat(3)
{
show_debug_message(irandom(100));
}
Which is actually:
[S1F1] [S2F1] [S3F1] [S4F1] [S4F2] [S4F3] [S4F4]
Ignore the first three numbers, as they are not a part of the sequence of seed 4. The last four generated numbers will be equal to the sequence generated by re-setting the seed to the current seed - remember that this will also reset the frame, so you're starting at S4F1.
You can then replicate the last generated numbers like before:
Code:
var rnd = random_get_seed();
random_set_seed(rnd);
repeat(4)
{
show_debug_message(irandom(100));
}
Result:
Which is actually:
[S4F1] [S4F2] [S4F3] [S4F4]
Therefore, the results become
fully replicable if you only set the seed once, generate four numbers, re-set the seed to itself and generate four numbers again:
Code:
randomize();
repeat(4)
{
show_debug_message(irandom(100));
}
var rnd = random_get_seed();
random_set_seed(rnd);
repeat(4)
{
show_debug_message(irandom(100));
}
Result: