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.
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 = ["script_A.sqf","script_B.sqf","script_Z.sqf"];
_script = _array select (random ((count _array) - 1));
[...] execVM _script;
[/QUOTE]
('old' syntax below)
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_array = ["script_A.sqf","script_B.sqf","script_Z.sqf"]
_script = _array select (random ((count _array) - 1))
[...] exec _script
[/QUOTE]
'Random' returns real values, you can check about index rounding here (http://community.bistudio.com/wiki/Array).
This code won'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'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'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 = [X, Y] call PickRandomIntegerFunction;
_scriptToExecute = format["dummy%1.sqs", _int];
[ player ] exec _scriptToExecute;
[/QUOTE]
Note that I haven't actually tried that code, but it should work pretty well. It may not be 100% syntaxtually accurate though.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.