Jump to content
Luft08

What the heck is going on here???

Recommended Posts

I'm trying to debug my convoy code. I have placed a civilian on the map and named him "civ_1" and then try to move him to the random convoy starting position with this code:

private _temp = []; // ---------------------Test, remove
_temp = convoyPath select 0;// ------------------Test, remove
// The following line prevents civ_1 from spawning high above the ground:
private _civPos = [_temp select 0, _temp select 1, 0];// <<<-- This is the offending line.
civ_1 setPos _civPos; // ------------------Test, remove

However, when the code runs I get the error message: Error undefined variable in expression: _temp

Share this post


Link to post
Share on other sites

I am not able to find anything related to convoyPath in BIKI. Is this supposed to be a new addition or a variable you have defined somewhere else in your code (not shown here)?

Share this post


Link to post
Share on other sites
1 hour ago, ZaellixA said:

I am not able to find anything related to convoyPath in BIKI. Is this supposed to be a new addition or a variable you have defined somewhere else in your code (not shown here)?

Sorry, I gave incomplete info.  convoyPath is an array of positions I get from:

if(!isServer) exitWith {};

params["_startPos", "_endPos"];
if(convoyPath isEqualTo []) then {
	(calculatePath ["wheeled_APC","safe",_startPos,_endPos]) addEventHandler ["PathCalculated",{
    	{
			if((_forEachIndex mod 15 == 0) && drawPath) then {
				private _markerName = ("marker" + str _forEachIndex);
    			private _marker = createMarker [_markerName, _x];
    			_marker setMarkerType "mil_dot"; 
    			_marker setMarkerColor "ColorOrange";
     		};
     		convoyPath pushBack _x;
    	} forEach (_this select 1);
	}];
};

The drawPath variable is a global boolean. (I would like to pass that boolean as a param but I can't get the code to acknowledge that it has been defined. Different problem...) 

Share this post


Link to post
Share on other sites
6 hours ago, Luft08 said:

drawPath variable is a global boolean. ...  but I can't get the code to acknowledge that it has been defined. Different problem

Same problem, If drawPath is not defined then it will be Nil in the event, causing the event to never run the code in the IF statement, so convoyPath will never be set OR remain as [] (You do not actually show where and to what configPath is defaulted to, so just going off of the isEqualTo [] ).

So in your first code selecting index 0 of configPath is Nil so _temp will be nil, and selecting index 0 of _temp is undefined (nil), hence the error.

h = [] spawn {
	waitUntil{ time>0 };
	
	fnc_test = {
		path = [];
		_temp = path select 0;
		_civPos = [_temp select 0, _temp select 1, 0]; //<-- ERROR: _temp undefined
		systemChat format[ "CivPos: %1", _civPos ]; //<-- [any,any,0]
	};

	call fnc_test;
};

Run from some init in test mission.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
8 hours ago, Luft08 said:

// The following line prevents civ_1 from spawning high above the ground:

Use setVehiclePosition.

Quote

If position is in water and vehicle can float, it is placed on water surface, otherwise it is placed on the ground, even if ground is under water. If roof surfaces support walking, units will be placed on roofs if such position is given.

h = [] spawn {
	_unit = group player createUnit[ typeOf player, player getPos[ 10, getDir player ], [], 0, "NONE" ];
	
	sleep 1;
	
	_pos = player getPos[ 20, getDir player ];
	_pos set[ 2, 10 ];
	//_unit setVehiclePosition[ _pos, [], 0, "NONE" ];
	_unit setPosATL _pos;
};

Run and then uncomment setVehiclePosition line and comment setPosATL to see difference.

  • Like 2

Share this post


Link to post
Share on other sites

@Luft08, I haven't done testing without the isNil check for adding the PathCalculated event handler to calculatePath, but I've noticed you do not use it. 

In the examples given previously by myself and @pierremgiin another thread, we both did.

Not sure if this could be messing up your convoyPath array, but the wiki does state this in the big yellow caution box at the top of calculatePath page.

 

Also, just to point out, convoyPath may be overwritten if the code is run in a loop.  I.E. first run convoyPath count is 0, next iteration it is > 0.  This would mean your statement "if(convoyPath isEqualTo []) then {...}" runs only once.

This depends on where you declare convoyPath = [] in relation to the entire code/scope.  If this is intent, disregard.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
1 hour ago, panther42 said:

@Luft08, I haven't done testing without the isNil check for adding the PathCalculated event handler to calculatePath, but I've noticed you do not use it. 

In the examples given previously by myself and @pierremgiin another thread, we both did.

Not sure if this could be messing up your convoyPath array, but the wiki does state this in the big yellow caution box at the top of calculatePath page..

Wow, how did I miss the big yellow caution box on the wiki? I'll rework my code. Thanks! 🙂

Share this post


Link to post
Share on other sites

Just in case, here is a link to a quick test mission I put together.

p42_calculatePath

 

It's set up for player to ride along.  Edit as you wish.

View the map to see route positions.

Change "end" marker to a different location, etc.

  • Thanks 1

Share this post


Link to post
Share on other sites
12 minutes ago, panther42 said:

Just in case, here is a link to a quick test mission I put together.

p42_calculatePath

 

It's set up for player to ride along.  Edit as you wish.

View the map to see route positions.

Change "end" marker to a different location, etc.

Thanks, I'll play around with it. How does it prevent the "double run" bug? I need to be able to get the path position array out.  My code checks that a global variable is equal to [] (which it is in the beginning) and then doesn't overwrite it.

Code with isNil added but untested:

if(!isServer) exitWith {};

params["_startPos", "_endPos"];
if(convoyPath isEqualTo []) then {
	isNil {(calculatePath ["wheeled_APC","safe",_startPos,_endPos]) addEventHandler ["PathCalculated",{
    	{
			if((_forEachIndex mod 15 == 0) && drawPath) then {
				private _markerName = ("marker" + str _forEachIndex);
    			private _marker = createMarker [_markerName, _x];
    			_marker setMarkerType "mil_dot"; 
    			_marker setMarkerColor "ColorOrange";
     		};
     		convoyPath pushBack _x;
    	} forEach (_this select 1);
	}]};
};

 

Share this post


Link to post
Share on other sites
1 hour ago, Luft08 said:

Thanks, I'll play around with it. How does it prevent the "double run" bug?

 

I do not believe I ever had issues with the "double run" bug for the event handler. 

Perhaps the code was updated, or wiki not updated once fixed? 

There may be certain instances when this happens, but I have not ran across in the testing I have done.

It would be easy to identify, as the wiki states there are only two elements returned, which are both the end point.

Share this post


Link to post
Share on other sites
3 minutes ago, panther42 said:

 

I do not believe I ever had issues with the "double run" bug for the event handler. 

Perhaps the code was updated, or wiki not updated once fixed?

 

No the bug is here. You can miss it if you create markers on path, because they are persistent. The returned array(s) are like described:

- the first one (first run of the EH) is OK,

- the second one (second useless, unwanted EH fire) returns a double array of destination.

Spoiler

For some unknown reason, the "PathCalculated" Event Handler is fired twice, first with calculated path and second with array consisting of 2 elements, which are identical and are equal to the end point.

 

Share this post


Link to post
Share on other sites

interesting, as the code I use, I would expect the global array to be overwritten by only the two points then?

see the mission I posted in a previous post.

Perhaps I'm missing something?

I have markers and diag_log inserted to verify route positions of the array.

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

×