Hello!
I'm trying to get a "light blinking randomly" effect to work in a specific room.
To do that, I'm trying to create a recursive timer that just switches the background and lasts a different random (float) amount of seconds each time it's triggered.
To generate a random float number I'm using the RandomFloat function described in this thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=50946.msg636495902#msg636495902).
Also, I'm using the Tween Module as well.
The code is structured in the following way:
In Global Variables, I've put a float variable to store the timer's time (float randLightTime = 0.0)
In my GlobalScript.asc:
// At the very top of the file
float RandomFloat(float max) {
int huge = 2147483647;
float f = IntToFloat(Random(huge)) / IntToFloat(huge); // 0.0 - 1.0
return f * max;
}
function repeatedly_execute_always()
{
if (cEgo.Room == 2) {
if (IsTimerExpired(9)) {
SetBackgroundFrame(GetBackgroundFrame()-1);
randLightTime = RandomFloat(2.5);
SetTimer(9, SecondsToLoops(randLightTime));
}
}
}
The timer is set in the room where this blinking light should be working (Room 2):
function room_Load()
{
SetTimer(9, SecondsToLoops(1.0));
}
I'm getting an error that involves both line 4 and line 13 in the GlobalScript.asc.
When I enter the room the game crashes and I get this message: "Random: invalid parameter passed -- must be at least 0".
I tried starting the timer in the game_start function and also tried changing the initial randLightTime value, without luck.
Am I missing something really stupid?
Thanks in advance!
This is how Random is implemented in AGS:
int __Rand(int upto) {
upto++;
if (upto < 1)
quit("!Random: invalid parameter passed -- must be at least 0.");
return rand()%upto;
}
This means that max value passed to Random can be INT_MAX - 1 (2147483646).
BTW, the rand() function it uses (from C library) does not guarantee a return value higher than 32767 (may be higher on some systems, but Windows has it like that).
I believe this info should be added to manual.
To fix your script, simply set "int huge = 32767;".
Thanks a million! I really didn't think of that as a problem, because somehow when I was working on Win8.1 it didn't gave me any kind of issue.
Then once I switched over to a Win7 system, and after I made some amendments to the code, I couldn't get it to work anymore (laugh)
Now that I've tweaked the value of int huge to 32767 (which makes sense, anyway) the problem seems to be fixed so, again, thanks! :D