Jump to content
Sign in to follow this  
Rydygier

Problem with "if () then {};" inside OnSingleMapClick's quotes

Recommended Posts

Here is code with some diag_log "checks":

_PosX = 0;
_Posy = 0;
_Ashells = [];
_i = 0;
_start = false;

OnMapSingleClick 
'
diag_log "check1";

if not (_start) then
	{
	diag_log "checkA";
	_start = true; 
	_PosX = _pos select 0;
	_PosY = _pos select 1; 

	_i = createMarker ["rangemark",[_posX,_posY]];
	_i setMarkerColor "ColorKhaki";
	_i setMarkerShape "ELLIPSE";
	_i setMarkerSize [1000,1000];
	_i setMarkerBrush "Border";
	};

diag_log "check2";

if ((_start) and (_alt)) then 
	{
	diag_log "checkB";
	_start = false;
		{
		deleteMarker ("shell" + str (_x))
		} 
	foreach _Ashells; 
	deleteMarker _i
	};

diag_log "check3"
';

On click RPT shows checks 1,2 and 3, but not A or B, not to mention about marker, which I try to create when clicking on map.

On the other hand, in the following case, without "if-then" condition:

_PosX = 0;
_Posy = 0;
_Ashells = [];
_i = 0;
_start = false;

OnMapSingleClick 
'
diag_log "check1";

//if not (_start) then
	//{
	diag_log "checkA";
	_start = true; 
	_PosX = _pos select 0;
	_PosY = _pos select 1; 

	_i = createMarker ["rangemark",[_posX,_posY]];
	_i setMarkerColor "ColorKhaki";
	_i setMarkerShape "ELLIPSE";
	_i setMarkerSize [1000,1000];
	_i setMarkerBrush "Border";
	//};

diag_log "check2";

/*if ((_start) and (_alt)) then 
	{
	diag_log "checkB";
	_start = false;
		{
		deleteMarker ("shell" + str (_x))
		} 
	foreach _Ashells; 
	deleteMarker _i
	};*/

diag_log "check3"
';

"CheckA" in RPT and marker on map shows on click...

Why? Seems, that code inside curled braces is ignored for some reason when it's in quotes? Perhaps these brackets should be replaced with something in this case? Or maybe something else is wrong, maybe I'm simply blind and do not see something obvious?

Share this post


Link to post
Share on other sites

Thanks. Again. "_start = false;" is now inside OnSingleMapClick's quotes and it's works (of course can't stay there, will think about that. Tommorow).

Hmm. It seemed to me that defining of this variable at the beginning does the trick with "scopes", apparently I was wrong. Something is still not understand (but hey, less than hour to midnight here...), and I do not want to repeat this kind of error in the future. Why is this variable had to be defined inside OnSingleMapClick, and not outside, and why only this and others used inside "if-then" do not? Is said:

If a local variable is initialized within a Control Structures (i.e. if, for, switch, while) its scope will stay within this structure

but "_start" wasn't initialized inside CS where is used, but before, similar to the second example from Biki.

Share this post


Link to post
Share on other sites

OnMapSingleClick, addEH and such create a completely new scope.

Control structures create a subscope.

You need to use global variables, or setVar/getVar to access data from outside.

Share this post


Link to post
Share on other sites

So that's it... Well, so will globaliz'em. Thanks. It is good to learn something new in the morning.

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  

×