Jump to content
ZaellixA

If, then, else issue [Resolved]

Recommended Posts

Hello everyone,

 

This is the first time I write here, so before I state the problem I would like to say a big thanks to all the people contributing and helping around here (I have found a lot of info and solved quite some issues of mine from pages here in the past).

 

So, my issue here is that I have a simple script (see below) using an if, then, else statement. The script is:

// Check if wounded mannequins exist
_groupIndex = allGroups findIf {
    groupId _x isEqualTo "medicalTrainingWoundedGroup"; // Look for a specific group ID
  };

/*
 * At this stage _groupIndex == -1 -> This is checked
 */

// Check returned group index
if(_groupIndex isEqualTo -1) then {
  hint "Creating Group..."; // Debugging
  _woundedGroup = createGroup[west, true]; // Create a group to assign wounded mannequins
  _woundedGroup setGroupId["medicalTrainingWoundedGroup"]; // Set ID of the group to reference it in the future calls of the script
} else {
  _woundedGroup = allGroups select _groupIndex; // Use the index to get it from the list of all groups
};

I have also added a comment stating that _groupIndex is equal to -1 for your convenience.

So, the actual problem I have encountered is that the first branch of the 'if, then' statement is NEVER executed, which is really weird to me. As you can see I have already checked the value of _groupIndex variable and it seems to be '-1' indeed, so if nothing else the first branch of the 'if' statement should be executed. But, no, instead the second branch (after the 'else' statement) is executed.

 

I have also tried

_groupIndex == -1

as well as

_groupIndex isEqualTo str -1

which I believe is wrong (but tried it nevertheless) and as isEqualTo does NOT return an error I didn't get the results I wanted but didn't get an error either.

 

I am quite new to scripting (although I have some intermediate experience with programming in general) and I am not sure where my error could be, so any insight would be most welcome.

 

Thank you in advance,

 

Z.

Share this post


Link to post
Share on other sites

The code looks fine to me. Don't see why it's not working. Have you tried plastering everything with diag_log's to see everything that's going on in there?
Can also "diag_log (_groupIndex isEqualTo -1)" to see if it prints "true".

Share this post


Link to post
Share on other sites

Well, thanks for the lightning speed reply.

 

I am not familiar with "diag_log(...)" but I will give it a try.

 

If I find what the issue is, I will make sure to post here to close the post (and hopefully help people in the process 🙂 )

 

Thanks again for the help,

 

Z.

Share this post


Link to post
Share on other sites

Hey all,

 

Finally I got it to work. I didn't really change anything to this part of the code.

 

What I DID change though is that I added a "declaration/definition/initialization" (not sure which term suites the way ArmA scripting handles variables) to the variable _woundedGroup, because it was not available to the rest of the code. The line is:

_woundedGroup = grpNull;

It is possible that I missread the error and this was the problem shown to me right from the beginning. Anyway, problem's solved and now the script works perfectly.

 

Thanks again for all the help.

 

Best,

 

Achilles.

  • Like 2

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

×