Proofread my LUA

Mikhaelstan

Chieftain
Joined
Dec 10, 2016
Messages
18
Hi everyone, I've recently finished my first LUA code and would be very grateful if someone could proofread it to identify errors and make suggestions.

The first provides Great Writer points equal to the number of Landmarks in your territory and the happiness derived from discovered Natural Wonders.

Spoiler :

function TjukurpaGP(playerID, city)
local pCapital = pPlayer:GetCapitalCity()
local iNatureHappy = pPlayer:GetHappinessFromNaturalWonders()
local iLandmark = GameInfoType["Improvement_Landmark"]
local iLandmarkCount = pPlayer:GetImprovementCount(iLandmark)
local iAlteration = iNatureHappy + iLandmarkCount
if pCapital then
else pCapital:ChangeSpecialistGreatPersonProgressTimes100(GameInfoTypes.SPECIALIST_WRITER, 100*iAlteration)
end
end
GameEvents.PlayerDoTurn.Add(TjukurpaGP)


The second gifts a free Great Writer after the completion of a Landmark 30% of the time
Spoiler :

function AllBecomesHidden(playerID, improvementID)
local pPlayer = Players[player]
local iThreshold = pCity:GetSpecialistUpgradeThreshold(Specialist_Writer)
local iProgress = pCity:GetSpecialistGreatPersonProgressTimes100(Specialist_Writer*100)
local iThreshold = pCity:GetSpecialistUpgradeThreshold(Specialist_Writer)
local iDice = Math.Random(0.1, 1.0)
local iCanAdd = iThreshold - iProgress
if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_PITJANTJATJARA then
if (pCity == nil) then return end
if improvementID = Improvement_Landmark then
if iDice > 0.3 then end
else iDice <= 0.3 then
pCity:ChangeSpecialistGreatPersonProgressTimes100(Specialist_Writer, iCanAdd*100)
end
end
end
end
GameEvent.BuildFinished.Add(AllBecomesHidden)


Thanks in advance.
 
Code:
function TjukurpaGP(playerID, city)
	local pCapital = pPlayer:GetCapitalCity()
	local iNatureHappy = pPlayer:GetHappinessFromNaturalWonders()
	local iLandmark = GameInfoType["Improvement_Landmark"]
	local iLandmarkCount = pPlayer:GetImprovementCount(iLandmark)
	local iAlteration = iNatureHappy + iLandmarkCount
	if pCapital then
		else pCapital:ChangeSpecialistGreatPersonProgressTimes100(GameInfoTypes.SPECIALIST_WRITER, 100*iAlteration)
	end
end
GameEvents.PlayerDoTurn.Add(TjukurpaGP)
  1. functions hooked to PlayerDoTurn only recieve one argument from the game core, for the player ID# of the player currently taking their turn. So the second argument you are stating city will always have a value of nil.
  2. This will simply cause runtime errors for attempting to index a nil value:
    Code:
    local pCapital = pPlayer:GetCapitalCity()
    You have to define variable pPlayer before you can use it.
  3. Same issue with this line
    Code:
    local iNatureHappy = pPlayer:GetHappinessFromNaturalWonders()
  4. You have two script-breaking errors in this line
    Code:
    local iLandmark = GameInfoType["Improvement_Landmark"]
    • You are missing an 's': it is GameInfoTypes not GameInfoType
    • "Improvement_Landmark" is invalid because capitalization matters in lua. You need "IMPROVEMENT_LANDMARK"
  5. Other than adding " marks at each end so that lua knows you are using a text string, you must copy all tags from the game's xml files exactly
  6. This is wrong
    Code:
    	if pCapital then
    		else pCapital:ChangeSpecialistGreatPersonProgressTimes100(GameInfoTypes.SPECIALIST_WRITER, 100*iAlteration)
    	end
    • As structured, this line is asking if variable pCapital has a value other than nil, and if it has a value other than nil execute the then portion of the code-chunk.
    • But you have nothing for the code to do when the variable pCapital has a value other than nil.
    • The else clause in the code-chunk in this case executes only when pCapital has a value nil.
    • So you are telling lua to add great person points to a non-existant city, which the game cannot do.
  7. Fix as this, for the code as it currently is
    Code:
    function TjukurpaGP(playerID)
    	local pPlayer = Players[playerID]
    	local pCapital = pPlayer:GetCapitalCity()
    	if (pCapital ~= nil) then
    		local iAlteration = pPlayer:GetHappinessFromNaturalWonders() + pPlayer:GetImprovementCount(GameInfoTypes["IMPROVEMENT_LANDMARK"])
    		pCapital:ChangeSpecialistGreatPersonProgressTimes100(GameInfoTypes.SPECIALIST_WRITER, 100*iAlteration)
    	end
    end
    GameEvents.PlayerDoTurn.Add(TjukurpaGP)
  8. As the code currently is, however, every player with a capital city would get this adjustment applied, so you need to limit it to only the civilization you want it applied to. Hint here is that many mods have code wherein they are limiting an effect to only one civilization.



Code:
function AllBecomesHidden(playerID, improvementID)
	local pPlayer = Players[player]
	local iThreshold = pCity:GetSpecialistUpgradeThreshold(Specialist_Writer)
	local iProgress = pCity:GetSpecialistGreatPersonProgressTimes100(Specialist_Writer*100)
	local iThreshold = pCity:GetSpecialistUpgradeThreshold(Specialist_Writer)
	local iDice = Math.Random(0.1, 1.0)
	local iCanAdd = iThreshold - iProgress
	if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_PITJANTJATJARA then
		if (pCity == nil) then return end
			if improvementID = Improvement_Landmark then
				if iDice > 0.3 then end
			else iDice <= 0.3 then
				pCity:ChangeSpecialistGreatPersonProgressTimes100(Specialist_Writer, iCanAdd*100)
			end
		end
	end
end
GameEvent.BuildFinished.Add(AllBecomesHidden)

  1. Functions hooked to Game Event "BuildFinished" recieve data for the following arguments in the following order: playerID, PlotX, PlotY, improvementID
    1. playerID: the ID # of the player whose unit completed the improvement
    2. PlotX: the plot's grid-X position on the game map for the completed improvement
    3. PlotY: the plot's grid-Y position on the game map for the completed improvement
    4. improvementID: the ID # from xml-table <Improvements> for the improvement
  2. You have your argument for "improvementID" in the spot where the game engine will pass the data for the plot-location's map-grid-X location.
  3. If I write my argument names as this in this order: PlotY, improvementID, playerID, PlotX this does not change the way the data is passed from the game engine. What I am calling "PlotY" is in the 1st argument position and therefore will get the data for the player ID #, not the data for the Plot's grid-Y position.
  4. If I write my argument names as this in this order: Hamburger, Cheeseburger, TunaMelt, Sandwich this does not change the way the data is passed from the game engine. What I am calling "Hamburger" is in the 1st argument position and therefore will get the data for the player ID #.
  5. You've mismatched usage of playerID and player in these two lines
    Code:
    function AllBecomesHidden(playerID, improvementID)
    	local pPlayer = Players[player]
  6. Variable Specialist_Writer is not defined
  7. Variable pCity is not defined
  8. math.random wants whole integer numbers.
  9. This has two errors
    Code:
    if improvementID = Improvement_Landmark then
    • Variable Improvement_Landmark is not defined
    • on a conditional check line, equality must be shown as == and not as =
  10. == tells lua to look for whether two things are equivalent to each other
  11. = tells lua to make the first thing equal to the second thing
  12. This line
    Code:
    else iDice <= 0.3 then
    would need to be expressed as
    Code:
    elseif iDice <= 0.3 then
    but is actually completely unecessary since this line will end execution of the funtion when variable iDice is greater than "0.3":
    Code:
    if iDice > 0.3 then end
  13. Plus as written this line
    Code:
    else iDice <= 0.3 then
    is interpretted as being a second condition to check against when this line evaluates to boolean "false":
    Code:
    if improvementID = Improvement_Landmark then
  14. This line is missing the needed 's' at the end of "GameEvent"
    Code:
    GameEvent.BuildFinished.Add(AllBecomesHidden)
 
Thank you for your detailed correcting of my code and for the tips on making sure it works. I however have radically changed the design of the mod to the point where I have scraped this code altogether. Sorry for the wasted effort.
 
Back
Top Bottom