Jump to content
lordfrith

i seem to have seriously broken my patrol script

Recommended Posts

hi folks

i was making a scenario and rewrote an old patrol function that i had.

 

it made arma 3 crash!

 

so i tried isolating the function in a test scenario and while arma didn't crash the function did absolutely nothing.

 

i've been staring at this for over an hour now trying to see what really stupid thing i've done wrong... anyone help?

 

what the function is supposed to do:

 

if no enemies are detected move either to random position or random building position. if there are enemies present group should move to intercept. In a loop.

 

to test, create a new mission and place an AI group near any town. create an init.sqf and paste in:

 

EDITED now working:

 

Spoiler

LF_fnc_AreaPatrol_A = {

    sleep 0.1;
params ["_unit", "_centerObject", "_range"];
    
hint "stage 0";
private _LFPatrolGrp = group _unit;

        while {alive _unit} do {
                hint "stage 1";
                if(isNull(_unit findNearestEnemy _unit))then{
                hint "stage 2";
                        _numbers = [1,2,3,4,5,6,7,8];
                        _number = selectRandom _numbers;
                        _oldpos = getPos _unit;
                private _clrWP = [_LFPatrolGrp] call LF_fnc_clearWP;
                        if (_number <= 2) then {            
                        private _wpPos = [_centerObject, 10, _range, 0, 0, 40] call BIS_fnc_findSafePos;
                        private _patWp1 = _LFPatrolGrp addWaypoint [_wpPos, 20];
                                _patWp1 setWaypointType "MOVE";
                                _patWp1 setWaypointSpeed "LIMITED";
                                //_patWp1 setWaypointStatements ["false", "alive _unit"];
                                _unit setCombatMode "RED";
                                _unit setBehaviour "safe";
                                hint format ["random for %1", _unit];
                                waitUntil {(unitReady _unit)};
                        };
                        if (_number > 2) then {
                    
                                _buildArray = nearestBuilding _unit buildingPos -1;
                    
                                if (count _buildArray == 0) then {
                                //_wpPos = [getPos _centerObject, random 20, random 360] call BIS_fnc_relPos;
                                private _wpPos = [_centerObject, 10, _range, 0, 0, 40] call BIS_fnc_findSafePos;
                                private _patWp1 = _LFPatrolGrp addWaypoint [_wpPos, 20];
                                        _patWp1 setWaypointType "MOVE";
                                        _patWp1 setWaypointSpeed "LIMITED";
                                        //_patWp1 setWaypointStatements ["false", "alive _unit"];
                                        _unit setCombatMode "RED";
                                        _unit setBehaviour "safe";
                                        hint format ["building waypoint not found for %1", _unit];
                                        waitUntil {(unitReady _unit)};
                                } else {
                                        _buildingPos = selectRandom _buildArray;
                                private _patWp1 = _LFPatrolGrp addWaypoint [_buildingPos, 20];
                                        _patWp1 setWaypointType "MOVE";
                                        _patWp1 setWaypointSpeed "LIMITED";
                                        //_patWp1 setWaypointStatements ["false", "alive _unit"];
                                        _unit setCombatMode "RED";
                                        _unit setBehaviour "safe";
                                        hint format ["building waypoint set  for %1", _unit];
                                        waitUntil {(unitReady _unit)};
                                };
                        };
                } else {
                        _clrWP = [_LFPatrolGrp] call LF_fnc_clearWP;
                        _quarry = _unit findNearestEnemy _unit;
                private _wpPos = [_quarry, 10, _range, 0, 0, 40] call BIS_fnc_findSafePos;
                private  _patWp1 = _LFPatrolGrp addWaypoint [_wpPos, 0];
                        _patWp1 setWaypointType "SAD";
                        _patWp1 setWaypointSpeed "FULL";
                        //_patWp1 setWaypointStatements ["false", "alive _unit"];
                        _unit setCombatMode "RED";
                        _unit setBehaviour "AWARE";
                        hint format ["hunting waypoint set  for %1", _unit];
                        sleep 60;
                };
        };
};

 

//script spawn

 

[tester,tester,500] spawn LF_fnc_AreaPatrol_A;

 

 

 

hopefully someone can see what i'm missing here...

  • Like 1

Share this post


Link to post
Share on other sites

put some debug messages to see how far it gets.

 

example:

 

player sidechat "State 3";

 

  • Like 1

Share this post


Link to post
Share on other sites

Try adding checks in case BIS_fnc_findSafePos doesn't return anything usable (can return default map safe positions that can be mid-air etc).

Other than that replace BIS_fnc_findSafePos with a simple getPos alternate syntax.

There's no need to use the BIS function for waypoint positions, since waypoints get cancelled when they're not reachable.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, gc8 said:

put some debug messages to see how far it gets.

 

it didn't get a far as line 3 "hint 'stage 0';" (see script in spoiler bar) which is what was making me think i've done some monumental syntax fuck up... like i say it seems to fail to call the function at all

 

1 hour ago, Grumpy Old Man said:

There's no need to use the BIS function for waypoint positions, since waypoints get cancelled when they're not reachable.

 

hmm thats pretty interesting and i'll follow it up if i can get it working even a little

Edited by lordfrith

Share this post


Link to post
Share on other sites

Are you passing the right arguments to the function?

  • Like 1

Share this post


Link to post
Share on other sites

well... i think so... ;)

 

Quote

LF_ambPat = [this, this, 600] call LF_fnc_AreaPatrol_A;

 

the first 'this' should just be the group leader to give way points to, the second 'this' should be the ref object the group patrols around (for the test it was the group leader also), it was only used by BIS_fnc_findSafePos anyways, 3rd param is the range to scan for.

 

i've had to head out so i won't be able to test more till tomorrow but the problem does seem to be something is stopping it from running at all rather than a part of the function generating errors.

 

thanks for the ideas folks

Share this post


Link to post
Share on other sites

First of all you should use spawn instead call as there are loops and sleeps in your function.

Next you should prevent to spawn this function multiple times on the same unit.

Placing such execution in init field of unit is not smart idea as this will be executed multiple times in MP

  • Like 2

Share this post


Link to post
Share on other sites
6 hours ago, davidoss said:

you should use spawn instead call

cheers, i will do

6 hours ago, davidoss said:

Placing such execution in init field of unit is not smart idea as this will be executed multiple times in MP

to be clear this scenario is a quick test to find whats broken in the function, in its original scenario i was calling (spawning) this from the server for spawned groups

 

i'm gonna try rewriting that damn thing tomorrow, will post back results

 

 

  • Thanks 1

Share this post


Link to post
Share on other sites
16 hours ago, lordfrith said:

it didn't get a far as line 3 "hint 'stage 0';"

 

Well I run your script using spawn and it got to "state 2" and the unit started moving. So it appears to be working.

 

This is how I tested (in init.sqf):

 

[tester,tester,500] spawn LF_fnc_AreaPatrol_A;

 

Where tester is the guy you want to move

 

  • Like 1

Share this post


Link to post
Share on other sites

first off i edited the function  (see OP) and have got it working but the problem above still persists:

 

6 hours ago, gc8 said:

Well I run your script using spawn and it got to "state 2" and the unit started moving. So it appears to be working.

 

did you experience any lag at all on the loading screen? when i tested today i realised arma wasn't crashed (duh) it was just taking 3-5 mins to load stratis test mission.

 

heres the experiment:

 

https://www.dropbox.com/sh/b8x0o7f1hhoqw1z/AADdT6E8BIXMi1M5h-lXzBF3a?dl=0

 

run test scenario above as is, call function on tester unit from debug console. Everything works nice and fast no problem. No noticeable lag on mission load or on debug console function spawn.

 

second test, delete the // on the bottom line of init so the function is spawned from init.sqf. mission takes 3-5 mins to start..... then works fine.

 

if anyone else fails to replicate this then its not a scripting problem.

 

EDIT: oh and if anyone else can replicate this sorry in advance

 

Share this post


Link to post
Share on other sites
28 minutes ago, lordfrith said:

did you experience any lag at all on the loading screen?

 

Nope.

 

I downloaded the dropbox mission and run it, but it seems to freeze on mission start (loading screen) (Like you said). then I added sleep before the function calling and it worked, no more stuck in the loading screen.

 

code I used:

 

sleep 0.1;
[tester,tester,500] spawn LF_fnc_AreaPatrol_A;

 

  • Thanks 1

Share this post


Link to post
Share on other sites
2 minutes ago, gc8 said:

no more stuck in the loading screen.

 

YES!!! that does indeed work, thanks good sir

 

also works if i put the 'sleep 0.1; inside the function so i don't have to worry about where its spawned from, OP will be updated accordingly.

 

that was a simple fix eh, can anyone explain whats going on here so i can spot this in future? i never wrote a script that did that before!

Share this post


Link to post
Share on other sites

Hard to say where is the core of the problem, it just seems arma is not ready for those script commands to be called.

I only have got problem with sidechat command if it's called right at the start of the mission, nothing gets displayed. So I always put sleep before calling sidechat in the beginning like in init.sqf

 

 

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

×