Kingsley1997 39 Posted July 20, 2015 Here's what my problem is and what I'm doing at the moment in my code to handle it, but there are problems with it... In the mission there are FOB's placed around the map (markers names fob1..fob10 etc). When the player clicks the "request deployment" action they will be teleported to a random FOB. The server will then spawn random zones using selectBestPlaces and create AI units at them. The server will also create a marker and a trigger with the settings that if EAST are NOT PRESENT then it will execute sfn_handleZone on the server (sfn meaning server function). sfn_handleZone then sets that zone's marker to ColorWEST and deletes the trigger. After that it will check the other zones by looping from 0 to 10 (or however many maximum zones can spawn) and if one of the zones's marker color is NOT set the WEST then it will not set the "allZonesComplete" variable to true. After that loop finishes, it will check if allZonesComplete is true, if it is it will then find a good spot for an LZ near the zone, it will then spawn a Huron helicopter at a specified marker and then set the heli to go and LOAD at that LZ position. The problem I am having is that the helicopter is spawning infinitely, spawning every few seconds. From what I see in the server's RPT log is that the sfn_handleZone is being called far too many times based on the actual number of zones. If there are only 3 zones (which there usually is) then sfn_handleZones should only be called a maximum of 3 times, surely? Anyway, I've posted my code below, not all of it is relevant and one thing to note is I am using iniDBI to store zone information and restore it. sfn_createZones.sqf /* Author: Kingsley Description: Create random zones at the given FOB marker Parameter(s): 0: OBJECT - Player to be used 1: STRING - FOB marker name (fob1..fob10) Returns: BOOLEAN (returns true) */ ["Creating Zones", false] call fn_devLog; private ["_unit", "_fob"]; _unit = _this select 0; _fob = _this select 1; _puid = getPlayerUID _unit; _isBeingLoaded = false; _result = true; _profileName = _unit getVariable["profileName", ""]; if (_profileName == "") exitWith {}; _unitFileName = format["%1_%2", _puid, (_profileName call iniDB_CRC32)]; for "_z" from 0 to WS_CFG_maximumFobCount step 1 do { scopeName "zLoop"; [" Zone Loop Started", false] call fn_devLog; _zonName = format["%1_aiZone_%2", _fob, _z]; _zonExists = ["zones", _zonName, "position", "ARRAY"] call iniDB_read; [" Zone Data Read", false] call fn_devLog; if (count _zonExists > 0) then { // Zones exist in database, proceed to load them [" Zone Data Exists", false] call fn_devLog; _zonFormattedName = ["zones", _zonName, "formattedName", "STRING"] call iniDB_read; _zonInt = ["zones", _zonName, "zoneInt", "SCALAR"] call iniDB_read; _zonFob = ["zones", _zonName, "fob", "STRING"] call iniDB_read; _zonPos = ["zones", _zonName, "position", "ARRAY"] call iniDB_read; _zonRadius = ["zones", _zonName, "radius", "SCALAR"] call iniDB_read; _zonStatus = ["zones", _zonName, "status", "SCALAR"] call iniDB_read; // 1 = EAST CONTROL 2 = WEST CONTROL _zonIntensity = ["zones", _zonName, "intensity", "SCALAR"] call iniDB_read; _zonDeployments = ["zones", _zonName, "deployments", "SCALAR"] call iniDB_read; _markerColor = "ColorEAST"; _aiCount = 0; switch (_zonStatus) do { case 1: { _markerColor = "ColorEAST"; _aiCount = _zonIntensity; }; case 2: { _markerColor = "ColorWEST"; _aiCount = 0; }; default { _markerColor = "ColorEAST"; _aiCount = _zonIntensity; }; }; // Create Marker Set _marker = createMarker [_zonName, _zonPos]; _marker setMarkerShape "ELLIPSE"; _marker setMarkerSize [_zonRadius, _zonRadius]; _marker setMarkerColor _markerColor; _marker setMarkerBrush "Solid"; // Compile AI if (_aiCount > 0) then { _newGrp = createGroup east; for "_i" from 1 to _aiCount step 1 do { _target = _zonPos; _dist = random _zonRadius; _dir = random 360; _rot = random 360; _pos = _target; _positions = [(_pos select 0) + (sin _dir) * _dist, (_pos select 1) + (cos _dir) * _dist, 0]; _newUnit = "O_soldier_F" createUnit [_positions, _newGrp, "[this] call fn_rebelLoadout"]; _newUnit setDir _rot; }; [_newGrp, leader _newGrp, _zonRadius] call BIS_fnc_taskPatrol; }; //_zonVarName = format ["FOBZoneStatus_%1", _zonName]; //missionNamespace setVariable [_zonVarName, 0, true]; _trgName = format ["%1_TRIG", _zonName]; _trg = createTrigger ["EmptyDetector", _zonPos]; sleep 1; _trg setVehicleVarName _trgName; _trgAct = format ["[['%1', %2], 'sfn_handleZone', false, false, true] call BIS_fnc_MP; ['Zone Cleared', false] call fn_devLog;", _zonName, _trgName]; _trg setTriggerArea [_zonRadius * 2, _zonRadius * 2, 0, false]; _trg setTriggerActivation ["EAST", "NOT PRESENT", false]; _trg setTriggerStatements ["this", _trgAct, ""]; //[_zonName, _zonRadius, _fob, _zonVarName] spawn fn_handleZone; _isBeingLoaded = true; } else { if (_isBeingLoaded) exitWith {}; // Zones not found in database, proceed to create fresh zones [" Zones Not Found", false] call fn_devLog; // Find Random Spot _opPos = selectBestPlaces [(getMarkerPos _fob), 2500, "houses + forest", 10, 10]; _oldPos = [(getMarkerPos _fob)]; _zonCount = 0; [" Finished Finding Places", false] call fn_devLog; for "_p" from 0 to ((count _opPos) - 1) do { [" Entering Places Loop", false] call fn_devLog; _misRadius = floor (random 300 + 50); _misIntensity = ceil (_misRadius / 15); _spot = _opPos select _p; _spot2 = _spot select 0; _allow = true; for "_op" from 0 to ((count _oldPos) - 1) do { [" Entering Old Pos Loop", false] call fn_devLog; if ((_spot2 distance (_oldPos select _op)) < 1000) then { [" Less Than 1000m", false] call fn_devLog; _allow = false; }; }; if (_allow && _zonCount < 10) then { [" Allowed And Less Than 5", false] call fn_devLog; _mrkName = format["%1_aiZone_%2", _fob, _zonCount]; ["zones", _mrkName, "owner", _unitFileName] call iniDB_write; ["zones", _mrkName, "fob", _fob] call iniDB_write; ["zones", _mrkName, "zoneInt", _zonCount] call iniDB_write; ["zones", _mrkName, "formattedName", _mrkName] call iniDB_write; ["zones", _mrkName, "position", _spot2] call iniDB_write; ["zones", _mrkName, "radius", _misRadius] call iniDB_write; ["zones", _mrkName, "status", "1"] call iniDB_write; // 1 = EAST CONTROL 2 = WEST CONTROL ["zones", _mrkName, "intensity", _misIntensity] call iniDB_write; ["zones", _mrkName, "deployments", "1"] call iniDB_write; [" Zone Written", false] call fn_devLog; // Create Marker Set _marker = createMarker [_mrkName, _spot2]; _marker setMarkerShape "ELLIPSE"; _marker setMarkerSize [_misRadius, _misRadius]; _marker setMarkerColor "ColorEAST"; _marker setMarkerBrush "Solid"; [" Marker Set Created", false] call fn_devLog; // Compile AI _newGrp = createGroup east; for "_i" from 1 to _misIntensity step 1 do { _target = _spot2; _dist = random _misRadius; _dir = random 360; _rot = random 360; _pos = _target; _positions = [(_pos select 0) + (sin _dir) * _dist, (_pos select 1) + (cos _dir) * _dist, 0]; _newUnit = "O_soldier_F" createUnit [_positions, _newGrp, "[this] call fn_rebelLoadout"]; _newUnit setDir _rot; }; [_newGrp, leader _newGrp, _misRadius] call BIS_fnc_taskPatrol; [" AI Spawned", false] call fn_devLog; //_zonVarName = format ["FOBZoneStatus_%1", _mrkName]; //missionNamespace setVariable [_zonVarName, 0, true]; _trgName = format ["%1_TRIG", _mrkName]; _trg = createTrigger ["EmptyDetector", _spot2]; sleep 1; _trg setVehicleVarName _trgName; _trgAct = format ["[['%1', %2], 'sfn_handleZone', false, false, true] call BIS_fnc_MP; ['Zone Cleared', false] call fn_devLog;", _mrkName, _trgName]; _trg setTriggerArea [_misRadius * 2, _misRadius * 2, 0, false]; _trg setTriggerActivation ["EAST", "NOT PRESENT", false]; _trg setTriggerStatements ["this", _trgAct, ""]; [" Trigger Created", false] call fn_devLog; // [_mrkName, _misRadius, _fob, _zonVarName] spawn fn_handleZone; [" New Zone Created", false] call fn_devLog; _oldPos = _oldPos + [_spot2]; _zonCount = _zonCount + 1; }; }; }; }; _result sfn_handleZone.sqf private ["_zonName", "_trigVarName", "_hasFinished"]; _zonName = _this select 0; _trigVarName = _this select 1; _hasFinished = false; scopeName "mainHandleZone"; if (_hasFinished) exitWith {}; // Load zone helpers _zonFormattedName = ["zones", _zonName, "formattedName", "STRING"] call iniDB_read; _zonInt = ["zones", _zonName, "zoneInt", "SCALAR"] call iniDB_read; _zonFob = ["zones", _zonName, "fob", "STRING"] call iniDB_read; deleteVehicle _trigVarName; ["Deleted Trigger", false] call fn_devLog; ["Zone End", false] call fn_devLog; _zonFormattedName setMarkerColor "ColorWEST"; ["zones", _zonFormattedName, "status", "2"] call iniDB_write; // 1 = EAST CONTROL 2 = WEST CONTROL ["Starting Zone Loop", false] call fn_devLog; _allZonesComplete = true; for "_z" from 0 to 10 do { scopeName "zEndLoop"; _mrkName = format["%1_aiZone_%2", _zonFob, _z]; [_mrkName, false] call fn_devLog; if (getMarkerColor _mrkName == "") then { // Marker doesn't exist, gone too far _hasFinished = true; ["Marker doesn't exist, gone too far", false] call fn_devLog; breakTo "mainHandleZone"; } else { if (getMarkerColor _mrkName != "ColorWEST") then { ["Not equal to WEST", false] call fn_devLog; _allZonesComplete = false; }; }; }; //if (_misAllZonesComplete) exitWith {}; if (_allZonesComplete) then { _hasFinished = true; //missionNamespace setVariable [_zonVarName, true, true]; ["All Zones Complete", false] call fn_devLog; // Tour completed { if (isPlayer _x) then { // Start end tour process for this player (_x) if (local _x) then { hint "Tour Complete. Well done!"; }; }; } forEach playableUnits; // Start end tour process for this FOB (_zonFob) _randLZ = (getMarkerPos _zonFob) findEmptyPosition [0, 2000, "B_Heli_Transport_03_F"]; if (count _randLZ > 0) then { ["Found LZ position", false] call fn_devLog; // [position,direction,type,side or group] call BIS_fnc_spawnVehicle _hPad = "Land_HelipadEmpty_F" createVehicle _randLZ; _heli = [(getMarkerPos "heli_spawn"), 45, "B_Heli_Transport_03_F", west] call BIS_fnc_spawnVehicle; // Debug player moveInCargo _heli select 0; _heli select 0 call { _heli select 0 allowDamage false; }; _heliGrp = _heli select 2; _wp = _heliGrp addWaypoint [_randLZ, 0]; _wp setWaypointBehaviour "CARELESS"; _wp setWaypointCombatMode "RED"; _wp setWaypointCompletionRadius 0; _wp setWaypointFormation "NO CHANGE"; _wp setWaypointSpeed "FULL"; _wp setwaypointType "LOAD"; _wpMrkName = format ["%1_LZ", _zonFob]; _marker = createMarker [_wpMrkName, _randLZ]; _marker setMarkerShape "ICON"; _marker setMarkerSize [1, 1]; _marker setMarkerColor "ColorWEST"; _marker setMarkerType "hd_pickup"; } else { // Can't find LZ position? ["Can't find LZ position", false] call fn_devLog; }; }; If you have any suggestions on the logic of my mission, even if it involves me re-writing all of it, please let me know! This is a mission I'm making for the public as a persistent military simulator with different deployments, player progression etc. so hopefully when it all works, you can play it, not just me :D Share this post Link to post Share on other sites
austin_medic 109 Posted July 20, 2015 have you tried using a hint and a number variable that counts up to see how many times its running? It could point you to the problem (where is it being executed thats making it happen too many times?). Share this post Link to post Share on other sites
Kingsley1997 39 Posted July 20, 2015 have you tried using a hint and a number variable that counts up to see how many times its running? It could point you to the problem (where is it being executed thats making it happen too many times?). Yeah I've got my function fn_devLog running all over the place which will print out the given text in side chat and all I see is a bunch of messages executing way more than they need to. I've got a feeling that it's creating multiple triggers and so the trigger activation runs multiple times. Although I can't see how it would create multiple triggers because that code isn't even in a dodgy loop... Share this post Link to post Share on other sites