Aller au contenu

Photo

How do you randomise a timer?


  • Veuillez vous connecter pour répondre
11 réponses à ce sujet

#1
Mr. Versipellis

Mr. Versipellis
  • Members
  • 206 messages
 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 ?

#2
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
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;
}

#3
Mr. Versipellis

Mr. Versipellis
  • Members
  • 206 messages

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!

#4
Krevett

Krevett
  • Members
  • 104 messages
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...

#5
Mr. Versipellis

Mr. Versipellis
  • Members
  • 206 messages

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?

#6
Krevett

Krevett
  • Members
  • 104 messages
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 ;)

#7
the.gray.fox

the.gray.fox
  • Members
  • 127 messages
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 .


#8
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

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       

#9
Shadooow

Shadooow
  • Members
  • 4 471 messages
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.

#10
the.gray.fox

the.gray.fox
  • Members
  • 127 messages
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

#11
WhiZard

WhiZard
  • Members
  • 1 204 messages
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.

#12
Krevett

Krevett
  • Members
  • 104 messages
Is Random(0) a valid expression? Is this the same as Random(1) and will return 0? Thanks