[HELP] Random Building placer

Phyrstorm

Member
I am working on a script that will on server start randomly place (from an array of options) several buildings on the map. I am currently testing with a few buildings on the southern coast of Chernarus and they themselves will spawn from the normal sqf file but when I try to take their classnames, coords, and setdir and put them into the createVehicle array I get a "Cannot Create non-ai Vehicle" error. Below is the code I have so far and below that is the section of my rpt file with the diag_log output and the create error. Any help or suggestions would be appreciated.

CODE

PHP:
/*
Random Building Spawn for Chernarus - By Phyrstorm
Usage [building,coords,dir]
Building is the Building class name
Coords are the coords from your custom mission file
direction is the setDir from your mission file
*/

private ["_building","_coords","_dir","_count","_buildarray"];

if (!isServer) exitWith {};

_building = "Land_Mil_Barracks_i";
_coords = [3363.9216, 2167.3716, 2.3841858e-006];
_dir = -132.08899;
_count = 3;
//_buildarray usage [building,coords,dir]

_buildarray = [
["""Land_Mil_Barracks_i""","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],
["""Land_Mil_Barracks_i""","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],
["""Land_Mil_Barracks_i""","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"],
["""Land_Mil_Barracks_i""","[3385.0901, 2186.7957, 6.6757202e-006]","-132.08899"],
["""Land_Mil_Barracks_i""","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],
["""Land_Mil_Barracks_i""","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]
];

for "_x" from 1 to _count do {

_randcount = floor (random (count _buildarray));
diag_log format ["%1",_randcount];
_buildrand = _buildarray select _randcount;
diag_log format ["%1",_buildrand];
_building = _buildrand select 0;
diag_log format ["%1",_building];
_coords = _buildrand select 1;
diag_log format ["%1",_coords];
_dirs = _buildrand select 2;
diag_log format ["%1",_dirs];
_dir = parseNumber _dirs;
diag_log format ["%1",_dir];
_buildarray set [_randcount,-1];
diag_log format ["%1",_buildarray];
_buildarray = _buildarray - [-1];
diag_log format ["%1",_buildarray];

_vehicle_1 = objNull;
if (true) then
{
_this = createVehicle [_building, getMarkerPos _coords, [], 0, "CAN_COLLIDE"];
_vehicle_1 = _this;
_this setDir _dir;
_this setPos getMarkerPos _coords;
};
diag_log format ["%1, %2, %3, %4, %5",_buildrand,_buildarray,_building,_coords,_dir];
diag_log text format ["%1 spawned at %2 facing %3.",_building,_coords,_dir];
};

RPT excerpt

PHP:
11:46:36 "3"
11:46:36 "[""Land_Mil_Barracks_i"","[3385.0901, 2186.7957, 6.6757202e-006]","-132.08899"]"
11:46:36 ""Land_Mil_Barracks_i""
11:46:36 "[3385.0901, 2186.7957, 6.6757202e-006]"
11:46:36 "-132.08899"
11:46:36 "-132.089"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"],-1,[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"],[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 Cannot create non-ai vehicle "Land_Mil_Barracks_i",
11:46:36 "[""Land_Mil_Barracks_i"","[3385.0901, 2186.7957, 6.6757202e-006]","-132.08899"], [[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"],[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]], "Land_Mil_Barracks_i", [3385.0901, 2186.7957, 6.6757202e-006], -132.089"
11:46:36 "Land_Mil_Barracks_i" spawned at [3385.0901, 2186.7957, 6.6757202e-006] facing -132.089.
11:46:36 "2"
11:46:36 "[""Land_Mil_Barracks_i"","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"]"
11:46:36 ""Land_Mil_Barracks_i""
11:46:36 "[3377.9146, 2180.3911, 9.5367432e-007]"
11:46:36 "-132.08899"
11:46:36 "-132.089"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],-1,[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 Cannot create non-ai vehicle "Land_Mil_Barracks_i",
11:46:36 "[""Land_Mil_Barracks_i"","[3377.9146, 2180.3911, 9.5367432e-007]","-132.08899"], [[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]], "Land_Mil_Barracks_i", [3377.9146, 2180.3911, 9.5367432e-007], -132.089"
11:46:36 "Land_Mil_Barracks_i" spawned at [3377.9146, 2180.3911, 9.5367432e-007] facing -132.089.
11:46:36 "2"
11:46:36 "[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"]"
11:46:36 ""Land_Mil_Barracks_i""
11:46:36 "[3392.9038, 2194.2275, 6.1988831e-006]"
11:46:36 "-132.08899"
11:46:36 "-132.089"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],-1,[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 "[[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]]"
11:46:36 Cannot create non-ai vehicle "Land_Mil_Barracks_i",
11:46:36 "[""Land_Mil_Barracks_i"","[3392.9038, 2194.2275, 6.1988831e-006]","-132.08899"], [[""Land_Mil_Barracks_i"","[3363.9216, 2167.3716, 2.3841858e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3370.8013, 2173.6431, 6.1988831e-006]","-132.08899"],[""Land_Mil_Barracks_i"","[3400.0671, 2200.752, 4.2915344e-006]","-132.08899"]], "Land_Mil_Barracks_i", [3392.9038, 2194.2275, 6.1988831e-006], -132.089"
11:46:36 "Land_Mil_Barracks_i" spawned at [3392.9038, 2194.2275, 6.1988831e-006] facing -132.089.
 
try using this:

Call it in your init.sqf

Code:
if (isServer) then {
    _nil = [] execVM "Path_to\spawn_building.sqf";
};
Code:
_unit_1 = objNull;
if (true) then
{
_posArray =     [
        [[3363.9216, 2167.3716, 2.3841858e-006],132.08899],
        [[3370.8013, 2173.6431, 6.1988831e-006],132.08899],
        [[3377.9146, 2180.3911, 9.5367432e-007],132.08899],
        [[3385.0901, 2186.7957, 6.6757202e-006],132.08899],
        [[3392.9038, 2194.2275, 6.1988831e-006],132.08899],
        [[3400.0671, 2200.752, 4.2915344e-006],132.08899],
        ];
_unit_1 = _this;
_randPos = _posArray call bis_fnc_selectrandom;
_pos = _randPos select 0;                                 
_dir = _randPos select 1;
_this = createVehicle ["Land_Mil_Barracks_i", [1599.43,7785.17,0], [], 0, "CAN_COLLIDE"];
_this setposatl _pos;
_this setdir _dir;


};
processInitCommands;
 
Last edited:
That didn't work at all.

Code:
17:10:54 Error in expression <, 2200.752, 4.2915344e-006],132.08899],
];
_unit_1 = _this;
_randPos = _posArray>
17:10:54   Error position: <];
_unit_1 = _this;
_randPos = _posArray>
17:10:54   Error Missing [
17:10:54 File z\addons\dayz_server\buildings\randtest.sqf, line 11
17:10:54 Error in expression <, 2200.752, 4.2915344e-006],132.08899],
];
_unit_1 = _this;
_randPos = _posArray>
17:10:54   Error position: <];
_unit_1 = _this;
_randPos = _posArray>
17:10:54   Error Missing [
17:10:54 File z\addons\dayz_server\buildings\randtest.sqf, line 11
 
whopsie last comma was to much ^^
Code:
_unit_1 = objNull;
if (true) then
{
_posArray =     [
        [[3363.9216, 2167.3716, 2.3841858e-006],132.08899],
        [[3370.8013, 2173.6431, 6.1988831e-006],132.08899],
        [[3377.9146, 2180.3911, 9.5367432e-007],132.08899],
        [[3385.0901, 2186.7957, 6.6757202e-006],132.08899],
        [[3392.9038, 2194.2275, 6.1988831e-006],132.08899],
        [[3400.0671, 2200.752, 4.2915344e-006],132.08899]
        ];
_unit_1 = _this;
_randPos = _posArray call bis_fnc_selectrandom; 
_pos = _randPos select 0;                                    
_dir = _randPos select 1;
_this = createVehicle ["Land_Mil_Barracks_i", [1599.43,7785.17,0], [], 0, "CAN_COLLIDE"];
_this setposatl _pos;
_this setdir _dir;


};
processInitCommands;
 
that code worked but the direction was not getting set. However looking at your code I realized I was over thinking the code. I had too many quotes in the array turning the coords array and direction into strings then trying to convert them. I took out the extra quotes and removed the conversions from string to array and number and now my original code works. Thank you for the help SchwEde! Here is the finished code with all the debugging output commented out. It can be removed if you choose.

PHP:
/*
    Random Building Spawn for Chernarus - By Phyrstorm
    Usage [building,coords,dir]
    Building is the Building class name
    Position are the coords from your custom mission file
    direction is the setDir from your mission file
*/

private ["_building","_position","_dir","_count","_buildarray"];

if (!isServer) exitWith {};

_building = [];
_coords = [];
_dir = [];
_count = 3;
//_buildarray usage [building,position,dir]

_buildarray = [
    ["Land_Mil_Barracks_i",[3363.9216, 2167.3716, 2.3841858e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3370.8013, 2173.6431, 6.1988831e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3377.9146, 2180.3911, 9.5367432e-007],-132.08899],
    ["Land_Mil_Barracks_i",[3385.0901, 2186.7957, 6.6757202e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3392.9038, 2194.2275, 6.1988831e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3400.0671, 2200.752, 4.2915344e-006],-132.08899]
];

for "_x" from 1 to _count do {

    _randcount = floor (random (count _buildarray));
    //diag_log format ["%1",_randcount];
    _buildrand = _buildarray select _randcount;
    //diag_log format ["%1",_buildrand];
    _building = _buildrand select 0;
    //diag_log format ["%1",_building];
    _position = _buildrand select 1;
    //diag_log format ["%1",_position];
    _dir = _buildrand select 2;
    //diag_log format ["%1",_dir];
    _buildarray set [_randcount,-1];
    //diag_log format ["%1",_buildarray];
    _buildarray = _buildarray - [-1];
    //diag_log format ["%1",_buildarray];

    _vehicle_1 = objNull;
    if (true) then
    {
        _this = createVehicle [_building, _position, [], 0, "CAN_COLLIDE"];
        _vehicle_1 = _this;
        _this setDir _dir;
        _this setPos _position;
    };

    diag_log text format ["%1 spawned at %2 facing %3.",_building,_position,_dir];
};
 
that code worked but the direction was not getting set. However looking at your code I realized I was over thinking the code. I had too many quotes in the array turning the coords array and direction into strings then trying to convert them. I took out the extra quotes and removed the conversions from string to array and number and now my original code works. Thank you for the help SchwEde! Here is the finished code with all the debugging output commented out. It can be removed if you choose.

PHP:
/*
    Random Building Spawn for Chernarus - By Phyrstorm
    Usage [building,coords,dir]
    Building is the Building class name
    Position are the coords from your custom mission file
    direction is the setDir from your mission file
*/

private ["_building","_position","_dir","_count","_buildarray"];

if (!isServer) exitWith {};

_building = [];
_coords = [];
_dir = [];
_count = 3;
//_buildarray usage [building,position,dir]

_buildarray = [
    ["Land_Mil_Barracks_i",[3363.9216, 2167.3716, 2.3841858e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3370.8013, 2173.6431, 6.1988831e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3377.9146, 2180.3911, 9.5367432e-007],-132.08899],
    ["Land_Mil_Barracks_i",[3385.0901, 2186.7957, 6.6757202e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3392.9038, 2194.2275, 6.1988831e-006],-132.08899],
    ["Land_Mil_Barracks_i",[3400.0671, 2200.752, 4.2915344e-006],-132.08899]
];

for "_x" from 1 to _count do {

    _randcount = floor (random (count _buildarray));
    //diag_log format ["%1",_randcount];
    _buildrand = _buildarray select _randcount;
    //diag_log format ["%1",_buildrand];
    _building = _buildrand select 0;
    //diag_log format ["%1",_building];
    _position = _buildrand select 1;
    //diag_log format ["%1",_position];
    _dir = _buildrand select 2;
    //diag_log format ["%1",_dir];
    _buildarray set [_randcount,-1];
    //diag_log format ["%1",_buildarray];
    _buildarray = _buildarray - [-1];
    //diag_log format ["%1",_buildarray];

    _vehicle_1 = objNull;
    if (true) then
    {
        _this = createVehicle [_building, _position, [], 0, "CAN_COLLIDE"];
        _vehicle_1 = _this;
        _this setDir _dir;
        _this setPos _position;
    };

    diag_log text format ["%1 spawned at %2 facing %3.",_building,_position,_dir];
};
you don't have to use the _vehicle_1 crap. You can just do a createVehicle array like so :
Code:
_yes = createVehicle [_building, _position, [], 0, "CAN_COLLIDE"];

https://community.bistudio.com/wiki/createVehicle_array

or even _yes = _building createVehicle _position;
 
Last edited:
Awesome that's good to know. Less code, less overhead.
Exactly. everything else about your script seems very sound though, awesome job. Keep it up bro. If you have any further questions you can always message me or Schwede or....both! We are fountains of knowledge (well...sometimes)
 
Back
Top