Author Topic: How do you randomise a timer?  (Read 415 times)

Legacy_Mr. Versipellis

  • Full Member
  • ***
  • Posts: 236
  • Karma: +0/-0
How do you randomise a timer?
« on: June 24, 2012, 02:02:03 pm »


                I'm having a total mind blank here - I want some NPC marker sellers to shout out random phrases, which is simple enough to do - I've got that sorted. What I'm having difficulty with getting them to repeat the action, but first waiting a random number of seconds - I don't want it to seem too robotic. Like, I'd have an NPC say a phrase, then wait a random number of seconds within a certain parameter (let's say 30 seconds + the roll of 1d20), then say another phrase, which would be chosen by a d10 roll, for example. I'm sure I've seen this done before - could anyone offer any guidance on how ?
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
How do you randomise a timer?
« Reply #1 on: June 24, 2012, 02:46:34 pm »


               If you're calling that behaviour from its heartbeat script, set a variable each time the NPC says something, and have the variable unset (or deleted) on a delay.  Something like:

if (!GetLocalInt  (OBJECT_SELF, "AMBIENT_SHOUT") )
 {
   SetLocalInt (OBJECT_SELF, "AMBIENT_SHOUT", TRUE);
   DelayCommand (30.0+ IntToFloat (d20() ), DeleteLocalInt (OBJECT_SELF, "AMBIENT_SHOUT") );
  // Do talking here;
  }
               
               

               
            

Legacy_Mr. Versipellis

  • Full Member
  • ***
  • Posts: 236
  • Karma: +0/-0
How do you randomise a timer?
« Reply #2 on: June 24, 2012, 05:21:55 pm »


               

Failed.Bard wrote...

 If you're calling that behaviour from its heartbeat script, set a variable each time the NPC says something, and have the variable unset (or deleted) on a delay.  Something like:

if (!GetLocalInt  (OBJECT_SELF, "AMBIENT_SHOUT") )
 {
   SetLocalInt (OBJECT_SELF, "AMBIENT_SHOUT", TRUE);
   DelayCommand (30.0+ IntToFloat (d20() ), DeleteLocalInt (OBJECT_SELF, "AMBIENT_SHOUT") );
  // Do talking here;
  }

Is that a good idea, though? I tend to avoid Heartbeat scripts so as not to lag up the game; this was a spawn script. Thanks, though!
               
               

               
            

Legacy_Krevett

  • Full Member
  • ***
  • Posts: 203
  • Karma: +0/-0
How do you randomise a timer?
« Reply #3 on: June 24, 2012, 05:45:22 pm »


               If you don't want to use heartbeat tou can create a custom function for the npc to speak its random line and at the end of the function call it again with a DelayCommand(put random timer her, your function);
That's how I work in my module to check for tiredness every minute or reputation, alccolization etc...
               
               

               
            

Legacy_Mr. Versipellis

  • Full Member
  • ***
  • Posts: 236
  • Karma: +0/-0
How do you randomise a timer?
« Reply #4 on: June 25, 2012, 06:46:23 pm »


               

Krevett wrote...

If you don't want to use heartbeat tou can create a custom function for the npc to speak its random line and at the end of the function call it again with a DelayCommand(put random timer her, your function);
That's how I work in my module to check for tiredness every minute or reputation, alccolization etc...

But how would I randomise the time taken?
               
               

               
            

Legacy_Krevett

  • Full Member
  • ***
  • Posts: 203
  • Karma: +0/-0
How do you randomise a timer?
« Reply #5 on: June 25, 2012, 07:24:07 pm »


               void DoSpeak()
{
float fRand = IntToFloat(Random(10) + 11); // gives random timer between 11.0 and 20.0 seconds for example

....do your speaking here....

Delaycommand(fRand, DoSpeak());
}

not tested but that should work...you just need to adjust the random timer as it fits you '<img'>
               
               

               
            

Legacy_the.gray.fox

  • Full Member
  • ***
  • Posts: 214
  • Karma: +0/-0
How do you randomise a timer?
« Reply #6 on: June 26, 2012, 06:53:34 pm »


               Hello.

