C++/Lua Request Thread

So, it looks like that's out of the picture, then.
One more question related to this: It is possible to have a Unit that generates Faith upon it's death?
Not directly with XML, but it can be done with lua. I seem to remember this very effect being discussed either upthread or in the main C&C forum. You use lua event UnitPreKill or UnitKilledInCombat, depending on whether you only want the unit to give the faith-effect as a result of being killed in battle, or whether any 'removal' of the unit from the game can trigger the effect.
 
Not directly with XML, but it can be done with lua. I seem to remember this very effect being discussed either upthread or in the main C&C forum. You use lua event UnitPreKill or UnitKilledInCombat, depending on whether you only want the unit to give the faith-effect as a result of being killed in battle, or whether any 'removal' of the unit from the game can trigger the effect.

I tried it out with UnitKilledInCombat (To make it generate faith upon death), but I kinda hit a roadblock. How do I make it so that the owner of the unit gets the faith?
Code:
-- Centurion
local iCenturion = GameInfoTypes.UNIT_KI_CENTURION
--local iPlayer = GameInfoTypes.CIVILIZATION_ANGEL_LAND

local function FaithFromDeath(iKiller, iOwner, iUnitType)
	if iUnitType == iCenturion then
			eValue = GameInfoTypes["YIELD_FAITH"]
			iValue = 3
	end
end
GameEvents.UnitKilledInCombat.Add(OnUnitKilledInCombat)

As for the E&D code, I changed the code the remove the errors you found. But the 2nd Decision isn't showing up still.
 
I tried it out with UnitKilledInCombat (To make it generate faith upon death), but I kinda hit a roadblock. How do I make it so that the owner of the unit gets the faith?
Mistakes in your code are so blatant. It's as if you copy someone else's code without even trying to understand what it does.
Code:
local iCenturion = GameInfoTypes.UNIT_KI_CENTURION
local iFaithFromCenturion = 3

local function OnUnitKilledInCombat(iKiller, iOwner, iUnitType)
	if iUnitType == iCenturion then
		Players[iOwner]:ChangeFaith(iFaithFromCenturion)
	end
end
GameEvents.UnitKilledInCombat.Add(OnUnitKilledInCombat)
 
Mistakes in your code are so blatant. It's as if you copy someone else's code without even trying to understand what it does.
Code:
local iCenturion = GameInfoTypes.UNIT_KI_CENTURION
local iFaithFromCenturion = 3

local function OnUnitKilledInCombat(iKiller, iOwner, iUnitType)
	if iUnitType == iCenturion then
		Players[iOwner]:ChangeFaith(iFaithFromCenturion)
	end
end
GameEvents.UnitKilledInCombat.Add(OnUnitKilledInCombat)

To tell you truth, that's what I did! And I'll probably never understand Lua.
Anyways, thanks.
 
Not sure how plausible this is to code however

It's a Naval unit with the ability of
"Whenever a Naval Trade Route Passes over this unit both. Cities the trade route is connected gain a Food bonus. (Can only trigger once per Trade Route per Turn.)"
 
If by "Trade Route" you mean the trade unit (ie the cargo ship itself), without a modded DLL it is impossible to determine the trade route that any given trade unit is for.
 
If by "Trade Route" you mean the trade unit (ie the cargo ship itself), without a modded DLL it is impossible to determine the trade route that any given trade unit is for.
Yeah, I meant the ship itself.

I Had a feeling it was going to be like this. Now would it be possible for it to work somewhat like CL's Afghanistan's UI but with a unit instead of a UI?
Spoiler :
DjchO98.png
 
I was wondering if someone could rig up an Lua code where it prevents a particular civilization from generating certain types of great people. I require this for a civilization's UA.

Also, another thing I need an Lua code for:

I tried using the FaithFromDyingUnits tag in the Beliefs table, but it turns out that it doesn't do anything (which begs the question: why is it even in there then?) Anyways, I need an Lua code that basically does what FaithFromDyingUnits was supposed to do, i.e. Make your units provide Faith when dying (so long as you're following a particular belief).

