Jump to content
Sign in to follow this  
Kydoimos

Random command in EventHandler

Recommended Posts

Hi all - should be quite a simple one this, though my scripting skills are sadly lacking and not quite up to the task! Basically, I'd like to add a "Hit" eventhandler to an object, but have a random chance of it actually firing and activating the code, each time it's hit. I'm guessing I'd need to use the 'if' command in the .sqf script - but I'm not quite sure how to use the 'random' command. So: if ( // some sort of code needed!) then {nul =[] execVM "myScript.sqf}. Any ideas? Thanks!

Share this post


Link to post
Share on other sites

I think you will have to pass into your script via the EH, and then have the script to some sort of randomized variable value, then have just one value that would leave the script at true, and execute the rest of the way:

this addEventHandler ["Hit", {nul = [] execVM "myscript.sqf}];

myscript.sqf:

_randomnumber = random 5; //if my thoughts are correct you should get a 20% chance of the script firing.

if (_randomnumber == 2) then { code........ } else exitWith {};

Share this post


Link to post
Share on other sites

Remember to use floor JShock, random will return a decimal value (or use < 1 instead of == 2)

Alternatively:

if ((random 1) < 0.2) then {};

Edited by SilentSpike

Share this post


Link to post
Share on other sites
Remember to use floor JShock, random will return a decimal value (or use < 1 instead of == 2)

Alternatively:

if ((random 1) < 0.2) then {};

Thanks Spike, I swear I might be a good scripter if I could just remember all these details....ugh....

Share this post


Link to post
Share on other sites

Lovely guys! Thanks ever so! :cool: I also see how the random command works now, brill!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×