Jump to content
Millenium7

'IF' statement is a bloody nightmare! why doesn't it WORK!?

Recommended Posts

I'm trying to use the 'IF' statement in scripts and it flat out doesn't work. I have not been able to get it to work. If I include it in a script, the script doesn't work properly, and it seems that anything after the 'IF' statement does not get executed either

i.e.

if debug==1 then

// Doesn't work

? debug==1 :

// Doesn't work

if debug ==1 then {

};

// Doesn't work, so I tried a suggestion on here to do this...

_d = player getVariable ["debug", 0];

hint str(_d)

if (_d>0) then {

null = execvm "debug.sqf";

};

// Should work right? DOESNT WORK! argh! always returns 0

What am I doing wrong and why does it have to be so hard?

Share this post


Link to post
Share on other sites

Are you using SQF (execVM) or SQS? If the latter (and I'd recommend against it), your script needs to be line-based, so the entire IF-statement should be on one line. SQF does not suffer this limitation.

Share this post


Link to post
Share on other sites

i'm using SQF

I'm running a main .sqf file every 1 second from a trigger (if there's a faster and more reliable way, i'd like to know about it, as I'd like to be able to play a sound as frequently as 10 times a second but triggers are hopelessly unreliable at timing) and that script calls additional scripts

It doesn't seem to matter how I lay my 'IF' statement out, it never executes. And calling 'getvariable' always returns the number after the variable (in this case 0, if I change it to 5 its 5)

I can however access the variable ssuccessfully if I just put something like hint str(debug) it'll show 1. Just whenever an 'if' statement is involved everything goes to crap

Share this post


Link to post
Share on other sites

all the examples in wiki have worked for my variables in past , take not of destroyed variables destroyed when created withing the if then statement , not sure how your declaring /changing you variable

https://community.bistudio.com/wiki/if

Share this post


Link to post
Share on other sites

Also make sure to put the condition into (round) brackets. This code works in a .sqf skript:

if (debug == 1) then {
hint "Debug is 1!";
};

Hope that helps :)

Share this post


Link to post
Share on other sites

its not working

I have a trigger with Condition (repeat <= time) and on activation

null = player execvm "runscripts.sqf"; repeater = time + 1

I also make it 'beep' so I know its running. It continues to beep no problem

I placed....

if (debug==1) then {

hint "debug is 1!";

};

into the script and I never get any hint

I have another trigger (radio alpha) which sets debug = 1

No hint when I do 0-0-1

Share this post


Link to post
Share on other sites

i dont understand your work flo sorry

but in anycase isnt debug a reserved variable for the game or is that the debug your trying to return anyway ?

Share this post


Link to post
Share on other sites

this is kind of curious...

I made a trigger like yours that is triggered repeatedly and calls the script. If I then set debug = 1 I get a unspecified error (showscripterrors just reports "Error" at "if (|#|debug == 1) then {...", not describing the kind of error).

However, if I call the same script with a trigger that only triggers once, it works as expected (no error). And to make things even more funny, if I trigger the script via a trigger only once and then later activate the repeating trigger, it works for the repeating trigger too :confused:

i dont understand your work flo sorry

but in anycase isnt debug a reserved variable for the game or is that the debug your trying to return anyway ?

I renamed the variable to Bundschuh to make sure not to use a reserved variable, but the effect is the same.

Share this post


Link to post
Share on other sites

hmm well it now works for some reason. I dunno what I did I just rewrote everything seemingly the way it was.

I have noticed however if I place a single character out of line, i.e.

