Lua - unexpected bizzare behavior

Infixo

Deity
Joined
Jan 9, 2016
Messages
4,034
Location
Warsaw
Hello, I need help with a really bizzare behavior.
I've encountered a situation where putting some lines of code into a function will produce a different result. No change of order, ofc, just part of the code is in a function - different result. If I put all lines into the same function - evereything is back to normal.

Details:

1. I have the following (simplified) code:
Code:
function SetUnitMovesLens()
  -- now does nothing
end
function OnLocalPlayerTurnBegin()  -- standard event hook
  <some code A>
  <some code B>
  <some code C>
  <some code D>
end
The above is working as expected, no problems.
2. Then I have this:
Code:
function SetUnitMovesLens()
  <some code A>
  <some code B>
  <some code C>
  <some code D>
end
function OnLocalPlayerTurnBegin()  -- standard event hook
  SetUnitMovesLens()
end
Working the same, no problems.
3. Last version:
Code:
function SetUnitMovesLens()
  <some code C>
  <some code D>
end
function OnLocalPlayerTurnBegin()  -- standard event hook
  <some code A>
  <some code B>
  SetUnitMovesLens()
end
..and it doesn't work. Meaning - the program works, but the results are totally different. I'm lost and confused.

I have attached a mod with this situation. I'll be glad if anyone could check if this situation happens also for you and maybe even explain it or fix it?

Attached mod and how to use it.
It's a simple mod for testing some UI functions (specifically Lenses). Run it with no other mods active. Then start a game and move your units for a few turns. All the plots that your units ever visited will be highlighted with red, your current positions with blue. Also for 2 first units the path of their movement will be drawn. Then there are 3 versions of HelloWorldPopup.lua that corresponds to 3 scenarios I described above. Scenario 1 and 2 are working as expected. Scenario 3 is not working - plots are not highligthed and no paths are drawn.
 

Attachments

Looks like a localization error to me.

The essential difference between methods #1 & #2 and the non-cooperative method #3 is that all the code is not running from the same function in #3 whereas it is in #'s 1 & 2

Non-Cooperative version #3:
Code:
function OnLocalPlayerTurnBegin_UnitMoves()
	dprint("FUNSTA OnLocalPlayerTurnBegin_UnitMoves(), LocalPlayer", Game.GetLocalPlayer());

	
	
	-- here is data gathering
	local ePlayer = Game.GetLocalPlayer();

	.......snipped lines .....

	SetUnitMovesLens();
	dprint("FUNEND OnLocalPlayerTurnBegin_UnitMoves(), LocalPlayer", Game.GetLocalPlayer());
end
ePlayer is then referenced within function SetUnitMovesLens() but it would be a nil value at that point:
Code:
	UILens.SetLayerHexesArea(LensLayers.HEX_COLORING_GREAT_PEOPLE, ePlayer, m_targetPlots);
You need to do one of:
  1. Pass ePlayer as an argument from OnLocalPlayerTurnBegin_UnitMoves() to SetUnitMovesLens();
  2. define ePlayer directly within SetUnitMovesLens();
Generally my preference is to always use method #1 because then you know all your code is marching to the same drum-beat, but in this particular instance the local player is not likely to change in the time it takes for OnLocalPlayerTurnBegin_UnitMoves() to be intiated and for it to summon SetUnitMovesLens(), so in this particular case you are probably safe to define the variable in both places.
 
Back
Top Bottom