I would like to point out that the Random() core function of NWScript is not as random as you would expect. The Lexicon explains it quite well.
Given the same unmodified Area, the same number of Players in the module, and the same number of calls to Random() made *before* a call such as:


float fRand = IntToFloat(Random(10) + 11);

and the above code will return always the same random amount.

You can not directly change the seed, but you can -at least- do something to force the random sequence being played to look like something different everytime.
It is sufficient to use a numeric value you really have no control over, such as the current time milliseconds. Then use it in some creative way to force an unpredictable number of calls to Random(). For example:


int nMsec = GetTimeMillisecond () & 0x3F;
while (nMsec--) { Random (1); }

The ideal moment to execute the above snippet is when a PC enters the Area. Everytime a PC enters, the random sequence for that Area shall be offsetted to something that nobody can promptly recognize. And the illusion of true randomness is achieved.


-fox
               
               

               


                     Modifié par the.gray.fox, 26 juin 2012 - 06:00 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How do you randomise a timer?
« Reply #7 on: June 26, 2012, 09:56:40 pm »


               

the.gray.fox wrote... Per. Lexicon.

Given the same unmodified Area, the same number of Players in the module, and the same number of calls to Random() made *before* a call such as:


float fRand = IntToFloat(Random(10) + 11);

and the above code will return always the same random amount.



The Lexicon is not often wrong.   But it is in this case.    The above statment is false.

I tested it in  single player, New module, One area,  Only A single placeable with a single script to report the random number OnOpen,  All module events removed.  

Every time I started a new game, fully shutting down NWN between tries, The chest reported a new string of random numbers. 

It would appear that NWN reseeds with at least every restart.  

Or did I miss something in my testing? 

L8       
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
How do you randomise a timer?
« Reply #8 on: June 26, 2012, 10:37:21 pm »


               Lf8: I remember seeing the behavior that lexicon described in past in single player. Maybe it was changed by some patch, cos I also didnt see this behavior to happen lately. Never had problem with random in multiplayer module, the values were always random. Maybe not *truly* random from proffesional programming experiences, but random enough in all my scripts where I used random.
               
               

               
            

Legacy_the.gray.fox

  • Full Member
  • ***
  • Posts: 214
  • Karma: +0/-0
How do you randomise a timer?
« Reply #9 on: June 26, 2012, 11:15:21 pm »


               I must say, Lightfoot8, Good thing you are around.
It appears I had bad knowledge on this. And yet I am sure I had seen it with my own eyes. Probably dreamed.

Anyway, you are correct.
And I see that the random sequence also changes if you switch between 2 areas, no need to fully shut the game.


I wonder... is this an accident or a signal? Am I to conclude that I am getting -hhhh!- senile? gray not without reason, but... this soon? It is worrying that I fail on tiny details such as this. Should it be more pills or more fish? A good neurologist, perhaps.

They say you can measure your intellectual "age" (maybe "freshness" is a better word) judging by the games you can complete. On the solid and worldwide accepted assumption that an 8 years old kid is virtually able to complete any game with an arm tied on his back and ask for second, I shall embark on a journey to complete the first Quake game on Nightmare difficulty. The globally agreed on parameters (for Quake 1) are that an 8yo kid can conquer it all in just 3 days, dispersing 1 quarter of gallon of sweat per game level on his worst day. I will use these values for comparison, and eventually determine what my true brain age is.
Wish me good luck.


-fox
               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
How do you randomise a timer?
« Reply #10 on: June 27, 2012, 09:52:49 pm »


               I brought up random numbers before, and a number of posters were convinced that there was a prior pattern to the seed value.  Probably the people that could best tell which patch this was changed in would be those producing NWN machinimas as this would break consistencies in standard fights so that you could not have a reproducible set of swings, hits, and misses.
               
               

               
            

Legacy_Krevett

  • Full Member
  • ***
  • Posts: 203
  • Karma: +0/-0
How do you randomise a timer?
« Reply #11 on: June 29, 2012, 09:03:48 pm »


               Is Random(0) a valid expression? Is this the same as Random(1) and will return 0? Thanks