Jump to content
Sign in to follow this  
Janat

How can I get this script working? (SQF)

Recommended Posts

What this script should do: randomly make either tt7 or tt8 "true", and show if tt7 or tt8 is "true".

What this script does: shows that tt7 and tt8 are "any". Which is fine, but it doesn't change either of them to "true". If I change tt8 to "true" via a trigger, the script properly shows it's "true".

_arrayy = [0,1,2,3,4] select (random 4);

if {(_arrayy select (random 4)) > 2} then {tt8 = true;} else {tt7 = true;};
titleCut [format["All objectives complete\nCall Dow Jones\nRandom number is %1 and tt8 %2 and tt7 %3", _arrayy, tt8, tt7], "PLAIN"];
exit;

Any and all help appreciated :)

Share this post


Link to post
Share on other sites

_arrayy = [0,1,2,3,4] select (random 4);

"random 4" will produce a random float number between 0 and 4, so it could be 0.345 or 3.1 etc. You have to access array elements with integer numbers though (0, 1, 2, 3 etc.)

So you have to round or floor the result of "random 4" to an integer value.

Don't know if there are official functions to do that but a quick search in the biki or ofpec.com editor's depot should bring something up.

You could also use the randomselect function.

As soon as the first line in your script works you will already select a random element so you don't need to select it again in the if...then statement, (_array > 2) should be enough...

Share this post


Link to post
Share on other sites

Selecting the array twice would be stupid, so I'll use just _arrayy > 2. But I don't think the issue is in selecting a number from array. I tried select 3 which is clearly a round number and 3 > 2 but still I only get "any" values for tt7 and tt8.

Share this post


Link to post
Share on other sites

Why are are selecting number from array anyway. Just do:

if (random 1 < 0.5) then {
 tt7 = true;
} else {
 tt8 = true;
};

Share this post


Link to post
Share on other sites
Why are are selecting number from array anyway. Just do:

if (random 1 < 0.5) then {
 tt7 = true;
} else {
 tt8 = true;
};

We're selecting a number from array just because we can. Can there ever be any better reason? ;)

Anyway tried with your code too, and it changes BOTH of them to true :confused:

Tried with my code again, now it looks like this:

_arrayy = [0,1,2,3,4] select (round (random 4));

if {_arrayy > 2} then {
tt8 = true;
} else {
tt7 = true;
};
titleCut [format["All objectives complete\nCall Dow Jones\nRandom number is %1 and tt8 %2 and tt7 %3", _arrayy, tt8, tt7], "PLAIN"];
exit;

And that too changes both tt7 and tt8 to true :confused:

Edited by Janat

Share this post


Link to post
Share on other sites

What shk wrote should work and is the better way to do it. In addition you should initialise both variables with false, depending on what you want to do with them afterwards.

Still I was trying to point out basic errors in Janat's script...

Share this post


Link to post
Share on other sites

First of all, you have a basic syntax error in your if condition (use of '{' brackets) which is why neither variable is being assigned. Squint would have told you this ;-)

"random 4" will produce a random float number between 0 and 4, so it could be 0.345 or 3.1 etc. You have to access array elements with integer numbers though (0, 1, 2, 3 etc.)

Actually this isn't true- you can access array elements with floating point number using 'select'.

Share this post


Link to post
Share on other sites

Oh, so that's one reason why it isn't working. But if I don't use brackets or use () brackets, it assigns both tt7 and tt8 value true. As if I there was no 'if'.

Share this post


Link to post
Share on other sites
But if I don't use brackets or use () brackets, it assigns both tt7 and tt8 value true. As if I there was no 'if'.

Turn on -showscripterrors or look in your rpt file for errors. Not using brackets will cause a serious syntax error and it's unlikely either branch will be taken. Using the correct brackets should allow one or other (but not both) branches to be taken. Either way I'm highly sceptical that tt7 and tt8 are being set by the 'if' statement as you describe. It sounds far more likely that you have set the values earlier in the script and you haven't shown that in the fragment you've pasted here.

Share this post


Link to post
Share on other sites

I'm showing the full script, though I have plans on expanding it. I test it by running it via radio trigger. Also I'm sure it's the script that changes them to true. But anyway, I'll use showscripterrors tomorrow and do more debugging.

Share this post


Link to post
Share on other sites
_arrayy = [0,1,2,3,4] select (round (random 4));

This is incredibly redundant. In this case, it's the exact equivalent of just using:

_arrayy = round (random 4);

if {_arrayy > 2} then {
tt8 = true;
} else {
tt7 = true;
};

This if statement will not evaluate properly because of your syntax error (use of {} instead of (), which is what you need). It sets both variables to true because it is essentially ignoring the if statement.

Share this post


Link to post
Share on other sites

yeap, i dont quite understand why u want to randomly fetch a value from an array, but if u want something similar you can do this:

_arrayy = round (random 4);

switch (_array) do
{
case 1:   {code here};
case 2:   {code here};
case 3:   {code here};
case 4:   {code here};
};

Share this post


Link to post
Share on other sites
/*The following should already be set in init.sqf or similar, but not this script
tt7 = false;
tt8 = false;
*/

private ["_array_y", "_num"]; //standard practice to contain variables in the proper scope 

_array_y = [0,1,2,3,4];
_num = _array_y select floor(random(count _array_y));
if ( _num > 2 ) then { tt8 = true; } else { tt7 = true; };
// ^ an equal probability would be (random 1 > 0.6)

titleCut [format["All objectives complete\nCall Dow Jones\nRandom number is %1 and tt8 %2 and tt7 %3", _num, tt8, tt7], "PLAIN"];

//exit is not a SQF command

Share this post


Link to post
Share on other sites

Thanks for all replies. I made some changes to my script, but it still seems to ignore the if statement. Also Squint didn't show any errors. When I launch Arma 2 with -showscripterrors and ran the script, it gave this error:

'|#|};'
Error missing {

I can't see any missing brackets in my code though.

private ["_arrayy"];

_arrayy = [0,1,2,3,4] select round (random 4);

if (_arrayy > 2) then {
tt8 = true;
} else {
tt7 = true;
};
titleCut [format["All objectives complete\nCall Dow Jones\nRandom number is %1 and tt8 %2 and tt7 %3", _arrayy, tt8, tt7], "PLAIN"];

Now this is odd. The script works when I place all the stuff in if statement on one line like this:

private ["_arrayy"];

_arrayy = [0,1,2,3,4] select round (random 4);

if (_arrayy > 2) then {tt8 = true;} else {tt7 = true;};
titleCut [format["All objectives complete\nCall Dow Jones\nRandom number is %1 and tt8 %2 and tt7 %3", _arrayy, tt8, tt7], "PLAIN"];

Edited by Janat

Share this post


Link to post
Share on other sites

How could I make such a stupid mistake :rolleyes: Thanks. Now it works like a charm.

Share this post


Link to post
Share on other sites

BTW, round (random 4) will sometimes round up to 4 which is out of bounds of your array which is zero-based and only has indexes from 0-3.

Share this post


Link to post
Share on other sites
BTW, round (random 4) will sometimes round up to 4 which is out of bounds of your array which is zero-based and only has indexes from 0-3.

His array is of length 5 (0-4).

Share this post


Link to post
Share on other sites

My mistake. The way the code is written is confusing though.

If he just needs a probability, he should just use:

if (random 1 > 0.6)

If the array is representative of something more, he should not hard code the array or count and use:

floor(random(count _arrayy))

Share this post


Link to post
Share on other sites

And using round instead of floor will make the last option have less of a chance to get picked. ;)

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  

×