Jump to content
lordfrith

[Question] basic building patrol script help

Recommended Posts

i'm trying to make a really simple building patrol script using doMove and an array of building pos. theory is unit will move to building pos, and do amibient anim until next do move is received.

 

the debug hints show that its selecting new pos and unit ok but never seems to complete or fail domove. unit will maybe move once then never does new Domove orders.

 

this is possibly issue with slightly buggy cargo tower building pos but i'll post function here is anyone can see anything stupid i'm doing?

 

Spoiler

LF_fnc_BuildingPatrol_A = {
    
    private ["_LFpatUnit", "_LFPatrolGrp", "_centerObject","_buildArray", "_buildingPos"];
    
    _LFpatUnit = _this select 0;
    _LFPatrolGrp = group _LFpatUnit;
    _centerObject = _this select 1;
    _buildArray = _this select 2;

    while {alive _LFpatUnit && true} do {
        //hint "murp";
                if ((count units _LFPatrolGrp) == 0) exitWith {};
        
                private [ "_buildingPos", "_oldpos"];
        
                _oldpos = getPos _LFpatUnit;
                _clrWP = [_LFPatrolGrp] call LF_fnc_clearWP;

        private ["_newpos"];
            
                _buildingPos = selectRandom _buildArray;
                _LFpatUnit doMove (_centerObject buildingPos _buildingPos);
                _LFpatUnit moveTo (_centerObject buildingPos _buildingPos);
                _LFpatUnit setCombatMode "RED";
                _LFpatUnit setBehaviour "SAFE";
                _LFpatUnit setSpeedMode "LIMITED";
                _sleep = 10 + (random 120);
                LFwait = false;
                _wait = [_sleep] spawn LF_fnc_sleep;

    
                waitUntil {moveToCompleted _LFpatUnit or moveToFailed _LFpatUnit or LFwait};
                doStop _LFpatUnit;
                hint format ["%1, %2,%3, %4", _LFpatUnit, _buildingPos, moveToCompleted _LFpatUnit, moveToFailed _LFpatUnit];
                [_LFpatUnit,"WATCH2","FULL",{(_this knowsAbout player) > 10}] call BIS_fnc_ambientAnimCombat;
                _sleep = 10 + (random 30);
                LFwait = false;
                _wait = [_sleep] spawn LF_fnc_sleep;
                waitUntil {(LFwait)};    
                _LFpatUnit call BIS_fnc_ambientAnim__terminate;
        };
};

 

Share this post


Link to post
Share on other sites

You're free to use mine if you want, it's open source https://github.com/AZCoder/AZC-LIB/tree/master/patrol

Written for SP so no idea if it works in MP. You can dice it, slice it, take what you want, refactor, works very well. Units will either patrol one building or all buildings within a range and stop and stand around at each point in the building.

[there are a few inter-dependencies listed at top of main script, just warning you will need the whole lib to test it, but you can still take what you want from the scripts]

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

@AZCoder thank you, i'm having a look at them just now, even if i don't use them its always helpful to see how else it can be done :D

 

i got it working adapting SpinFun's building patrol script from his AI script pack, its looks like this:

Spoiler

///////house patrol script adapted from SpinFun's Ai script pack
//
//*infantry units patrols inside nearest building
//*Syntax: nul = [_unit, _building, _bPoss] spawn LF_fnc_BuildingPatrol_A;

private ["_unit","_newPos","_bPoss","_building","_buildingPos"];

_unit = _this select 0;
_building = _this select 1;
_bPoss = _this select 2;
while { alive _unit }do{
    if(isNull(_unit findNearestEnemy _unit))then{
        _unit forceSpeed 1;
        _unit setBehaviour "SAFE";
    };
        while{true}do{
            LFwait = false;
            _buildingPos = selectRandom _bPoss;
            _newPos = _building buildingPos _buildingPos;
            waitUntil {unitReady _unit || _unit distance _newPos < 2};
            hint format ["%1, %2", _unit, _buildingPos];
            _unit doMove (_building buildingPos _buildingPos);
            _unit forceSpeed 1;
            _unit setBehaviour "SAFE";
            if(!isNull(_unit findNearestEnemy _unit) or !alive _unit)exitWith{hint format ["%1 patrol ended", _unit];};
            waitUntil {unitReady _unit || _unit distance _newPos < 2};
            [_unit,"WATCH2","FULL",{(LFwait)}] call BIS_fnc_ambientAnimCombat;
    private _sleep = 1 + (random 10);
            
    private _wait = [_sleep] spawn LF_fnc_sleep;
            waitUntil {(LFwait)};    
            _unit call BIS_fnc_ambientAnim__terminate;
            if(!isNull(_unit findNearestEnemy _unit) or !alive _unit)exitWith{hint format ["%1 patrol ended", _unit];};
        };
    if(!isNull(_unit findNearestEnemy _unit))exitWith{};
    };
};

 

i just wish i could see what was going wrong in my original example, if i can't figure out whats breaking i don't learn anything!

Share this post


Link to post
Share on other sites

Do you have showScriptErrors checked in the launcher? I don't know if you have an error, but I sure get lots of them when writing code.....

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

×