Randomizing numbers when restarting game

Started by edmundito, Fri 16/06/2006 04:38:02

Previous topic - Next topic

edmundito

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.
The Tween Module now supports AGS 3.6.0!

Gilbert

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!

Kweepa

#2
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.
Code: ags

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


Still waiting for Purity of the Surf II

Gilbert

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.

Radiant

The easiest thing to do is invoke the system timer... the amount of milliseconds since midnight is, for all practical purposes, random.

Gilbert

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.

Radiant

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 :) )

edmundito

*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.
The Tween Module now supports AGS 3.6.0!

SMF spam blocked by CleanTalk