PDA

View Full Version : How to exec a script based on a name from an array



SkogUlv
Dec 27 2006, 19:19
If I have an array like :

Dummy = [ "Dummy1.sqs", "Dummy2.sqs", "Dummy3.sqs" ]

And I wanna execute one of them randomly using say :

[ player ] exec "dummy?.sqs"


How do I solve this puzzle so I can execute a script, based on the name from an array.

Foobee
Dec 27 2006, 20:03
Hello, you can randomize the select in the array :

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_array = &#91;&#34;script_A.sqf&#34;,&#34;script_B.sqf&#34;,&#34;script_Z.sqf&#34;&#93;;

_script = _array select &#40;random &#40;&#40;count _array&#41; - 1&#41;&#41;;

&#91;...&#93; execVM _script;

[/QUOTE]

(&#39;old&#39; syntax below)
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_array = &#91;&#34;script_A.sqf&#34;,&#34;script_B.sqf&#34;,&#34;script_Z.sqf&#34;&#93;

_script = _array select &#40;random &#40;&#40;count _array&#41; - 1&#41;&#41;

&#91;...&#93; exec _script

[/QUOTE]

&#39;Random&#39; returns real values, you can check about index rounding here (http://community.bistudio.com/wiki/Array).
This code won&#39;t work on an empty array http://forums.bistudio.com/oldsmileys/wink_o.gif

Cheers

ColonelSandersLite
Dec 28 2006, 11:09
If you really want to do this:

[ player ] exec "dummy?.sqs"

You don&#39;t need an array to do it.

You do need a get random integer function though. You can find a couple of these on ofpec including mine in here (http://www.ofpec.com/index.php?option=com_smf&Itemid=36&topic=29008.0). I believe mine works without modification in arma, let me know if I&#39;m wrong should you try it.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_int = &#91;X, Y&#93; call PickRandomIntegerFunction;
_scriptToExecute = format&#91;&#34;dummy%1.sqs&#34;, _int&#93;;
&#91; player &#93; exec _scriptToExecute;
[/QUOTE]

Note that I haven&#39;t actually tried that code, but it should work pretty well. It may not be 100% syntaxtually accurate though.