Jump to content
JulesMK2

Can't get if condition to fire

Recommended Posts

I am sure this has come up quite a bit but the examples I am finding aren't really helping much.

The task I am trying to complete is when a player triggers the addAction, it changes the value of a local variable to true and then fires a hint if the value is true.
For some reason, I just cannot get the hint inside the if to fire. I have tried changing the value of "activefob" from 0 to 1 and also currently false to true but to no avail. Is anybody able to lend a hand or tell me where I am going wrong? Much appreciated!

 

_spawnPoleFnc = {
	//These comments are just for my own methodical thinking
	_pole = "Flag_UK_F" createVehicle getMarkerPos "marker_5"; //Spawns Flag
	_pole setVariable ["activefob", false]; //Sets variable to flag
	_pole addAction["Activate FOB",
	{
		[west, "HQ"] sideChat "FOB Activated!";
		(_this select 0) setVariable ["activefob", true]; //Changes variable value
	}];

	if ((_pole getVariable "activefob")) then {
		hint "It works!";
	};

};

call _spawnPoleFnc;

 

Share this post


Link to post
Share on other sites

I remember having some headaches in the past with setVariable / getVariable.

 

I looked over some old code I created that uses getVariable / setVariable.

For some reason that I cannot recall, I went with -1 for false and 1 for true.

 

I am unable to test right now but I wonder if the following would work out for you:

//	Set "activefob" to 1.
//	The true makes variable global and persistent.
_pole setVariable ["activefob", 1, true];

if ((_pole getVariable ["activefob", -1]) != -1) then
{
	hint "It works!";
}
else
{
	hint "Kill Maff!";
};

OR

//	Set "activefob" to true.
//	The SECOND true makes variable global and persistent.
_pole setVariable ["activefob", true, true];

if (_pole getVariable "activefob") then
{
	hint "It works!";
}
else
{
	hint "Kill Maff!";
};

 

Hopefully that works and is of use to you.

 

As I look at this I am transported back to Day 1 and I'm confused as ever. 🤣
 

  • Like 1

Share this post


Link to post
Share on other sites

Haha I will test it shortly and let you know! Thanks for your input and this can indeed be confusing at the best of times

Share this post


Link to post
Share on other sites

So I have tried both examples and I am getting the same output as last time. It will send the message in side chat (FOB Activated!), however nothing after that seems to get executed or the value is never set. here is my updated code after following your snippet (0 = false, 1 = true).
 

_spawnPoleFnc = {
	
	_pole = "Flag_UK_F" createVehicle getMarkerPos "marker_5"; //Spawns Flag
	_pole setVariable ["activefob", 0, true]; //Sets variable to flag
	_pole addAction["Activate FOB",
	{
		[west, "HQ"] sideChat "FOB Activated!";
		(_this select 0) setVariable ["activefob", 1, true]; //Changes variable value
	}];

	if ((_pole getVariable ["activefob", 0]) != 0) then {
		hint "It works!";
	};

};

call _spawnPoleFnc;

unknown.png

Share this post


Link to post
Share on other sites
11 hours ago, JulesMK2 said:

The task I am trying to complete is when a player triggers the addAction, it changes the value of a local variable to true and then fires a hint if the value is true.
For some reason, I just cannot get the hint inside the if to fire.

 

Move the if-then inside the addAction code.

 

  • Like 1

Share this post


Link to post
Share on other sites

I missed it the first time around.

A few beers later and I see it... I think.

 

The first setVariable sets "activefob" to 0, or false.

You then add the action "Activate FOB", when that has been activated the "activefob" becomes 1, or true.

 

But... You are checking "activefob" before _pole can be interacted with by a player.

It will always return 0, or false because no one has completed the "Activate FOB" action.

 

Did my previous examples display "Kill Maff"?

 

You can also test the getVariable in the debug console.

 

 

I still can't get to Arma to have a play around.

  • Like 1

Share this post


Link to post
Share on other sites

Ah yes, I completely forgot Sequential Programming existed 😂
I suppose I can always just turn the if check into a while and at the end, set the variable back to 0 to prevent a loop.

EDIT: It does seem like the problem is setting the value to one as @opusfmspol mentioned. The false block in your example @Maff does fire.

EDIT 2: Moving the if/else into the addAction worked! I didn't properly read what @opusfmspol had suggested. Thank you very much for the help, you two!

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

×