Lua Help Needed

KingRoon

Chieftain
Joined
May 23, 2016
Messages
2
Need someone to look over my code, nothing seems to be happening. (I am very new to lua.)

Code:
GameEvents.PlayerDoTurn.Add(
	function(iPlayer)
		local pPlayer = Players[iPlayer]
		local iPlayerCiv = pPlayer:GetCivilizationType()
		local iMongoliaCiv = GameInfoTypes.CIVILIZATION_MONGOL
        if iPlayerCiv == iMongoliaCiv then
                local iTurnTrigger = 4
                local iTurnsElapsed = Game.GetElapsedGameTurns()
                if iTurnsElapsed % iTurnTrigger == 0 then
                    local unit
					local playerStartPlot
					playerStartPlot = pPlayer.GetStartingPlot()
					unit = pPlayer:InitUnit(GameInfoTypes["UNIT_WARRIOR"], playerStartPlot:GetX(), playerStartPlot:GetY())
				end
		end
	end)

Tried to make this code from some guides online, however I might be missing something, since it doesn't actually spawn a warrior every 4 turns for the mongolia civ. This is all the code I have btw.
 
looks like it should work, unless the syntax is not right for lua, could rewrite it in more "safe" style (define a named function outside the Add())

Anyway, did you add the lua file as a game ui addin on the project settings? you have to, check whowards's signature

Launch the FireTuner while playing to see errors, use print() to do print debugging
 
You had one fatal syntax error, plus I re-arranged the code a little to cache data that does not change at the start of the game session rather than looking it up or re-creating it every turn for every player that processes through the PlayerDoTurn event.

I also eliminated one or two variables that weren't actually necessary.
Code:
local iMongoliaCiv = GameInfoTypes.CIVILIZATION_MONGOL
local iWarrior = GameInfoTypes["UNIT_WARRIOR"]
local iTurnTrigger = 4

GameEvents.PlayerDoTurn.Add(
	function(iPlayer)
 		local pPlayer = Players[iPlayer]
		if (pPlayer:GetCivilizationType() == iMongoliaCiv) and (Game.GetElapsedGameTurns() % iTurnTrigger == 0) then
			local playerStartPlot = pPlayer:GetStartingPlot()
			local pUnit = pPlayer:InitUnit(iWarrior, playerStartPlot:GetX(), playerStartPlot:GetY())
			pUnit:JumpToNearestValidPlot()
		end
	end)
There are still potential issues in that the player's starting plot might have been conquered by another player or might be currently occupied by an enemy, which will tend to kill off the enemy -- it will also tend to re-capture the player's original capital if the capital-plot and the starting-plot are the same. So I would actually re-write as:
Code:
local iMongoliaCiv = GameInfoTypes.CIVILIZATION_MONGOL
local iWarrior = GameInfoTypes["UNIT_WARRIOR"]
local iTurnTrigger = 4

GameEvents.PlayerDoTurn.Add(
	function(iPlayer)
 		local pPlayer = Players[iPlayer]
		if (pPlayer:GetCivilizationType() == iMongoliaCiv) and (Game.GetElapsedGameTurns() % iTurnTrigger == 0) then
			local pCapitalCity = pPlayer:GetCapitalCity()
			if pCapitalCity ~= nil then
				local pUnit = pPlayer:InitUnit(iWarrior, pCapitalCity:GetX(), pCapitalCity:GetY())
				pUnit:JumpToNearestValidPlot()
			end
		end
	end)
This will give the units in or next to the player's current capital city.

-----------------------------------------------------------------------------------------

[edit] And to Gia's point about proper implimentation of the lua file within modbuddy, see whoward69's what ModBuddy setting for what file types tutorial

You want the lua file containing this code to be set-up as an InGameUIAddin (Post #3 I believe in W.Howard's tutorial).
 
You had one fatal syntax error, plus I re-arranged the code a little to cache data that does not change at the start of the game session rather than looking it up or re-creating it every turn for every player that processes through the PlayerDoTurn event.

I also eliminated one or two variables that weren't actually necessary.
Code:
local iMongoliaCiv = GameInfoTypes.CIVILIZATION_MONGOL
local iWarrior = GameInfoTypes["UNIT_WARRIOR"]
local iTurnTrigger = 4

GameEvents.PlayerDoTurn.Add(
	function(iPlayer)
 		local pPlayer = Players[iPlayer]
		if (pPlayer:GetCivilizationType() == iMongoliaCiv) and (Game.GetElapsedGameTurns() % iTurnTrigger == 0) then
			local playerStartPlot = pPlayer:GetStartingPlot()
			local pUnit = pPlayer:InitUnit(iWarrior, playerStartPlot:GetX(), playerStartPlot:GetY())
			pUnit:JumpToNearestValidPlot()
		end
	end)
There are still potential issues in that the player's starting plot might have been conquered by another player or might be currently occupied by an enemy, which will tend to kill off the enemy -- it will also tend to re-capture the player's original capital if the capital-plot and the starting-plot are the same. So I would actually re-write as:
Code:
local iMongoliaCiv = GameInfoTypes.CIVILIZATION_MONGOL
local iWarrior = GameInfoTypes["UNIT_WARRIOR"]
local iTurnTrigger = 4

GameEvents.PlayerDoTurn.Add(
	function(iPlayer)
 		local pPlayer = Players[iPlayer]
		if (pPlayer:GetCivilizationType() == iMongoliaCiv) and (Game.GetElapsedGameTurns() % iTurnTrigger == 0) then
			local pCapitalCity = pPlayer:GetCapitalCity()
			if pCapitalCity ~= nil then
				local pUnit = pPlayer:InitUnit(iWarrior, pCapitalCity:GetX(), pCapitalCity:GetY())
				pUnit:JumpToNearestValidPlot()
			end
		end
	end)
This will give the units in or next to the player's current capital city.

-----------------------------------------------------------------------------------------

[edit] And to Gia's point about proper implimentation of the lua file within modbuddy, see whoward69's what ModBuddy setting for what file types tutorial

You want the lua file containing this code to be set-up as an InGameUIAddin (Post #3 I believe in W.Howard's tutorial).

Ty for that, I will try the code tomorrow. However I'm not too worried about the capital part, I was using that part as a place holder and was planning to spawn the units at another unit, I just didn't make that unit yet.
 
Back
Top Bottom