Jump to content
Sign in to follow this  
IAmMarshicus

Running Random Scripts?

Recommended Posts

I've been playing Arma since Arma 2 came out now, every-now-and-then going into the editor. Normally if I found a problem with any coding I would be able to find a solution... but I can't this time round.

Basically I have a number of .sqf scripts with all different gear for certain unit types. I would like it if I could type something into the init of a unit so the game would randomly pick a script to run, which would then give these units some random gear each time the game is started.

Is this possible? If not through the init line, is there another way? :confused:

Share this post


Link to post
Share on other sites

nul = this call {
   _scripts = ["script1.sqf", "script2.sqf", "script3.sqf"];
   _joice = _scripts select (floor(random(count _scripts)));
   _nul = [] execVM _joice;
};

This should work from the initline. I hope it's self explanatory.

Share this post


Link to post
Share on other sites

Thank you, but would you mind explaining it for me? I've had a few tries at using it in the init line and by a script, but nothing happens.

EDIT - I understand everything apart from this.

_joice = _scripts select [color="Red"][b](floor(random(count[/b][/color] _scripts)));

Edited by TheHandsy

Share this post


Link to post
Share on other sites

Giving me links to the wiki is all well and good but it doesn't always help solve why something isn't working. ...Sadly the wiki hasn't really helped me understand this script but thanks anyway. :)

Share this post


Link to post
Share on other sites

Count returns the size of the array (number of elements in the array). Random generates a random number between 0 and that number. Floor rounds it down to the nearest integer value (ex: 3.7125 becomes 3).

The end result is a random index in your array.

Share this post


Link to post
Share on other sites
Count returns the size of the array (number of elements in the array). Random generates a random number between 0 and that number. Floor rounds it down to the nearest integer value (ex: 3.7125 becomes 3).

The end result is a random index in your array.

Thanks for explaining it better for me. Just to be sure though, I don't have to type in any numbers do I?

Share this post


Link to post
Share on other sites
Giving me links to the wiki is all well and good but it doesn't always help solve why something isn't working. ...Sadly the wiki hasn't really helped me understand this script but thanks anyway. :)

Yeah, the wiki can give you the main idea of how to use a command in its most basic way and some times on intermediate level. But it lacks of more examples on different situations and specially on control structures.

Share this post


Link to post
Share on other sites

I created 03 sqf files from BIS 3D Editor with some compositions (scenary) wich I intend to load randomlly (one or another or another) at mission start (a MP mission).

The following code....

nul = this call {

_scripts = ["script1.sqf", "script2.sqf", "script3.sqf"];

_joice = _scripts select (floor(random(count _scripts)));

_nul = [] execVM _joice;

};

..used to work good as far as I remember, in SP and running from init field of something in editor.

Now, it´s still working but I got an error message (when loading -showscripterros Parameter at A2 startup):

'nul |#|= this call { _scripts = ["script1.sqf"...'

Error Generic error in expression

I´d like to:

-Fix the error message

-Make it run from Init.sqf

-Make it MP compatible

For the MP part, I think that "combining" the first code (nul = this call {

_scripts = ["script1.sqf", ....)

with this I found in other topic:

if (isServer) then

{

[] execVM "xxxx.sqf";

}

else

{};

..would work, but I spent several hours trying to implement, but I really believe I could not with my poor "SQF" skills.

However I believe it´s a relativelly easy to implement for someone with a little more knowledge.

SOS pls!!

Share this post


Link to post
Share on other sites

Might be a little off-topic, or maybe not, however...

regarding just to picking a random element of an array....

Even though the probability that this happens is very very very very small, if

"random count _array" exactly returns the number elements of the array, i.e. "count _array" (what in fact is possible), then

"_array select floor(random(count _scripts))" returns an error.

So technically, this way of picking a random element is wrong.

The best way I could come up with is:

_j = (count _array - 1) min (round random (count _array));
_element = _array select _j;

Share this post


Link to post
Share on other sites

_array select floor random count _array is the optimal way to get a random element because rounding makes the first and last element's probability only 50% and the random command never returns the given maximum number.

Share this post


Link to post
Share on other sites

Thnx [GLT]Myke

But the point is beyond that,

Basically I have m1.sqf , m2.sqf and m3.sqf,

They are mission files made with BIS3DE and I can load each one of them with:

[] execVM "m1.sqf"; just like the way you suggested and they look pretty good.

What I need is to load m1.sqf or m2.sqf or m3 sqf in the mission start and being MP compatible.

The idea is to create a militia hideout, with a warlord and a scenery around it. I made 3 versions of it (almost with the sames objects inside but displaced along the 03 different buldings). Each time you run the mission the objective is in one of the random scenery.

I know I could do using shk_move objets, but the idea is that ther is always the same warlord, but the objects and units around it can be diferent.

so actually waht I need is the syntax for runnig scripts randomlly, at mission startup and Mp compatible.

Thnx!!

Edited by Charlis

Share this post


Link to post
Share on other sites
Might be a little off-topic, or maybe not, however...

regarding just to picking a random element of an array....

Even though the probability that this happens is very very very very small, if

"random count _array" exactly returns the number elements of the array, i.e. "count _array" (what in fact is possible), then

"_array select floor(random(count _scripts))" returns an error.

So technically, this way of picking a random element is wrong.

The best way I could come up with is:

_j = (count _array - 1) min (round random (count _array));
_element = _array select _j;

Technically yes.

_iterations = _this select 0;
_c = 0;
for [{_x = 0}, {_x < (_iterations + 1)}, {_x = _x + 1}] do {
_ran = random 2;
if (_ran == (round _ran)) then {_c = _c + 1};
hintsilent format ["Iteration: %1\nNatural numbers: %2", _x, _c];
//sleep 0.001;
};

Wrote this script to test how often natural numbers are given with random command. I've run it with 500'000 iterations several times with no natural numbers ever.

So i guess the chance to hit a natural number is:

< 1 : 5'000'000

Be careful, this script easily eats up a good portion on CPU ressources. Maybe re-enabling the sleep command is a good idea. Also select desert island as test environment, lower your weapon and just stare at the sky. No kidding, the script eat up 30FPS while running.

Share this post


Link to post
Share on other sites
Myke;1824185']Be careful' date=' this script easily eats up a good portion on CPU ressources. Maybe re-enabling the sleep command is a good idea. Also select desert island as test environment, lower your weapon and just stare at the sky. No kidding, the script eat up 30FPS while running.[/quote']

Move the hintSilent out of the loop and you should getter performance.

Considering a float (32bit) has 24 bits of information giving about 16777215 different values where 2 values might correspond to a whole number the odds are against you. Worse if the value is actually a double.

Share this post


Link to post
Share on other sites

In the past i worked a lot with random numbers and i never ever encountered that random returned a natural number ever. This yesterday was just a quick test (only 5'000'000 iterations) but in the past i've tested it extensively to be near as sure it can't happen.

So my prediction, the chance of getting a natural number out of the random command is:

0.0000000000000000000000000000000001

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  

×