if (so and so) then {

{

}

else

{

};

The whole script fails because theres that extra { in the second line. Nothing gets run at all, maybe I had a typo somewhere.

But please, for the love of god BIS, I really really REALLY hope you guys plan to implement a better way of scripting than this. A GUI built in to the editor would be fantastic, preferably with a compiler that can pick up basic syntax error, and also gives you a bit more information on how to use commands than the current editor (which does almost nothing, still have to look up wiki's for hours)

Share this post


Link to post
Share on other sites

did you define

debug = 1;

anywhere?

try

hint str debug;

what does it say?

Edited by Killzone_Kid

Share this post


Link to post
Share on other sites

There are many editors with syntax highlighting available. I happen to use Ultraedit, but I believe Notepad++ has syntax highlighting definitions available. There are dedicated Arma editors and tools made by the community, although I don't think there is anything that is out for Arma 3 specifically.

Share this post


Link to post
Share on other sites

Just do

if (debug = 1) then {
hint "debug is 1!";
};

Then set

debug = 1

wherever you need it.

edit: Ignore my post, I missed a post where you seemed to get it to work. Sorry.

Share this post


Link to post
Share on other sites
hmm well it now works for some reason. I dunno what I did I just rewrote everything seemingly the way it was.

I have noticed however if I place a single character out of line, i.e.

if (so and so) then {

}

else

{

};

The problem still persist. It doesn't works!

Edited by irarock

Share this post


Link to post
Share on other sites
Just do

if (debug = 1) then {
hint "debug is 1!";
};

Then set

debug = 1

wherever you need it.

edit: Ignore my post, I missed a post where you seemed to get it to work. Sorry.

You're missing a =

= assign

== equals

Share this post


Link to post
Share on other sites

i put in a trigger

condition: this

In Activation: radiodisturb=1;

then from another trigger i call the script.sqf

the script say:

if (!(radiodisturb==1)) then {heligo=1;};

it doesn't works :(

Share this post


Link to post
Share on other sites

As I am still beginning to learn arma scripting, can someone verify this claim?

I don't care for curly brackets, never have, but I am structuring my syntax like this

if (condition == value) then {
  command;
}
else
{
  command;
};

rather than

if (condition == value) then {command;} else {command;};
or
if (condition == value) then {
  command;
} else {
  command;
};

I do this for two reasons. First is to me its more readable and second I use scite as my editor and the code folding and matching braces works much better.

So, before I go writing much more code, is there an issue of some sort with how the syntax is structured, specifically as it seems suggested when it spans multiple lines?

Share this post


Link to post
Share on other sites
As I am still beginning to learn arma scripting, can someone verify this claim?

I don't care for curly brackets, never have, but I am structuring my syntax like this

if (condition == value) then {
  command;
}
else
{
  command;
};

rather than

if (condition == value) then {command;} else {command;};
or
if (condition == value) then {
  command;
} else {
  command;
};

I do this for two reasons. First is to me its more readable and second I use scite as my editor and the code folding and matching braces works much better.

So, before I go writing much more code, is there an issue of some sort with how the syntax is structured, specifically as it seems suggested when it spans multiple lines?

Usually depends on what language you're familiar with. I'm sure the structures has some fancy words but I can't be bothered looking it up

Java, javascript etc does it the third way

void applyBrakes() {
   if (isMoving) {
       currentSpeed--;
   } else {
       System.err.println("The bicycle has already stopped!");
   } 
}

C++, C# does it the first way

bool condition = true;

if (condition)
{
   Console.WriteLine("The variable is set to true.");
}
else
{
   Console.WriteLine("The variable is set to false.");
}

The second way would be like writing a ternary operator, sort of. Only used for really small if-else

_markerColor = if (alive player) then {"ColorGreen"}else{"ColorRed"};

javascript example:

var alive = false;
var color = alive ? 'ColorGreen' : 'ColorRed';
// color is ColorRed

As for your question, no. If you're writing in .sqf and not .sqs it doesn't matter as long as you have semicolons and correct parenthesis.

---------- Post added at 07:25 PM ---------- Previous post was at 07:22 PM ----------

i put in a trigger

condition: this

In Activation: radiodisturb=1;

then from another trigger i call the script.sqf

the script say:

if (!(radiodisturb==1)) then {heligo=1;};

it doesn't works :(

You're checking if radiodisturb is NOT 1. Not if it's 1.

Edited by cuel

Share this post


Link to post
Share on other sites

yeah sorry. i didn't say all

trigger 1 where i put radiodisturb=1 is a radio command trigger so if i don't run it there's no radio disturb and an heli should come to kill me. The problem is IF doesn't works

Share this post


Link to post
Share on other sites

Thanks cuel. That is how I thought it would be. And yes, C++ is where I pulled my format from although I've always liked basic type syntaxes better.

I'll ask while this is an "if" thread, but is there no "else if" available in arma? Are you supposed to use case or switch type code blocks if you need that functionality?

ie.

if (cond) then
 do
elseif (cond) then
 do
elseif (cond) then
 do
else
 do
endif

That elseif functionality I miss quite a bit when I am checking values.

Share this post


Link to post
Share on other sites

Sort of. The syntax is just hard to read which is why switch case is better

cond1 = false;
cond2 = false;
cond3 = true;
if (cond1) then {
hint "wont happen";
}else{
if (cond2) then {
	hint "wont happen";
} else{
	if (cond3) then {
		hint "this works";
	};
};
};

cond1 = false;
cond2 = false;
cond3 = true;

switch(true) do {
case cond1: {
	hint "wont happen";
};
case cond2: {
	hint "wont happen";
};
case cond3: {
	hint "this works";
};
};

Share this post


Link to post
Share on other sites

if statements can be nested

if (a) then {b} else {if © then {d} else { if (f) then {g} };};};};}; etc

Share this post


Link to post
Share on other sites

Thats pretty much how I have been doing it. Its also why I use the C style formatting because of the code folding in scite. If I get the opening/closing brackets structured correctly scite will help to indicate which brackets match, thus reducing errors.

Its not a big deal when I code from scratch as I have a habit of creating both open/close braces/brackets/parentheses at once then filling values in, but when learning like this I am using others snippets and modifying as needed to see what happens, and thats when the formatting can be a pain even with code folding, maybe moreso because the folding gets wonky.

Thanks for the infos!

Share this post


Link to post
Share on other sites

So I'm struggling with this a little its been years since I even wrote anything... 

 

but I'm checking 2 variables for a "true" so if CONDITION1 or CONDITION2 =="true" then {do this command}

 I have it like this

 

if (CONDITION1) || (CONDITION2) then {COMMAND};

 

 

so would this work?

 

 
 
 
if (CONDITION1) then {Command}
 
else{
 
if (CONDITION2) then {Command
} else{
 
 
};
 
};

 

 

 

if either CONDITION2 or CONDITION1 changed to true?

 

Cheers Targs

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

×