LeeS
Imperator
- Functions subscribed to the PlayerDoTurn hook event need to state a variable name on the function line for the gamecore to pass the ID# of the player currently being processed. You need
Code:
function PlecoRuinsResource(iPlayer)
Code:function PlecoRuinsResource()
- You need to get the player object from the passed player ID# before you can do any "player" methods. Otherwise you get a nil value error. This causes a nil value error because pPlayer has never been defined
Code:
pPlayer:GetCivilizationType()
Code:function PlecoRuinsResource(iPlayer) local pPlayer = Players[iPlayer]
- The next line has multiple problems:
- iTriremes has never been defined and is therefore a nil value
- Syntax "==" is not used to place a value into a variable. "=" is required
- You've got the order reversed for a "set value" statement. Your code is attempting to place the current value within variable "iTriremes" into the pPlayer:GetUnitClassCount(iUnitClass) method, which you can never do
- It appears you were attempting to create variable "iTriremes" and set it to the current number of units the player has belonging to the UNITCLASS_TRIREME class of units, but your order is reversed nor did you make "iTriremes" a local variable, which would tend to make whatever value is placed into this variable persist between turns.
- What you would want would be
Code:
local iTriremes = pPlayer:GetUnitClassCount(iTriremeUnitClass)
- You've forgotten the needed 'then' keyword here
Code:
if iTriremes > 0
- You have no properly-defined unit object here, so "pUnit" is a nil value
Code:
pUnit:GetID()
- In order to process through all of a given player's units, Civ5 lua gives us a special command for this
Code:
pPlayer:Units()
Code:for pUnit in pPlayer:Units() do local iUnitType = pUnit:GetUnitType() if pUnit:IsCombatUnit() then print("A unit of UnitType " .. iUnitType .. " (" .. Locale.ConvertTextKey(GameInfo.Units[iUnitType].Description)) .. ") belonging to player " .. iPlayer .. " (" .. pPlayer:GetName() .. ") was discovered") end end
- This would print a message into the lua log for every combat unit that was discovered showing the unit's name as it appears in-game as well as the player's lua ID# and their name.
- "pUnit" is an object variable that holds each unit's object in turn as the loop is conducted through all units belonging to the player. I could just as easily have called this object variable "Cheeseburgers":
Code:
for Cheeseburgers in pPlayer:Units() do local iUnitType = Cheeseburgers:GetUnitType() if Cheeseburgers:IsCombatUnit() then print("A unit of UnitType " .. iUnitType .. " (" .. Locale.ConvertTextKey(GameInfo.Units[iUnitType].Description)) .. ") belonging to player " .. iPlayer .. " (" .. pPlayer:GetName() .. ") was discovered") end end
- An object variable is just that: a variable. We can call it anything we want. But whatever we call it, we must consistently call it the same thing so long as we are using that same variable.
- "Cheeseburgers" in this case is a temporary object-variable used within the loop. "Cheeseburgers" holds each successive object for each successive unit as the loop through the payer's units is conducted.
- When we talk about a unit method or a player method between ourselves, we generally shorthand and show things like
Code:
Unit:GetPlot()
- Your needed code then becomes
Code:
local iCiv = GameInfoTypes.CIVILIZATION_PLECOSTOMUS local iSuckerMouth = GameInfoTypes.UNIT_SUCKERMOUTH_SQUAD local iTriremeUnitClass = GameInfoTypes.UNITCLASS_TRIREME function PlecoRuinsResource(iPlayer) local pPlayer = Players[iPlayer] if (pPlayer:GetCivilizationType() == iCiv) and (pPlayer:GetUnitClassCount(iTriremeUnitClass) > 0) then for pUnit in pPlayer:Units() do if (pUnit:GetUnitType() == iSuckerMouth) then local pPlot = pUnit:GetPlot() local iOriginalPlotOwner = pPlot:GetOwner() local iResourceID = pPlot:GetResourceType(); if (iResourceID ~= -1) and (iOriginalPlotOwner ~= -1) and (iOriginalPlotOwner ~= iPlayer) then local pPlotOwnerPlayer = Players[iOriginalPlotOwner] --plot resource removal stuff goes here if pPlotOwnerPlayer:IsHuman() then local text = Locale.ConvertTextKey("TXT_KEY_PLECO_DESTRUCTION", GameInfo.Resources[iResourceID].Description); local heading = Locale.ConvertTextKey("TXT_KEY_PLECO DESTRUCTION_SHORT"); pPlotOwnerPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, text, heading, pUnit:GetX(), pUnit:GetY()); end end end end end end GameEvents.PlayerDoTurn.Add(PlecoRuinsResource);
This chunk of the code will only need to be run if the resource is actually removed:Code:if pPlotOwnerPlayer:IsHuman() then local text = Locale.ConvertTextKey("TXT_KEY_PLECO_DESTRUCTION", GameInfo.Resources[iResourceID].Description); local heading = Locale.ConvertTextKey("TXT_KEY_PLECO DESTRUCTION_SHORT"); pPlotOwnerPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, text, heading, pUnit:GetX(), pUnit:GetY()); end
- At this point there are some questions that need answering from a code-logic point-of-view:
- Are you going to remove resources from City-States, even if they are at war with you ? You might want these resources later on.
- Are you going to remove resources from Allies and Friends, or only players with whom you are currently at war?
- Are you going to remove strategic and luxury resources? Remember, you might want these later on.
- There might be other questions that need answering before any further work on the code could be made. Troller might have a suggestion or two, forex.

Last edited: