Jump to content
Sign in to follow this  
chaoticgood

Undefined variable in vehicle tow script

Recommended Posts

Hello, my script is throwing out an error and I don't know why:

playerTow =
{
while{true}do
	{
	sleep 3;

	_vehArray = nearestObjects[_this,["Car","Tank"],6];
	hint format ["Vehicle Array: %1",_vehArray];
	_vehCount = count _vehArray;
	if(_vehCount >=1)then{
	_vehSelected = (_vehArray select 0);
	sleep 6;

	hint format ["Vehicle selected: %1",_vehSelected];

	if(isNil "_ID")then
	{
	_ID = _vehSelected addAction ["Tow Object",{handle2 = [_vehSelected] spawn towObject;}];
	waitUntil{
	sleep 3;
	(_this distance _vehSelected) >= 6;};
	_vehSelected removeAction _ID;
	};

		};
	};
};

Ok, so what this script does:

Firstly, it constantly checks every 3 seconds for a vehicle of the given type and counts what it has found.

Secondly, once it finds a vehicle or if multiple vehicles, it will use the closest vehicle to the player.

Thirdly, if an addAction is not already applied to the selected vehicle, it adds one. If the player leaves the area, it removes the addAction.

The hints are for dubugging purposes.

Now for the error:

handle2 = [|#|_vehSelected] spawn towObject;

Error undefined variable in expression: _vehSelected

I just don't get why it's saying this, I know that it's saying the variable needs defining, but at this point in the script, it looks like to me that it is defined.

Share this post


Link to post
Share on other sites

If nothing is found, if (_vehArray select 0) is null, then you'll get the undefined error. You could toss in a _vehSelected = objNull; just above that line to get around the error.

Share this post


Link to post
Share on other sites

Thanks Kylania, but it still gives the same error with the amendment you suggested.

Share this post


Link to post
Share on other sites

_vehSelected inside the ddActions code block is in a different scope so it has no idea what this variable is (its undefined in this scope). As the addAction is on the vehicle you want to tow use (_this select 0) instead.

_ID = _vehSelected addAction ["Tow Object", {handle2 = [(_this select 0)] spawn towObject;}]; 

or pass the code the _vehSelected in the args param.

_ID = _vehSelected addAction ["Tow Object", {handle2 = [(_this select 3)] spawn towObject;}, _vehSelected]; 

you can also add your condition for the action to show here aswell rather than keep adding and removing it based on distance.

_ID = _vehSelected addAction ["Tow Object", {handle2 = [(_this select 3)] spawn towObject;}, _vehSelected, 1, true, true, "", "(_this distance _target) < 6"]; 

To be honest i would turn the whole process of how your doing this on its head.

No need for a function continuly checking for near vehicles.

Just add an action to the player that checks his cursor targets type, if its a CAR or TANK show the action.

player addAction ["Tow Object", {(_this select 3) attachTo [(_this select 0),[0,-6,2] ];}, cursorTarget, 1, true, true, "", "(((_target distance cursorTarget ) < 6) && ({typeof cursortarget iskindof _x}count [""Car"",""Tank""] != 0))"];

Mwoarhh is that a car stuck to your arse!

Edited by Larrow

Share this post


Link to post
Share on other sites

Oh wow, that's so much simpler than what I had in mind. When I first started, I tried to plan out my vehicle tow script to be as efficient as possible, I thought about adding an addAction to every vehicle on the server. I thought "that seems unnecessary", how about if I add it to only the vehicle that the player is near. Then you come in and do it 1 line of code :]

EDIT: I tried your code and I can't get it to work >.<

Using your last line, I click tow object while looking at a vehicle and nothing happens.

Edited by chaoticgood

Share this post


Link to post
Share on other sites

Works fine from the debug console as written in my last post but if you put it in the players init the action shows but it fails todo anything.

h = [] spawn {
sleep 1;
id = player addAction ["Tow Object", {(_this select 3) attachTo [(_this select 0),[0,-6,2] ];}, cursorTarget, 1, true, true, "", "(((_target distance cursorTarget ) < 6) && ({typeof cursortarget iskindof _x}count [""Car"",""Tank""] != 0))"];
};

Put that in the players init, works ok.

Of course its not exactly what you want lol but gives you something to work from.

Edited by Larrow

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  

×