Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Nesting IF THEN ELSE statements?

  1. #1
    Staff Sergeant
    Join Date
    Oct 24 2008
    Location
    In a land down-under
    Posts
    319

    Nesting IF THEN ELSE statements?

    OK folks, please be nice, this is literally my first ever script!

    Basically I have an escaping general (assassination objective) who, upon BLUFOR detection, will attempt to escape in the nearest viable vehicle. This script therefore evaluates whether primary vehicle is functional, general runs to it and drives off. If vehicle disabled, goes to secondary and escapes.

    Have tested, works fine.

    BUT how do I implement the same method to a third or even fourth vehicle??? Tried nesting another if then else statement within second ELSE {..} section, and it returns an error. Obviously _vehicle3 is current unused for this reason......

    PS any comments on making this better or doing it a better way much appreciated, this may be the bulldozer method for all I know!

    Code:
    // escape_vehicle.sqf
    // November 2009 - Gnarly (zspecialunit.org)
    /*
    	Places the escaping unit into the first viable  (i.e. functional) vehicle of 3 specified.
    	Put this in the init line of a waypoint in the editor: 
    	nul = [who, vehicle1, vehicle2, vehicle3] execVM "scripts\escape_vehicle.sqf";
    */
    
    
    _who 		= _this select 0;
    _vehicle1       = _this select 1;
    _vehicle2     	= _this select 2;
    _vehicle3	= _this select 3;
    
    
    if (canmove _vehicle1 )then
    {
    	_who domove getpos _vehicle1;
    	_who assignAsDriver _vehicle1;
    	[_who] orderGetIn true;
    	hint "veh1 good";
    	
    } else {
    	_who domove getpos _vehicle2;
    	_who assignAsDriver _vehicle2;
    	[_who] orderGetIn true;
    	hint "veh2 good";
    		
    	};
    Note: accidentally posted in Scripting (addons) forum as well...

  2. #2
    Make sure you define any variable used inside a "private" statement at the top of the script otherwise they loose definition inside nests.
    Like;
    private ["_who", "_vehicle1", "_vehicle2", "_vehicle3"];

  3. #3
    Staff Sergeant
    Join Date
    Oct 24 2008
    Location
    In a land down-under
    Posts
    319
    Author of the Thread
    Related thread which is WAY over my head....


    http://forums.bistudio.com/showthrea...vehicle+escape

    ---------- Post added at 11:08 AM ---------- Previous post was at 11:01 AM ----------

    Quote Originally Posted by [APS]Gnat View Post
    Make sure you define any variable used inside a "private" statement at the top of the script otherwise they loose definition inside nests.
    Like;
    private ["_who", "_vehicle1", "_vehicle2", "_vehicle3"];
    WOW it works!!! So simple, thanks Gnat!

  4. #4
    You could make an array of the all the vehicles, then use forEach. Note that the below example will hint the editorname of the vehicle.
    Code:
    _who		= _this select 0;
    _vehicle1	= _this select 1;
    _vehicle2	= _this select 2;
    _vehicle3	= _this select 3;
    
    _vehicles = [_vehicle1,_vehicle2,_vehicle3];
    
    {
    	if (canMove _x) exitWith
    	{
    		_who doMove getPos _x;
    		_who assignAsDriver _x;
    		[_who] orderGetIn true;
    		hint format["%1 is good",_x];
    	};
    } forEach _vehicles;

  5. #5
    Staff Sergeant
    Join Date
    Oct 24 2008
    Location
    In a land down-under
    Posts
    319
    Author of the Thread
    Quote Originally Posted by Fincuan View Post
    You could make an array of the all the vehicles, then use forEach. Note that the below example will hint the editorname of the vehicle.
    Thanks muchly Fincuan, much simpler!

    However I will have to use the original script modified , as my primary vehicle will actually be a chopper with pilots standing by, so I need to be able to set the General as cargo for that particular case (as well as check whether at least one pilot is alive: chopper may be functional, but pilots have been killed hence voiding chopper as exfil option).

    Just to be painful, during testing I note that if waypoint at which script is initiated is not close to valid vehicle, and this vehicle is incapacitated whilst general moving towards it, he basically ignores all other vehicles and runs off for final distant waypoint (the one he is supposed to drive to), rather than reassessing the remaining vehicle options. How would I continuously assess current designated vehicle, and redesignate if current is !(canmove)?

    ---------- Post added at 12:30 PM ---------- Previous post was at 11:29 AM ----------

    OK penultimate version below, incorporating health checks on the two pilots. Check line is highlighted, seems to take upwards of 30 seconds for this line to be evaluated, before I'm sure there was barely a pause. I assume I've got the AI's knickers in a twist with some very inefficient looping?

    Code:
    // escape_vehicle.sqf
    // November 2009 - Gnarly (zspecialunit.org)
    /*
    	Places the escaping unit into the first viable  (i.e. functional) vehicle of 3 specified.
    	Put this in the init line of the item or waypoint in the editor: 
    	nul = [who, vehicle1, vehicle2, vehicle3] execVM "scripts\escape_vehicle.sqf";
    */
    
    private ["_who", "_vehicle1", "_vehicle2", "_vehicle3"];
    
    _who 		= _this select 0;
    _vehicle1       = _this select 1;
    _vehicle2     	= _this select 2;
    _vehicle3	= _this select 3;
    
    
    if ((canmove _vehicle1 && alive pilot1) or (canmove _vehicle1 && alive pilot2))then
    {
    	_who domove getpos _vehicle1;
    	_who assignAscargo _vehicle1;
    	[_who] orderGetIn true;
    	hint "veh1 good";
    	
    	} else {
    		if (canmove _vehicle2 )then
    		{
    		_who domove getpos _vehicle2;
    		_who assignAsDriver _vehicle2;
    		[_who] orderGetIn true;
    		hint "veh2 good";
    	
    			} else {
    			_who domove getpos _vehicle3;
    			_who assignAsDriver _vehicle3;
    			[_who] orderGetIn true;
    			hint "veh3 good";
    		
    			}
    		
    		};

  6. #6
    you could change your nested if with a switch command..

    something like this

    Code:
    switch (true) do {
    
        case ((canmove _vehicle1) && (alive pilot1 || alive pilot2)) :
        {
            _who domove getpos _vehicle1;
    	_who assignAscargo _vehicle1;
    	[_who] orderGetIn true;
    	hint "veh1 good";
        };
        case ((canmove _vehicle2) && (alive pilot1 || alive pilot2)) :
       {
            _who domove getpos _vehicle2;
    	_who assignAsDriver _vehicle2;
    	[_who] orderGetIn true;
       };
        default
        {
    	_who domove getpos _vehicle3;
    	_who assignAsDriver _vehicle3;
    	[_who] orderGetIn true;
    	hint "veh3 good";
        };
    };

    altho i have not tested this.
    but switch works by breaking out when 1st true condition found in one of the cases.

    so it can be used instead of nested if's

  7. #7

  8. #8
    Looks completely fine to me

  9. #9
    Staff Sergeant
    Join Date
    Oct 24 2008
    Location
    In a land down-under
    Posts
    319
    Author of the Thread
    Cheers, will give it a try!

  10. #10
    Quote Originally Posted by [APS]Gnat View Post
    Make sure you define any variable used inside a "private" statement at the top of the script otherwise they loose definition inside nests.
    Like;
    private ["_who", "_vehicle1", "_vehicle2", "_vehicle3"];
    Now I'm confused. I thought private statements simply made a variable local to the current scope. I didn't think it made variables persist.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •