Jump to content
Sign in to follow this  
doomanchu

Random Script

Recommended Posts

I have a few scripts that place triggers, units, objectives on markers that are placed on the map.

I'd like to make a script that will randomly select one of those other scripts. This script will be activated from a trigger.

Any ideas?

Share this post


Link to post
Share on other sites

Put script filename strings in an array and get a random one from that array.

Something like this:

scripts=["script1.sqs","script2.sqs","script3.sqs"]

[] exec (scripts select (floor random count scripts))

Share this post


Link to post
Share on other sites

.sqf and you may will get a error becouse the max. number will be 3 but the script will select 4 [0,1,2,3].

scripts=["script1.sqs","script2.sqs","script3.sqs"];

_select = (scripts select (floor random count scripts - 1) );
[] execVM _select;

Share this post


Link to post
Share on other sites
.sqf and you may will get a error becouse the max. number will be 3 but the script will select 4 [0,1,2,3].

scripts=["script1.sqs","script2.sqs","script3.sqs"];

_select = (scripts select (floor random count scripts - 1) );
[] execVM _select;

Your fix will cause the last item in the array to be ignored. The count of the array is 3 and the maximum number floored from random 3 (max=2.99999) is 2. Oh, and sqs scripts aren't executed with execVM. ;)

Edited by Celery

Share this post


Link to post
Share on other sites
Oh, and sqs scripts aren't executed with execVM. ;)

Since we're not playing OFP anymore, virtually every other code example someone will get here will be written in SQF. So giving examples in SQS, which will NOT work in virtually every other script they'd use this in, is a bit dangerous without expressly mentioning that you're using a depreciated code syntax in your example. :eek:

If you're going to give code snippets with SQS syntax, please mark it as such and include SQF conversions as well. Just to avoid confusion like this. :)

Share this post


Link to post
Share on other sites

Here we go again. How is sqs bad or deprecated? It works just as well as always and doesn't even require a semicolon after each line.

Share this post


Link to post
Share on other sites

Because when the new guy on the block starts asking questions about code in SQS format, many of doesn't even bother trying to help. I once was fairly familiar with SQS, now it's completely alien to me. If I see questions about something and example is SQS, I just move along and let others answer.

Because SQS has no support for structured expressions, making complex code look very bad?

Because SQF files are compiled, making them slightly more efficient?

Because Biki claims SQS is deprecated since Armed Assault?

Because SQS lacks advanced scripting features, which some of us take for granted. Like macros (not tested, but I expect it to not work) or multiline blocks.

and doesn't even require a semicolon after each line.

Then again, you're forced to putting everything on the same line within a block. Hardly a valid concern.

Share this post


Link to post
Share on other sites

Something like:

 
private ["_scriptNum"];
_scriptNum = floor(random 3);
switch (_scriptNum) do {
case 0:
{
   execVM "script1.sqf";
};
case 1:
{
   execVM "script1.sqf";
};
case 2:
{
   execVM "script3.sqf";
};
};

Or even this:

[color=black]private[size=2][size=2] [[/size][/size][size=2][size=2]"_scriptNum"[/size][/size][/color][size=2][size=2][color=black]];[/color][/size]
[/size][color=black][size=2][size=2]_scriptNum [/size][/size][size=2][size=2]= floor[/size][/size][size=2][size=2]([/size][/size][size=2][size=2]random[/size][/size][/color][size=2][size=2][color=black] 3);[/color][/size]
[/size][color=black][size=2][size=2]if[/size][/size][size=2][size=2] ([/size][/size][size=2][size=2]_scriptNum [/size][/size][size=2][size=2]==[/size][/size][size=2][size=2] 0) [/size][/size][size=2][size=2]then[/size][/size][/color][size=2][size=2][color=black] {[/color][/size]
[/size][color=black][size=2][size=2]    execVM[/size][/size][size=2][size=2]"script1.sqf"[/size][/size][/color][size=2][size=2][color=black];[/color][/size]
[size=2][color=black]}[/color][/size][/size][size=2][size=2][color=black]else[/color][/size][/size][size=2][size=2][color=black]{[/color][/size]
[/size][color=black][size=2][size=2]    if[/size][/size][size=2][size=2] ([/size][/size][size=2][size=2]_scriptNum [/size][/size][size=2][size=2]==[/size][/size][size=2][size=2] 1) [/size][/size][size=2][size=2]then[/size][/size][/color][size=2][size=2][color=black] {[/color][/size]
[/size][color=black][size=2][size=2]        execVM[/size][/size][size=2][size=2]"script2.sqf"[/size][/size][/color][size=2][size=2][color=black];[/color][/size]
[size=2][color=black]    }[/color][/size][/size][size=2][size=2][color=black]else[/color][/size][/size][size=2][size=2][color=black]{[/color][/size]
[/size][color=black][size=2][size=2]        execVM[/size][/size][size=2][size=2]"script3.sqf"[/size][/size][/color][size=2][color=#ff6347][size=2][color=black];[/color][/size]
[size=2][color=black]    };[/color][/size]
[size=2][color=black]};[/color][/size]
[/color][/size]

Using floor it will only return 0-2.

Edited by callihn

Share this post


Link to post
Share on other sites

Not sure what I'm doing wrong with the following script to spawn random vehicles on my makers (marker1 and marker2).

if (!isServer) exitWith {};

switch (random floor(3)) do {
case 0: {createVehicle ["ZU23_TK_EP1", [(getMarkerPos "marker1") select 0,(getMarkerPos "marker1") select 1,0], [], 3, "GROUND"];};
case 1: {createVehicle ["T72_TK_EP1", [(getMarkerPos "marker1") select 0,(getMarkerPos "marker1") select 1,0], [], 3, "GROUND"];};
case 2: {createVehicle ["ZSU_TK_EP1", [(getMarkerPos "marker1") select 0,(getMarkerPos "marker1") select 1,0], [], 3, "GROUND"];};
};

switch (random floor(3)) do {
case 0: {createVehicle ["ZU23_TK_EP1", [(getMarkerPos "marker2") select 0,(getMarkerPos "marker2") select 1,0], [], 3, "GROUND"];};
case 1: {createVehicle ["T72_TK_EP1", [(getMarkerPos "marker2") select 0,(getMarkerPos "marker2") select 1,0], [], 3, "GROUND"];};
case 2: {createVehicle ["ZSU_TK_EP1", [(getMarkerPos "marker2") select 0,(getMarkerPos "marker2") select 1,0], [], 3, "GROUND"];};
};

Share this post


Link to post
Share on other sites

createVehicle array returns an object, so make sure you have:

_object = createVehicle.. blah blah.

Also, you can simply use getMarkerPos "marker2", since your z coord is 0 anyway, no reason to split it out like you are.

Share this post


Link to post
Share on other sites

You are doing "random floor" instead of "floor random".

Share this post


Link to post
Share on other sites
You are doing "random floor" instead of "floor random".

Thanks, that worked!

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  

×