Jump to content
Sign in to follow this  
beta

Trouble with nearestObjects command

Recommended Posts

Having some problems with this command.

This is the code I am using,

Script

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

//Written by beta

//Salvages disabled vehicles when in salvage truck

_truck = _this select 0;

//infinite loop

for [{_i=0}, {_i<1}, {_i=0}] do

{

   _objArray = nearestObjects [_truck, ["Truck5tOpen", "WarfareWestSalvageTruck", "M1Abrams", "Stryker_ICV_M2", "Stryker_ICV_MK19", "UH60", "AH1W"], 20];

   

   for [{_i = 0}, {_i < count _objArray}, {_i = _i + 1}] do

   {

       _x = _objArray select _i;

       

       if (!(canMove _x) || !(canFire _x) && damage _x < 1) then

       {

          _x setDir getDir salvage_pad;

          _x setPos getPos salvage_pad;

          _truck vehicleChat "Vehicle Salvaged.";

       };

       

       sleep 0.1;

   };

   if (vehicle player == player) exitWith {};

   sleep 5;

};

if (true) exitWith {};

Run from init in a respawn script

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

//Written by beta

// Modified from Domination by Xeno

//Respawns _vehicle after _delay seconds

if (!isServer) exitWith {};  //exit unless server

//declare variables

_vehicle = _this select 0;

_delay = _this select 1;

_startpos = getpos _vehicle;

_startdir = getdir _vehicle;

_type = typeOf _vehicle;

//init vehicle

_st = "clearMagazineCargo this; clearWeaponCargo this; this addEventhandler [""GetIn"", {[_vehicle] execVM ""scripts\salvage.sqf"";}];";

_vehicle setVehicleInit _st;

processInitCommands;

//infinite loop

for [{_i=0}, {_i<1}, {_i=0}] do

{

   if (!(alive _vehicle)) exitWith

   {

sleep 0.1;

_vehicle removeAllEventHandlers "killed";

_vehicle removeAllEventHandlers "getin";

_vehicle removeAllEventHandlers "getout";

sleep 0.5;

_vehicle = objNull;

sleep _delay;

_newveh = _type createvehicle _startpos;

_newveh setpos _startpos;

_newveh setdir _startdir;

nil = [_newveh, _delay] execVM "scripts\truckS_respawn.sqf";

};

sleep 1;

};

if (true) exitWith {};

The error

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_objArray = |#|nearestObjects [_truck, ["Truck5tOpen", "WarfareWestSalvageTruck", "M1Abrams", "Stryker_ICV_M2", "Stryker_ICV_MK19", "UH60", "AH1W"], 20];

Error 0 elements provided, 3 expected

Line 10

Not exactly sure what is wrong ..

Any ideas?

EDIT: Small error found, "_i = i + 1", fixed, problem still remains.

EDIT 2: More exact error from the .rpt file:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

File C:\downloads\ArmA\missions\wac_coop_54_op_gloria_v104.Sara\scripts\salvage.sqf, line 10

Error in expression <_i=0}, {_i<1}, {_i=0}] do

{

_objArray = nearestObjects [_truck, ["Truck5tOpen", >

 Error position: <nearestObjects [_truck, ["Truck5tOpen", >

EDIT 3:

Changing the object to be the base of the nearestObjects check to player (from _truck) removes the error. I tried using 'truck', a global variable, but the error still remains. Renaming the truck to 'tt' in the editor, and changing the base to 'tt' removes the error as well.

It seems you cannot use a variable name as a value, it must be the object's name (not a pointer), so player, or some other name for an object is acceptable.

Share this post


Link to post
Share on other sites

That is how you start the first script?

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">[_vehicle] execVM ""scripts\salvage.sqf"";}

nearestObjects expect a position and not an vehicle.

And it get worser:

Since you launch this code in an event handler, _vehicle is undefined, so you provide 0 elements where 3 are required wink_o.gif

Use the provided elements from the event handler to retrieve the vehicle.

Share this post


Link to post
Share on other sites

I thought it had something to do with that ...

Kind of figured it out, but that makes it clear.

_vehicle WAS defined further up in the respawn vehicle script, it is the first element of the passed array from the editor, the unit itself (this). I didn't try using the event handler's array ... probably would have worked.

Either way, once I got this "working", I ran into a bunch more roadblocks, and decided to take a simpler approach.

Thanks for the help however, figuring out this language bit by bit!

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
Sign in to follow this  

×