Jump to content
Sign in to follow this  
Bartchie

Conditions problem in SQF - HELP !

Recommended Posts

Hello everybody, I have a problem with my function. Take a look at it:

if(isServer)then
{
_list = [1,2];
_numb1 = _list select (random 1);
_numb2 = 0;
if(_numb1 == _numb2)then
	{
	player sideChat "1 machinegun will be created";
	}else{
	player sideChat "2 machineguns will be created";
	};
};

The problem is that "else" doesn't work. I return both "player sideChat..." commands. Look like _numb1 is equal to _numb2 and it's not at the same time :/

BTW the script will be used for random number of machinegunners for spawning guerilla's camp.

I eaven checked what do I return from _numb1 and it's returning 1 or 0 randomly (so randomising thing does work 100%).

Why the hell that "else" doesnt work like it should ?

Share this post


Link to post
Share on other sites

Can't you make it even more complicated? :D

Just kidding, how about this:

if (isServer) then {
_count = ceil (random 2);
player sidechat format ["%1 machinegun(s) will be created", _count];
};

Btw, what do you think would "random 1" return?

Share this post


Link to post
Share on other sites

The main target of this function is to create one or two machineguns so:

if number is 0 then create 1 machinegun (create mg1)

if number is 1 then create 2 machineguns (create... mg1, create... mg2)

One of the numbers - _numb1 should be random 0 or 1

_numb2 is static number - just 0

When condition reaches true by _numb1 and _numb2 being equaled, then we will have 1 machiegun, else, we gonna have 2 machineguns.

Not just sideChat :)

But THX for reply anyway :D

Share this post


Link to post
Share on other sites

if(isServer)then
{
_list = [1,2];
_numb = _list select (random count _list);

if(_numb == 1)then
{
	player sideChat "1 machinegun will be created";
}
       else
       {
	player sideChat "2 machineguns will be created";
};
};

I would use "switch":

if(isServer)then
{
_numb = 1 + (random 1);

_numb = round _numb;

switch (_numb) do
{
               case 1 :
               {
	        player sideChat "1 machinegun will be created";
               };

               case 2 :
               {
	        player sideChat "2 machinegun will be created";
               };
};
};

More easy:

if(isServer)then
{
if (floor (random 100) < 50) then
{
	player sideChat "1 machinegun will be created";
       }
       else
       {
	player sideChat "2 machinegun will be created";
};
};

Edited by SNKMAN

Share this post


Link to post
Share on other sites

Thank you sooo much, SNKMAN.

Now I realised that the problem was about _numb1.

"Switch" construction is looking much better for me.

BTW I did it about 10 minutes ago, I changed _numb2 to 2 xD

if(isServer)then
{
_list = [1,2];
_numb1 = _list select (random 1);
_numb2 = 1;
hintsilent format ["_numb1 %1 _numb2 %2",_numb1,_numb2]
if (_numb1==_numb2) 
	then {player sideChat "1 machinegun will be created"} 
		else {player sideChat "2 machineguns will be created"}
};

Share this post


Link to post
Share on other sites

Hmmm....ceil (random 2) will spit out either 1 or 2, your goal is to create either 1 or 2 machineguns.....i already thought you will not just spit out a sidechat message but i don't see the problem to use _count as definite number of machineguns created.

Share this post


Link to post
Share on other sites

Your welcome.

Yes "switch" really looks much cleaner and you can add way more conditions without looking complicated.

if(isServer)then
{
_numb = (random 5);

_numb = cail _numb;

switch (_numb) do
{
               case 1 :
               {
	        player sideChat "1 machinegun will be created";
               };

               case 2 :
               {
	        player sideChat "2 machinegun will be created";
               };

               case 3 :
               {
	        player sideChat "3 machinegun will be created";
               };

               case 4 :
               {
	        player sideChat "4 machinegun will be created";
               };

               case 5 :
               {
	        player sideChat "5 machinegun will be created";
               };
};
};

Share this post


Link to post
Share on other sites

Oh damn it. Now I know what was a problem.

Looks like it's not working in this construction:

if(SOMETHING1)then
{
COMMAND1
COMMAND2
COMMAND3
if(SOMETHING2)
	then
	{
	COMMAND4
	};
	else
	{
	COMMAND5
	};
};

And it works when it's looking like this:

if(SOMETHING1)then
{
COMMAND1
COMMAND2
COMMAND3
if(SOMETHING2)then{COMMAND4};else{COMMAND5};
};

Why the hell it should be in one line and it doesn't work another way (more clean and nice looking way) ?

Is it because I'm using this script just with "[] exec "intel\testing1.sqf"" ?

I didn't used preprocess or precompile etc. - that's why ?

Share this post


Link to post
Share on other sites

Try removing the ; in red below. I've also made some indenting changes to make it look "cleaner"

if(SOMETHING1) then
{
COMMAND1;
COMMAND2;
COMMAND3;
if (SOMETHING2) then
{
	COMMAND4;
}[color="Red"];[/color]
else
{
	COMMAND5;
};
};

In short don't use "};" after if when you have an else, just use "}".

Share this post


Link to post
Share on other sites

@ Loyalguard How do you execute the function that you posted before ?

Because when I execute it like [] exec "function.sqf" then yours doesn't work :(

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  

×