One last thing: I'd like an Lua that when you declare friendship with someone it gives them a unique dummybuilding in their capital.

If anyone could rig these up for me, that'd be splendid.
 
I was wondering if someone could rig up an Lua code where it prevents a particular civilization from generating certain types of great people.

Code:
local iUnluckyCiv = GameinfoTypes.CIVILIZATION_GREECE
local iMusician = GameinfoTypes.UNIT_MUSICIAN

local function OnUnitCreated(iPlayer, iUnit, hexPos, iUnknown, cultureType, iCivID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
 	If iCivID = iUnluckyCiv then
		 local pPlayer = Players[iPlayer]
		 local pUnit = pPlayer:GetUnitByID(iUnit)
		 if pUnit:GetUnitType() == iMusician  then
			pUnit:Kill()
		end
	end
end
Events.SerialEventUnitCreated.Add(OnUnitCreated)
I'd like an Lua that when you declare friendship with someone it gives them a unique dummybuilding in their capital.
Code:
local iYourCiv = GameinfoTypes.CIVILIZATION_GREECE
local 	iYourPlayer
local 	pYourPlayer
local iDummyBuilding = GameinfoTypes.BUILDING_YOUR_MOD_DUMMY

local function OnPlayerDoTurn(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:IsAlive() and pPlayer:GetDoFCounter(iYourPlayer) == 1 then
		local pTheirCapital = pPlayer:GetCapitalCity()
		pTheirCapital :SetNumRealBuilding(iDummyBuilding , 1)
	end
end

for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == iYourCiv then
		iYourPlayer = iPlayer
		pYourPlayer = pPlayer
		GameEvents.PlayerDoTurn.Add ( OnPlayerDoTurn )
		break
	end
end
 
One question on the FaithFromDyingUnits answer: Can the unit definition be switched from UNIT_KI_CENTURION to UNITCLASS_KI_CENTURION? (just as an example) It seems impractical to have to define that for every single unit instead of at least being able to lump them into UnitClass, which would be somewhat more manageable.

Also, I implemented the other two Lua code strings and for the one that disables Great People generation, I keep getting a syntax error in the .Lua log:

Code:
[432830.093] Syntax Error: C:\...\Sid Meier's Civilization 5\MODS\Harkodos' The Pollutors (BNW)\Lua/Hexxus_Units.lua:18: 'then' expected near '='
[432830.093] Runtime Error: Error loading C:\...\Sid Meier's Civilization 5\MODS\Harkodos' The Pollutors (BNW)\Lua/Hexxus_Units.lua.

And here's the actual code I have at present:
Code:
local iAmerica = GameInfoTypes.CIVILIZATION_AMERICA
local iAdmiral = GameInfoTypes.UNIT_GREAT_ADMIRAL
local iArtist = GameInfoTypes.UNIT_ARTIST
local iEngineer = GameInfoTypes.UNIT_ENGINEER
local iGeneral = GameInfoTypes.UNIT_GREAT_GENERAL
local iMerchant = GameInfoTypes.UNIT_MERCHANT
local iMusician = GameInfoTypes.UNIT_MUSICIAN
local iProphet = GameInfoTypes.UNIT_PROPHET
local iScientist = GameInfoTypes.UNIT_SCIENTIST
local iWriter = GameInfoTypes.UNIT_WRITER

local function OnUnitCreated(iPlayer, iUnit, hexPos, iUnknown, cultureType, iCivID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)

	if iCivID = iAmerica then
		 local pPlayer = Players[iPlayer]
		 local pUnit = pPlayer:GetUnitByID(iUnit)
		 if pUnit:GetUnitType() == iAdmiral  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iArtist  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iEngineer  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iGeneral  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iMerchant  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iMusician  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iProphet  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iScientist  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iWriter  then
			pUnit:Kill()
		end
	end
end
Events.SerialEventUnitCreated.Add(OnUnitCreated)
The error likes to pop up on line 18:
Code:
	if iCivID = iAmerica then
There were originally a few capitalization mistakes, but I fixed those. This is where I'm stuck now.
 
Code:
local iAmerica = GameInfoTypes.CIVILIZATION_AMERICA
local iAdmiral = GameInfoTypes.UNIT_GREAT_ADMIRAL
local iArtist = GameInfoTypes.UNIT_ARTIST
local iEngineer = GameInfoTypes.UNIT_ENGINEER
local iGeneral = GameInfoTypes.UNIT_GREAT_GENERAL
local iMerchant = GameInfoTypes.UNIT_MERCHANT
local iMusician = GameInfoTypes.UNIT_MUSICIAN
local iProphet = GameInfoTypes.UNIT_PROPHET
local iScientist = GameInfoTypes.UNIT_SCIENTIST
local iWriter = GameInfoTypes.UNIT_WRITER

local function OnUnitCreated(iPlayer, iUnit, hexPos, iUnknown, cultureType, iCivID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)

	if iCivID == iAmerica then
		 local pPlayer = Players[iPlayer]
		 local pUnit = pPlayer:GetUnitByID(iUnit)
		local iUnitType = pUnit:GetUnitType()
		 if iUnitType  == iAdmiral or iUnitType == iArtist  or iUnitType == iEngineer  or iUnitType == iGeneral or iUnitType ==  iMusician or iUnitType == iMerchant or iUnitType == iProphet  or iUnitType == iScientist  or iUnitType == iWriter  then
			pUnit:Kill()
		end
	end
end
Events.SerialEventUnitCreated.Add(OnUnitCreated)
 
One question on the FaithFromDyingUnits answer: Can the unit definition be switched from UNIT_KI_CENTURION to UNITCLASS_KI_CENTURION?

To get unit class use this code:
Code:
local sUnitClass = GameInfo.Units[iUnitType].Class
local iUnitClass = GameInfoTypes[sUnitClass]
 
.........Also, I implemented the other two Lua code strings and for the one that disables Great People generation, I keep getting a syntax error in the .Lua log:

And here's the actual code I have at present:
Code:
local iAmerica = GameInfoTypes.CIVILIZATION_AMERICA
local iAdmiral = GameInfoTypes.UNIT_GREAT_ADMIRAL
local iArtist = GameInfoTypes.UNIT_ARTIST
local iEngineer = GameInfoTypes.UNIT_ENGINEER
local iGeneral = GameInfoTypes.UNIT_GREAT_GENERAL
local iMerchant = GameInfoTypes.UNIT_MERCHANT
local iMusician = GameInfoTypes.UNIT_MUSICIAN
local iProphet = GameInfoTypes.UNIT_PROPHET
local iScientist = GameInfoTypes.UNIT_SCIENTIST
local iWriter = GameInfoTypes.UNIT_WRITER

local function OnUnitCreated(iPlayer, iUnit, hexPos, iUnknown, cultureType, iCivID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)

	if iCivID = iAmerica then
		 local pPlayer = Players[iPlayer]
		 local pUnit = pPlayer:GetUnitByID(iUnit)
		 if pUnit:GetUnitType() == iAdmiral  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iArtist  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iEngineer  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iGeneral  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iMerchant  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iMusician  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iProphet  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iScientist  then
			pUnit:Kill()
		 else if pUnit:GetUnitType() == iWriter  then
			pUnit:Kill()
		end
	end
end
Events.SerialEventUnitCreated.Add(OnUnitCreated)
  1. Where is iCivID coming from? It is not stated in the list of arguments function "OnUnitCreated" is being passed from the gamecore when SerialEventUnitCreated fires, and is therefore always 'nil'.
  2. You also have confusion between what "iPlayer" is and what "iAmerica" is, and what "pPlayer" is.
  3. Try this:
    Code:
    ------------------------------------------------------------------------------------------------
    --data to be set as needed
    ------------------------------------------------------------------------------------------------
    local iRequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
    
    local tGreatPeopleTypesKeepOrDelete = { [GameInfoTypes.UNIT_GREAT_ADMIRAL] = "delete", [GameInfoTypes.UNIT_ARTIST] = "delete",
    	[GameInfoTypes.UNIT_ENGINEER] = "delete", [GameInfoTypes.UNIT_GREAT_GENERAL] = "delete", [GameInfoTypes.UNIT_MERCHANT] = "keep",
    	[GameInfoTypes.UNIT_MUSICIAN] = "delete", [GameInfoTypes.UNIT_PROPHET] = "delete", [GameInfoTypes.UNIT_SCIENTIST] = "keep",
    	[GameInfoTypes.UNIT_WRITER] = "delete" }
    
    ------------------------------------------------------------------------------------------------
    --code to be left alone below
    ------------------------------------------------------------------------------------------------
    
    for iUnit,Data in pairs(tGreatPeopleTypesKeepOrDelete) do
    	local sUnitClass = GameInfo.Units[iUnit].Class
    	for UnitRow in GameInfo.Units("Class='" .. sUnitClass .. "'") do
    		if not tGreatPeopleTypesKeepOrDelete[UnitRow.ID] then
    			tGreatPeopleTypesKeepOrDelete[UnitRow.ID] = Data
    		end
    	end
    end
    function OnUnitCreated(iPlayer, iUnit, hexPos, iUnknown, cultureType, iCivID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
    	local pPlayer = Players[iPlayer]
    	local pUnit = pPlayer:GetUnitByID(iUnit)
    	if(pPlayer == nil or
    		pUnit == nil or
    		pUnit:IsDead()) then
    		return
    	end
    	local iUnitType = pUnit:GetUnitType()
    	if (pPlayer:GetCivilizationType() == iRequiredCivilization) and (tGreatPeopleTypesKeepOrDelete[iUnitType]) then
    		 if tGreatPeopleTypesKeepOrDelete[iUnitType] == "delete" then
    			pUnit:Kill()
    		end
    	end
    end
    Events.SerialEventUnitCreated.Add(OnUnitCreated)
  4. Note that I have shown examples of both "keeping" and "deleting" specified great people unit-types.
  5. All unique versions of Great People from within the same Unit-Class will be covered. Always state as the default unit in the class for this portion of the code:
    Code:
    local tGreatPeopleTypesKeepOrDelete = { [GameInfoTypes.UNIT_GREAT_ADMIRAL] = "delete", [GameInfoTypes.UNIT_ARTIST] = "delete",
    	[GameInfoTypes.UNIT_ENGINEER] = "delete", [GameInfoTypes.UNIT_GREAT_GENERAL] = "delete", [GameInfoTypes.UNIT_MERCHANT] = "keep",
    	[GameInfoTypes.UNIT_MUSICIAN] = "delete", [GameInfoTypes.UNIT_PROPHET] = "delete", [GameInfoTypes.UNIT_SCIENTIST] = "keep",
    	[GameInfoTypes.UNIT_WRITER] = "delete" }
    • All other units from within the same unit-classes will be filled-in automatically by the code.
    • The code can be used to control more than just whether Great People are kept or deleted. For example, you could make it delete all units from within the same class as the UNIT_MECH (ie, the G.D.R. unit). The game does not know or care within this code whether the unit is a great person or not: all it knows is that a unit is a unit specified for handling ("keep" or "delete") by the code.
    • Units from unit-classes that are not listed in this portion of the code will always be kept because the code has not been given any instructions for that particular unit-type.
 
Both methods seemed to work just fine. I guess I'll go with LeeS' version though. But I thank you both for your effort and help.
 

  1. Where is iCivID coming from? It is not stated in the list of arguments function "OnUnitCreated" is being passed from the gamecore when SerialEventUnitCreated fires, and is therefore always 'nil'.

  1. iCivID is an argument from "OnUnitCreated".
 
I was wondering if someone could program a Lua script for a Civ's UA that works like this:
Code:
+1 Global Happiness for each Civilization following the same Ideology as you. You are twice as effective at influencing other Civilizations of different Ideologies to switch to your Ideology. Captured cities spend half as much time in Resistance.

And then I was also wondering if anyone could program something like this as well:
Code:
When one of your units dies, a replacement unit has a 20% chance to appear outside the Capital.
 
Back
Top Bottom