Jump to content
Sign in to follow this  
zilfondel

script to check multiple if-then conditions

Recommended Posts

Hi, I've spent the past few days researching and trying to build some code to check whether a unit has fired its weapon near certain marker locations (AO1, AO2, AO3). My goal is to create a script that sets the units setcaptive to true/false depending on whether it has blown its cover at that location or not by firing its weapon. In order to achieve my goal, I need to check several conditions on my unit.

Is it not possible to use this type of statement?

if ((a = 1) && (b = 1)) then {
} else {
};

This is the entire script that is blowing up on me:

if (!isServer) exitwith {};
_unit = this select 0;

_AO1 = markerpos "AO1";
_AO2 = markerpos "AO2";
_AO3 = markerpos "AO3";

while {true} do {

if ((_unit distance _AO1 < 10) && (badger1firedAO1 == true)) then {
hint "You have been a bad boy in AO1";
_unit setcaptive false;

} else {

if ((_unit distance _AO2 < 10) && (badger1firedAO2 == true)) then {
hint "You have been a bad boy in AO2";
_unit setcaptive false;

} else {

if ((_unit distance _AO3 < 10) && (badger1firedAO3 == true)) then {
hint "You have been a bad boy in AO3";
_unit setcaptive false;

} else {

hint "You are safe";
_unit setcaptive true;

};};};
sleep 1;
};

Unfortunately, this throws an error when I start the mission - Error ==: Type Bool, expected Number, String, Object, Side, Group, Text, Config entry, Displaly (dialog), Control, Team member, Task, Location

blah blah blah

line 10

This is line 10:

if ((_unit distance _AO1 < 10) && (badger1firedAO1 == true)) then {

The script worked fine until I added the second condition: && (badger1firedAO1 == true).

Any ideas how to fix this so that it works correctly?

Share this post


Link to post
Share on other sites

if ((_unit distance _AO1 < 10) && badger1firedAO1) then {
};

an if statement automatically checks for boolean, so comparing it to a boolean isn't possible.

Also in your first line you forgot ==, so a == 1, not a = 1.

Share this post


Link to post
Share on other sites

Thanks for the help, but now I'm getting a different error:

...e] do {

Error &&: Type number, expected Bool,code

File C....

Profiles\...

line 10

if ((_unit distance _AO1 < 10) |#|&& badger1firedAO1) then {

...which doesn't make any sense to me, as according to the Biki, my original syntax was correct:

http://community.bistudio.com/wiki/and

Share this post


Link to post
Share on other sites

How is badger1firedAO1 defined?

make sure it is defined as false

badger1firedAO1 = false;

Share this post


Link to post
Share on other sites

A problem could be this:

if (!isServer) exitwith {};

You kick local clients out of the script, but you try to show "hint"-messages on the local clients within the same script... it couldn`t work i guess... or am i wrong?

:confused:

Share this post


Link to post
Share on other sites

Well outgoing from his error (&& number expected bool) I'd think he initialized the variable with var = 0; and not var = false;

But as you mentioned this script can't be executed server-side only and the line should be removed.

Share this post


Link to post
Share on other sites

Thanks for the help guys, it works perfectly.

I am now working on another script runs for a unit and checks to see if he has fired his weapon when he is near a marker.

Very simple script, but I can't get it to work.

Here it is:

_unit = badger1;

_AO1 = markerpos "AO1";
_AO2 = markerpos "AO2";
_AO3 = markerpos "AO3";

while {true} do {

if (badger1 distance _AO1 < 10) then {
_unit addeventhandler [“firedâ€, "badger1firedAO1 = true;"];
} else {

};
sleep 1;
};

Error message:

error invalid number in expression

---------------------

The previous script I tested successfully was this by itself:

badger1 addeventHandler ["fired", "badger1firedAO1 = true;"];

These scripts are basically identical except for the if-then. How do I get this to work properly? Do I need to add the eventhandler and THEN check to see if he is near the marker's location?

Share this post


Link to post
Share on other sites

You don't need to loop that. That's the whole point of eventHandlers :)

eventHandler code should be wrapped in braces {}, not quotation marks.

You're also using the wrong type of quotation marks. Notice the difference between “fired†and "fired". The first ones don't work.

Else in an if-statement is also not required.

badger1 addeventhandler ["fired", 
{
if (badger1 distance (markerpos "AO1") <= 10) then 
{
	badger1firedAO1 = true;
};
}];

Share this post


Link to post
Share on other sites

That worked, thanks - now I understand how eventhandlers work!

Share this post


Link to post
Share on other sites

I'm trying to execute a script on a headless client if one is present. If not, the script should run on the server. What i tried to do is:

 

_hc = false; 
if (count entities "HeadlessClient_F" > 0) then { _hc = true} else {_hc = false}; 

if (_hc == true AND !(hasInterface OR isServer)) exitWith {hint "You're not an HC"}; 
if (!isServer) exitWith {hint "You are no HC and no Server"}; 

hint "You are an HC or the Server, script will be executed";

 

What am I doing wrong?

Share this post


Link to post
Share on other sites
4 hours ago, MoaB said:

I'm trying to execute a script on a headless client if one is present. If not, the script should run on the server. What i tried to do is:

What am I doing wrong?

//run from init.sqf:
private _hc = (count entities "HeadlessClient_F" > 0);
if ( (_hc && !(hasInterface || isServer)) || (!_hc && isServer) ) then {
	//this code will be executed on HC if present or on server if not.
};

Take a look at the table of KillzoneKid: https://community.bistudio.com/wiki/hasInterface

  • Like 1

Share this post


Link to post
Share on other sites

Thank you very much, that was what I was looking for

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  

×