Determining whose turn it is

Harald B

Warlord
Joined
Oct 20, 2010
Messages
135
Is there a lua function to find out which player's turn it is? I can't seem to find any in the wiki.
 
Game.GetActivePlayer()

If you need to ascertain when the player changes, hook the GameEvents.PlayerDoTurn event, eg

Code:
function OnPlayerDoTurn(iPlayer)
  print(string.format("Start of turn for player %i"), iPlayer)
end
GameEvents.PlayerDoTurn.Add(OnPlayerDoTurn)
 
Wait, doesn't that just tell you who the human player is? That's what I read several times when searching the forum. Well if you say so I'll give it a shot. Thank you.
 
Memo to self - do not write code/posts when suffering from flu!

Game.GetActivePlayer() in NOT what you want.

The event code works, so you can track the active player that way, or you could loop all players and test with pPlayer:IsTurnActive()
 
I'm afraid the event method won't do for my purposes, but good to know about the IsTurnActive method.
So if I understand things right, I could use the following as a surrogate?
Code:
function TheTurnPlayer()
for pPlayer in Players do
	if pPlayer:IsTurnActive() then return pPlayer
	end
end
end
Thanks again.
 
That would work, but depending on how many times you call it, the following may be more efficient

Code:
local g_CurrentPlayer = 0

function OnPlayerDoTurn(iPlayer)
  g_CurrentPlayer = iPlayer
end
GameEvents.PlayerDoTurn.Add(OnPlayerDoTurn)

function GetCurrentPlayer()
  return g_CurrentPlayer
end
 
I see. Just out of curiosity, is there a specific reason for defining the function GetCurrentPlayer() instead of just using g_CurrentPlayer directly?
 
Back
Top Bottom