R4IDER 10 Posted December 22, 2013 Hey, I am having a hard time trying to work out the best way of optimizing a script. In order to increase the server FPS I decided that I would only spawn units when their required, I know there are scripts out there that do this but I wanted to put together my own version just so I could learn how to do various things. I have one file with all my CreateUnit lines which are contained in various IF statements. The way it works is I will have a variable set to area1 = 0; when a player runs into a trigger zone this variable gets changed to area1 = 1; and I have a another trigger with condition (area1 == 1) and act is to run the script file. This runs perfectly or so I thought, the problem I have is when there are lots of players connecting to the server at the start of the game, for whatever reason it the load up screen takes a lot longer and clicking continue on the map screen takes up to 30 seconds before you get into the game. With only five players its fine more than that then it just doesn't work people won't load through. I am very confused about why this is happening because I have got if (!isServer) exitWith {}; at the top of my script which from my understanding should cause the script it exit if its ran by anything but the server. The other confusing part is that the script that is used to spawn the AI isn't run at the start of the mission it is only ran when the trigger is activated. The script is over 1000 lines and from my research I have found that anything over 300 lines isn't recommended but I don't think this should apply to my script since only small portions of it get run at anyone time. if (!isServer) exitWith {}; setgear = 1; // This gets set back to 0 in enemygear.sqf if (paradrop == 1) then { paradrop = 2; ParaGroup1 = CreateGroup EAST; _soldier = ParaGroup1 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn1"),[],0,"sergeant"]; _soldier = ParaGroup1 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn1"),[],0,"private"]; _soldier = ParaGroup1 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn1"),[],0,"private"]; _soldier = ParaGroup1 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn1"),[],0,"private"]; //sleep 1; _wp = ParaGroup1 addWaypoint [[21281.3,14998.7,0.00162935], 1]; _wp = [ParaGroup1, 1] setWaypointType "MOVE"; _wp = [ParaGroup1, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup1, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup1, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup1, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup1 addWaypoint [[20468.7,14626,0.00143385], 2]; _wp = [ParaGroup1, 2] setWaypointType "MOVE"; _wp = ParaGroup1 addWaypoint [[20468.7,14626,0.00143385], 3]; _wp = [ParaGroup1, 3] setWaypointType "CYCLE"; ParaGroup2 = CreateGroup EAST; _soldier = ParaGroup2 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn2"),[],0,"sergeant"]; _soldier = ParaGroup2 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn2"),[],0,"private"]; _soldier = ParaGroup2 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn2"),[],0,"private"]; _soldier = ParaGroup2 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn2"),[],0,"private"]; //sleep 1; _wp = ParaGroup2 addWaypoint [[21080.9,14428.8,0.00140472], 1]; _wp = [ParaGroup2, 1] setWaypointType "MOVE"; _wp = [ParaGroup2, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup2, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup2, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup2, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup2 addWaypoint [[20823,15188.1,0.00181961], 2]; _wp = [ParaGroup2, 2] setWaypointType "MOVE"; _wp = ParaGroup2 addWaypoint [[20823,15188.1,0.00181961], 3]; _wp = [ParaGroup2, 3] setWaypointType "CYCLE"; ParaGroup3 = CreateGroup EAST; _soldier = ParaGroup3 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn3"),[],0,"sergeant"]; _soldier = ParaGroup3 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn3"),[],0,"private"]; _soldier = ParaGroup3 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn3"),[],0,"private"]; _soldier = ParaGroup3 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn3"),[],0,"private"]; //sleep 1; _wp = ParaGroup3 addWaypoint [[20540,14936.4,0.00145864], 1]; _wp = [ParaGroup3, 1] setWaypointType "MOVE"; _wp = [ParaGroup3, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup3, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup3, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup3, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup3 addWaypoint [[21684.7,14946.9,0.00178051], 2]; _wp = [ParaGroup3, 2] setWaypointType "MOVE"; _wp = ParaGroup3 addWaypoint [[21684.7,14946.9,0.00178051], 3]; _wp = [ParaGroup3, 3] setWaypointType "CYCLE"; ParaGroup4 = CreateGroup EAST; _soldier = ParaGroup4 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn4"),[],0,"sergeant"]; _soldier = ParaGroup4 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn4"),[],0,"private"]; _soldier = ParaGroup4 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn4"),[],0,"private"]; _soldier = ParaGroup4 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawn4"),[],0,"private"]; //sleep 1; _wp = ParaGroup4 addWaypoint [[20608.9,15001,0.00160003], 1]; _wp = [ParaGroup4, 1] setWaypointType "MOVE"; _wp = [ParaGroup4, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup4, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup4, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup4, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup4 addWaypoint [[21817,14246.4,0.00151682], 2]; _wp = [ParaGroup4, 2] setWaypointType "MOVE"; _wp = ParaGroup4 addWaypoint [[21817,14246.4,0.00151682], 3]; _wp = [ParaGroup4, 3] setWaypointType "CYCLE"; ParaGroup5 = CreateGroup EAST; _soldier = ParaGroup5 createUnit ["O_SD_Soldier",(GetMarkerPos "area2"),[],0,"sergeant"]; _soldier = ParaGroup5 createUnit ["O_SD_Soldier",(GetMarkerPos "area2"),[],0,"private"]; _soldier = ParaGroup5 createUnit ["O_SD_Soldier",(GetMarkerPos "area2"),[],0,"private"]; _soldier = ParaGroup5 createUnit ["O_SD_Soldier",(GetMarkerPos "area2"),[],0,"private"]; //sleep 1; _wp = ParaGroup5 addWaypoint [[20733.6,15263.4,0.00120163], 1]; _wp = [ParaGroup5, 1] setWaypointType "MOVE"; _wp = [ParaGroup5, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup5, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup5, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup5, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup5 addWaypoint [[21039.8,14290.7,0.00143623], 2]; _wp = [ParaGroup5, 2] setWaypointType "MOVE"; _wp = ParaGroup5 addWaypoint [[21039.8,14290.7,0.00143623], 3]; _wp = [ParaGroup5, 3] setWaypointType "CYCLE"; ParaGroup6 = CreateGroup EAST; _soldier = ParaGroup6 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawnfar"),[],0,"sergeant"]; _soldier = ParaGroup6 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawnfar"),[],0,"private"]; _soldier = ParaGroup6 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawnfar"),[],0,"private"]; _soldier = ParaGroup6 createUnit ["O_SD_Soldier",(GetMarkerPos "paraspawnfar"),[],0,"private"]; //sleep 1; _wp = ParaGroup6 addWaypoint [[21738.1,14955.7,0.0014019], 1]; _wp = [ParaGroup6, 1] setWaypointType "MOVE"; _wp = [ParaGroup6, 1] setWaypointFormation "WEDGE"; _wp = [ParaGroup6, 1] setWaypointCombatMode "YELLOW"; _wp = [ParaGroup6, 1] setWaypointBehaviour "AWARE"; _wp = [ParaGroup6, 1] SetWaypointSpeed "FULL"; _wp = ParaGroup6 addWaypoint [[20453.2,14647.4,0.00152159], 2]; _wp = [ParaGroup6, 2] setWaypointType "MOVE"; _wp = ParaGroup6 addWaypoint [[20453.2,14647.4,0.00152159], 3]; _wp = [ParaGroup6, 3] setWaypointType "CYCLE"; GroupFourB_X = CreateGroup EAST; _soldier = GroupFourB_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"sergeant"]; _soldier = GroupFourB_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; _soldier = GroupFourB_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; _soldier = GroupFourB_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; //sleep 1; _wp = GroupFourB_X addWaypoint [[20740.6,13974.4,0.00147247], 1]; _wp = [GroupFourB_X, 1] setWaypointType "MOVE"; _wp = [GroupFourB_X, 1] setWaypointFormation "STAG COLUMN"; _wp = [GroupFourB_X, 1] setWaypointCombatMode "YELLOW"; _wp = [GroupFourB_X, 1] setWaypointBehaviour "SAFE"; _wp = [GroupFourB_X, 1] SetWaypointSpeed "LIMITED"; _wp = GroupFourB_X addWaypoint [[20753,13431.5,0.00145721], 2]; _wp = [GroupFourB_X, 2] setWaypointType "MOVE"; _wp = GroupFourB_X addWaypoint [[20753,13431.5,0.00145721], 3]; _wp = [GroupFourB_X, 3] setWaypointType "CYCLE"; GroupFourC_X = CreateGroup EAST; _soldier = GroupFourC_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"sergeant"]; _soldier = GroupFourC_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; _soldier = GroupFourC_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; _soldier = GroupFourC_X createUnit ["O_SD_Soldier",(GetMarkerPos "spawn1"),[],0,"private"]; //sleep 1; _wp = GroupFourC_X addWaypoint [[21279.2,14018.8,0.00141048], 1]; _wp = [GroupFourC_X, 1] setWaypointType "MOVE"; _wp = [GroupFourC_X, 1] setWaypointFormation "STAG COLUMN"; _wp = [GroupFourC_X, 1] setWaypointCombatMode "YELLOW"; _wp = [GroupFourC_X, 1] setWaypointBehaviour "SAFE"; _wp = [GroupFourC_X, 1] SetWaypointSpeed "LIMITED"; _wp = GroupFourC_X addWaypoint [[20810,13328,0.00144577], 2]; _wp = [GroupFourC_X, 2] setWaypointType "MOVE"; _wp = GroupFourC_X addWaypoint [[20810,13328,0.00144577], 3]; _wp = [GroupFourC_X, 3] setWaypointType "CYCLE"; }; Share this post Link to post Share on other sites
iceman77 18 Posted December 22, 2013 (edited) What you've posted seems fine to me. Though you could do things a bit different to shorten the length of the script. You could create the units (group) differently and store the waypoint into one function. ParaGroup1 = [getMarkerPos "paraspawn1", EAST, (configFile >> "CfgGroups" >> "East" >> "OPF_F" >> "Infantry" >> "OIA_InfTeam")] call BIS_fnc_spawnGroup; [ParaGroup1, [21281.3,14998.7,0.00162935], 1, "MOVE", "WEDGE", "YELLOW", "AWARE", "FULL"] call Raider_fnc_wayPoint; init.sqf call compile preProcessFileLineNumbers "functions.sqf"; functions.sqf Raider_fnc_wayPoint = { _group = _this select 0; _position = _this select 1; _index = _this select 2; _type = _this select 3; _formation = _this select 4; _mode = _this select 5; _behaviour = _this select 6; _speed = _this select 7; _wp = _group addWaypoint [_position, _index]; _wp = setWaypointType _type; _wp = setWaypointFormation _formation; _wp = setWaypointCombatMode _mode; _wp = setWaypointBehaviour _behaviour; _wp = SetWaypointSpeed _speed; }; Though pure code seems faster than calling a function... Anyhow, that's one way to do things a little more efficiently. If Larrow or any other like minded individual were here they could post something better / more logical even. Edited December 22, 2013 by Iceman77 Share this post Link to post Share on other sites
mikie boy 18 Posted December 22, 2013 if (!isServer) exitWith {}; this does not always do what you think its supposed to do - it was designed to exit from the current scope not the script. It can however, not intended from what i remember, exit from the script - but when this happens or when it does not im not sure. you could therefore expect that it is running this script for the client when they connect. Have you had a look to see how many units are created - make a quick script to count units and also groups. this will give you an idea as to what is happening. As iceman has indicated, the repetition of your code needs to be reduced. You should make a function (as shown by iceman) that takes the position coordinates as parameters. This will save on the repetition. if for whatever reason you have to create units individually,then stick the create unit within a for loop :) As for the code not running first time round -you must declare the global variable before hand and ensure that it is changed before this particular script is run. Share this post Link to post Share on other sites
R4IDER 10 Posted December 22, 2013 Thanks guys, I have started using functions rather than repeating the same code over and over. Share this post Link to post Share on other sites
R4IDER 10 Posted January 2, 2014 functions.sqf Raider_fnc_wayPoint = { _group = _this select 0; _position = _this select 1; _index = _this select 2; _type = _this select 3; _formation = _this select 4; _mode = _this select 5; _behaviour = _this select 6; _speed = _this select 7; _wp = _group addWaypoint [_position, _index]; _wp = setWaypointType _type; _wp = setWaypointFormation _formation; _wp = setWaypointCombatMode _mode; _wp = setWaypointBehaviour _behaviour; _wp = SetWaypointSpeed _speed; }; Using this I am running into an error which is "Error addwaypoint: Type string, expected Group". I'm displaying the group in a hint message so I know that the group name is correct. Here is what I have done so far. ["ParaGroup1", "[21279.2,14018.8,0.00141048]", "0", "MOVE", "STAG COLUMN", "YELLOW", "SAFE", "LIMITED"] call Fnc_wayPoint; Fnc_wayPoint = { _group = _this select 0; _moveto = _this select 1; _index = _this select 2; _type = _this select 3; _formation = _this select 4; _mode = _this select 5; _behaviour = _this select 6; _speed = _this select 7; hint format["[%1] %2 %3 %4 %5 %6 %7 %8", _group, _moveto, _index, _type, _formation, _mode, _behaviour, _speed]; _wp = _group addWaypoint [_moveto, _index]; }; Share this post Link to post Share on other sites
mikie boy 18 Posted January 2, 2014 This measn you are sending your function a string instead of a group - the group is not a string.. ["ParaGroup1", "[21279.2,14018.8,0.00141048]", "0", in fact , neither is the [x,y,z] array, or the numberical index - just look at what you are sending your function and see what is needed for each variable. [ParaGroup1, [21279.2,14018.8,0.00141048], 0, etc Hope that helps Share this post Link to post Share on other sites
R4IDER 10 Posted January 2, 2014 ok thanks, that makes sense well here is how I have gotten this to work for any of you that come across the same issue as me. I have created a function to spawn in a fire team. Fnc_SpawnFireTeam = { private ["_group","_marker","_soldier"]; _group = _this select 0; _marker = _this select 1; _group = CreateGroup WEST; _soldier = _group createUnit ["B_Soldier_TL_F",(GetMarkerPos _marker),[],0,"sergeant"]; _soldier = _group createUnit ["B_Soldier_TL_F",(GetMarkerPos _marker),[],0,"private"]; _soldier = _group createUnit ["B_Soldier_TL_F",(GetMarkerPos _marker),[],0,"private"]; _soldier = _group createUnit ["B_Soldier_TL_F",(GetMarkerPos _marker),[],0,"private"]; NewGroup = _group; }; I then call the function with the following line. ["ParaGroup1", "hctest1_marker"] call Fnc_SpawnFireTeam; I have then create a function for setting waypoints of the group. Fnc_wayPoint = { private ["_group","_moveto","_index","_type","_formation","_mode","_behaviour","_speed","_wp"]; _group = _this select 0; _moveto = _this select 1; _index = _this select 2; _type = _this select 3; _formation = _this select 4; _mode = _this select 5; _behaviour = _this select 6; _speed = _this select 7; _wp = NewGroup addWaypoint [_moveto, _index]; _wp = [NewGroup, _index] setWaypointType _type; _wp = [NewGroup, _index] setWaypointFormation _formation; _wp = [NewGroup, _index] setWaypointCombatMode _mode; _wp = [NewGroup, _index] setWaypointBehaviour _behaviour; _wp = [NewGroup, _index] SetWaypointSpeed _speed; }; I then call the function with the following arguments, I have left in the groupname at the start because I would like to work out a way of using that rather than a global variable. ["ParaGroup1", [22324,18195.8,0.00146294], 0, "MOVE", "STAG COLUMN", "YELLOW", "SAFE", "LIMITED"] call Fnc_wayPoint; ["ParaGroup1", [22296,18225.1,0.0014534], 1, "MOVE", "STAG COLUMN", "YELLOW", "SAFE", "LIMITED"] call Fnc_wayPoint; ["ParaGroup1", [22296,18225.1,0.0014534], 2, "CYCLE", "STAG COLUMN", "YELLOW", "SAFE", "LIMITED"] call Fnc_wayPoint; Share this post Link to post Share on other sites
Larrow 2822 Posted January 2, 2014 //["ParaGroup1", "hctest1_marker"] call Fnc_SpawnFireTeam; Fnc_SpawnFireTeam = { private ["_group","_marker","_soldier"]; _groupName = _this select 0; _marker = _this select 1; _group = CreateGroup WEST; _soldier = _group createUnit ["B_Soldier_TL_F",(getMarkerPos _marker),[],0,"sergeant"]; _soldier = _group createUnit ["B_Soldier_TL_F",(getMarkerPos _marker),[],0,"private"]; _soldier = _group createUnit ["B_Soldier_TL_F",(getMarkerPos _marker),[],0,"private"]; _soldier = _group createUnit ["B_Soldier_TL_F",(getMarkerPos _marker),[],0,"private"]; missionNamespace setVariable [_groupName, _group]; }; The missionNameSpace line creates a variable called whatever you pass as the groups name, e.g "ParaGroup1" , ParaGroup1 can now be used where ever you need to reference this group. //["ParaGroup1", [22324,18195.8,0.00146294], 0, "MOVE", "STAG COLUMN", "YELLOW", "SAFE", "LIMITED"] call Fnc_wayPoint; Fnc_wayPoint = { private ["_group","_moveto","_radius","_type","_formation","_mode","_behaviour","_speed","_wp"]; _group = missionNamespace getVariable (_this select 0); _moveto = _this select 1; _radius = _this select 2; _type = _this select 3; _formation = _this select 4; _mode = _this select 5; _behaviour = _this select 6; _speed = _this select 7; _wp = _group addWaypoint [_moveto, _radius]; _wp setWaypointType _type; _wp setWaypointFormation _formation; _wp setWaypointCombatMode _mode; _wp setWaypointBehaviour _behaviour; _wp setWaypointSpeed _speed; }; The group is retrieved from its missionNameSpace variable (global) , I left it this way so you can keep passing your string name around, you could instead just pass the variable ParaGroup1 and change the group line to _group = _this select 0. addWaypoint's 2nd parameter is the completion radius of the waypoint, not its waypoint index, If you are not injecting waypoints there is no real need for this, each new waypoint will be added to the end of the groups waypoints list. _wp will result in an array holding the groups reference and the added waypoint's index from addWaypoint so there is no need to keep specifying the [NewGroup, _index] just use _wp as the reference instead. The setWaypoint# commands have no return value. Share this post Link to post Share on other sites