je007 10 Posted January 10, 2017 First post here on the BI Forums. Has been a great resource so far, as evidenced by the fact that I haven't had to make a single post looking for help in the four years I've been involved in Arma :P I'm working on a farming script which dynamically creates an object that a player can gather. The issue I am having is that if multiple objects are created without the player moving around, they are all created in the same place as each other. On the wiki page for createVehicle it is stated that "If the exact position is occupied, nearest empty position is used.". For whatever reason this doesn't seem to be working in this case, however the object I am using does throw the following RPT error: No geometry and no visual shape. To fix this, I wrote a script which checks if an object is within a certain radius of another object of the same classname, and if it is then to move the object. For some reason, it doesn't seem to be doing anything. I tested the code using the debug console and it worked perfectly fine, however when I implement it into the mission file it doesn't seem to work. No errors thrown regarding this script from the RPT. There is more code in the SQF, but this is the only segment which is giving me issues. plantCrop.sqf _plantObject = "classname"; //Create the plant _plant = _plantObject createVehicle (getpos player); _plant setVariable ["plantReady", false, true]; //Check if plant is too close to another plant _nearestCrop = nearestObject [(getpos _plant), _plantObject]; _distance = (getpos _plant) distance (getpos _nearestCrop); if (_distance <= 2) then { _random = selectRandom [1,2]; if (_random == 1) then { _plant setPos [(getPos _plant select 0) + 2, getPos _plant select 1]; } else { if (_random == 2) then { _plant setpos [getPos _plant select 0, (getPos _plant select 1) + 2]; }; }; }; Share this post Link to post Share on other sites
serena 145 Posted January 10, 2017 You can inject in your code tracing instructions like systemChat or diag_log to get feedback about your script state. For example: systemChat format ["Script executing line 127, Variable _x = %1", _x]; Object moving code can be simplified to: if (_distance < 2) then { if (random 1 < 0.5) then {_plant setPos [(getPos _plant select 0) + 2, getPos _plant select 1]} else {_plant setpos [getPos _plant select 0, (getPos _plant select 1) + 2]} } }; Or even better: if (_distance < 2) then { _plant setPos (_plant getPos [0.5 + random 1, random 360])}; Share this post Link to post Share on other sites
killzone_kid 1222 Posted January 10, 2017 1 hour ago, je007 said: On the wiki page for createVehicle it is stated that "If the exact position is occupied, nearest empty position is used.". For whatever reason this doesn't seem to be working in this case, however the object I am using does throw the following RPT error: No geometry and no visual shape. Perhaps this is the reason why this is not working. Share this post Link to post Share on other sites
serena 145 Posted January 10, 2017 Also, you can change approach to object placement, based on moving or not player at this moment, and replace all code with: private _plant = "plantObjectClass" createVehicle (if (velocity player > 0) then {getPos player} else {player getPos [0.5 + random 1, random 360]}); Share this post Link to post Share on other sites
je007 10 Posted January 11, 2017 Thank you for the quick responses! I incorporated some of your suggestions to make the script simpler, however I had to make some changes in order to use the nearestObjects command (as opposed to the nearestObject command I originally used). I realized that the easiest way to search for objects of different classnames was to use nearestObjects. It isn't perfect, but it works plantCrop.sqf _nearestCrops = nearestObjects [_plant, ["put","plant","object","classnames","here"], 2]; _nearestCrop = _nearestCrops select 0; _distance = (getpos _plant) distance (getpos _nearestCrop); if (_distance <= 2 ) then { _plant setPos (_plant getPos [1.8 + random 1, random 360])}; Share this post Link to post Share on other sites
serena 145 Posted January 11, 2017 In case nearestObjetcts return empty array, nil value in _nearestCrop cause an error in second part of script. Safe variant: if ({_plant distance _x < 2} count _nearestCrops > 0) then { _plant setPos (_plant getPos [1.8 + random 1, random 360])}; Also, nearestObjects always return at least reference to newly created plant. So, you always has your condition true. Share this post Link to post Share on other sites