Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: edmundito on Fri 16/06/2006 04:38:02

Title: Randomizing numbers when restarting game
Post by: edmundito on Fri 16/06/2006 04:38:02
I was wondering if there is any way to Randomize() the Random() function. because I have something like this:

int a;
function game_start() {
  a = Random(100);
}

It works fine, but I want it so that when I restart the game, it randomizes those variables again. I tried SetRestartPoint(); before that but nothing happened. it seems that my randomized variables remained the same.
Title: Re: Randomize()?
Post by: Gilbert on Fri 16/06/2006 05:21:49
The problem is that, using RestartGame() merely just does a RestoreGame() action, loading the game saved (I think it's #999) when SetRestartGame() was invoked, so it's after all the initialization stuff were done (so all the variables were set and saved as game_start() won't be executed again).

This is an idea to do the job.
First, if there're multiple random variables set, you may better move them to a function first, just for convenience's sake (as you need to call this again when a game is restarted) , like:
function InitRandomThingie() {
  haha = Random(100);
  blah = Random(1000);
  ...
}
and call it once in game_start().

Then in the on_event() function add:
if (event == eEventRestoreGame&&data==999) InitRandomThingie();

That's all!
Title: Re: Randomize()?
Post by: Kweepa on Fri 16/06/2006 07:16:05
You can also do this:

Quote from: SteveMcCrea on Fri 26/05/2006 01:03:09It's probably because the random number generator is reset when the game is restarted, so it produces the same values again.
Try using the current time to randomise the sequence.

DateTime *dt = DateTime.Now;
int i = 0;
while (i < dt.Second)
{
  int dummy = Random(1);
  i++;
}
// your code here


Title: Re: Randomize()?
Post by: Gilbert on Fri 16/06/2006 07:44:16
Noted that Edmundo's variables were set in game_start(), so unless he moved that part to somewhere else, the random number weren't re-generated using this method.
Title: Re: Randomize()?
Post by: Radiant on Fri 16/06/2006 08:12:17
The easiest thing to do is invoke the system timer... the amount of milliseconds since midnight is, for all practical purposes, random.
Title: Re: Randomize()?
Post by: Gilbert on Fri 16/06/2006 11:06:26
The method I mention seems to be simplest, and it doesn't even need to use time functions, and it seemed to work when I tested it.
Title: Re: Randomize()?
Post by: Radiant on Fri 16/06/2006 13:37:43
A: No, my method is simpler.
B: No, MY method is simpler.
A: Mine.
B: Mine!
A: MINE!
B: Slacker.
A: Loser.
B: Ruffian.
A: Fop.

(insert rest of Fettucini quote here :) )
Title: Re: Randomize()?
Post by: edmundito on Fri 16/06/2006 18:38:49
*Use boiling pot with fetuccini brothers.*

Guys, Gilbert's solution worked just fine. I really didn't know that AGS quirk about restarting the game. I guess you learn something new every day.