Need help on approaching Leader Trait coding.

TheRandor69

Chieftain
Joined
Jun 24, 2015
Messages
31
Hello Civ Fanatics Community!

So I've started working on my first mod, and everything is going smooth, I've finished the Unit and the Building but I'm stuck when it comes to the leader trait.
I have no idea how to do this or where to start.

Basically I want the civilization to receive a 10% Science bonus for every Declaration of War. and get an additional 5 science for every enemy unit killed.

Is this possible?
Where should I start?
Can somebody help me out?

Thanks in advance!
 
Unfortunately there's no ScienceFromKills column under the leader <Traits> table, so you would need lua most likely for that. There is a Firaxis-provided hook GameEvents.UnitKilledInCombat(PlayerID killer, PlayerID killee, UnitType killeeUnitType) that will let you detect when a unit is killed in combat. The 1st argument shown in red is the PlayerID# of the killer, the second argument shown in red is the PlayerID# of the killee (dead unit), and the final argument shown is the UnitType of the killeeUnitType (the dead unit).

The declarations of war you would have to arrive at also via lua using the "Team" method Team:GetAtWarCount(bool ignoreMinors). You would most likely use this within a GameEvents.PlayerDoTurn(iPlayer) add-on function. You would have to translate the Player info into a Team info and then you could use the Team:GetAtWarCount(bool ignoreMinors) method to determine # teams a player is at war with.
 
Oh wow that's actually still pretty complicated ^^"
I don't know if I can pull that off without some base code to base it off of..
I'll give it a shot.

Thanks!
 
I'm sorry I've tried but I just can't get it to work ;-;
It sounds so simple on paper but I can't get it to work

I don't understand why it's so difficult for me ._.

I'm not really that good at LUA it seems. Could you perhaps help me a bit with the programming/implementation?
 
So, I've been working off someone else's mod to learn the ropes and I've come up with the following:

Code:
function SafavidWarTimeEffects(playerID)
	local pPlayer = Players[playerID]
	if (pPlayer:IsAlive()) then
--		print('pPlayer identified.')
		if (pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_AW_SAMANID) then
		if Teams[pPlayer:GetTeam()]:GetAtWarCount(true) > 0 then
			if not pPlayer:HasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"]) then
				pPlayer:SetNumFreePolicies(1)
				pPlayer:SetNumFreePolicies(0)
				pPlayer:SetHasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"], true)
			end
		else
			if pPlayer:HasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"]) then
				pPlayer:SetHasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"], false)
			end
		end
        end
	end
end

GameEvents.PlayerDoTurn.Add(SafavidWarTimeEffects)

and the policy is:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by ModBuddy on 8/4/2014 8:08:26 PM -->
<GameData>
	<Policies>
		<Row>
			<Type>POLICY_SAFAVID_DUMMY</Type>
		</Row>
	</Policies>
	<Policy_YieldModifiers>
		<Row>
			<PolicyType>POLICY_SAFAVID_DUMMY</PolicyType>
			<YieldType>YIELD_SCIENCE</YieldType>
			<Yield>200</Yield>
		</Row>
	</Policy_YieldModifiers>	
</GameData>

But it doesn't seem to work ._.
 
Is your civilization's <Type> value CIVILIZATION_AW_SAMANID ?

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

Is the lua file set up as an InGameUIAddin, as shown how to do in Post #3 here: whoward69's what ModBuddy setting for what file types tutorial

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

Also, you should generally at least provide a <Description> for a policy, even if the policy is a dummy. Generally I provide at least as a minimum, the following in the XML:
Code:
	<Policies>
		<Row>
			<Type>POLICY_UNHAPPY_001</Type>
			<Description>TXT_KEY_POLICY_UNHAPPY_001</Description>
			<PortraitIndex>24</PortraitIndex>
			<IconAtlas>POLICY_ATLAS</IconAtlas>
			<IconAtlasAchieved>POLICY_A_ATLAS</IconAtlasAchieved>
		</Row>
	</Policies>
	<Language_en_US>
		<Row Tag="TXT_KEY_POLICY_UNHAPPY_001">
			<Text>001 Unhappy</Text>
		</Row>
	</Language_en_US>
Not providing a <Description> tag for any game-table that has a column for <Description> is a really bad habit to get into. Even though you may have copied everything as a starting template from AW, don't get into bad habits others have allowed themselves to drift into. Too many game elements (units, policies, buildings, promotions, etc.) cause really bizarre or game-crashing/game-locking behaviors in the correct circumstances when a <Description> tag is omitted. Techs also seem to want a <Help> tag in order to behave nicely, but lack of a <Help> designation has not caused me troubles anywhere else besides techs where I have omitted it.
 
I'm pretty sure that the <type> tag has indeed the value CIVILIZATION_AW_SAMANID.

I will also make sure to include a description, I'll try to do that now.

As for the InGameUIAddin, I'm not sure, I haven't made this in some sort of solution I just kinda copied and pasted files around since I couldn't find a solution file to work off of. I have one now so I'll make sure to work in a sollution for my next mod, but I think it should be okay?

Code:
    <File md5="0772D00B287AB249DEA6F4B4CB044578" import="0">GameInfo/Safavid_Policies.xml</File>
    <File md5="45D970E572719EB1C786F6DC3D94320E" import="0">LUA/SamanidTrait_Culture.lua</File>

This is what the .modinfo file says.
 
I'm pretty sure that the <type> tag has indeed the value CIVILIZATION_AW_SAMANID.
I'm sure that is how Aw has designated his civilization, but is the tag correct for your civilization ?

I will also make sure to include a description, I'll try to do that now.

As for the InGameUIAddin, I'm not sure, I haven't made this in some sort of solution I just kinda copied and pasted files around since I couldn't find a solution file to work off of. I have one now so I'll make sure to work in a sollution for my next mod, but I think it should be okay?

Code:
    <File md5="0772D00B287AB249DEA6F4B4CB044578" import="0">GameInfo/Safavid_Policies.xml</File>
    <File md5="45D970E572719EB1C786F6DC3D94320E" import="0">LUA/SamanidTrait_Culture.lua</File>

This is what the .modinfo file says.
Insofar as all that goes it is correct, but it is insufficient. Your modinfo file also needs to have one of these in order to get the lua to work:
Code:
  <Actions>
    <OnModActivated>
      <UpdateDatabase>GameInfo/Safavid_Policies.xml</UpdateDatabase>
    </OnModActivated>
  </Actions>
  <EntryPoints>
    <EntryPoint type="InGameUIAddin" file="LUA/SamanidTrait_Culture.lua">
      <Name>SomeName</Name>
      <Description>SomeDescription</Description>
    </EntryPoint>
  </EntryPoints>
When everything is set-up correctly in ModBuddy, the 'Build' and 'Build Solution' functions of ModBuddy will create the entire mod for you, including the correct commands for the modinfo file. The modinfo file itself will also be created as an integral part of creating the mod.
 
I actually have not ever changed any names of any files, all I've done is change code inside the files themselves. So it should work, right?

I checked and the OnModActivated and Entrypoints are all in their correct place and in the modinfo file.
 
Is the player at war with a major civilization ?

The way you have the GetAtWarCount(true) specified, the "true" part is telling lua to ignore any wars with city-states.

I just tested the same methodology and effect using a generic mod I use for testing lua elements, and the science boost occured just fine after I declared war on a major civilization. Also, since the lua code is hooked to run as part of the PlayerDoTurn turn processing "GameEvent", you have to click NEXT TURN and let the game process to the next turn before any effects will be implimented.
 
I've just tried to declare war on a few major civs, but it doesn't seem to be affecting anything.

But it SHOULD work, right?
What do you propose might be the problem?
 
It should work if everything is properly implimented within the mod. My guess without the actual mod/code to look at is an improper implimentation somewhere.

Check this sometime later today or tomorrow (depends on when I will have time to expand with more instructions the OP).
 
I could PM you the mod? Also, does the md5 idcode matter in any way shape or form?

I will check that out later today looks pretty helpful!
 
By the way, do you know whether there is a way to get a similar thing going just as sweden? Where you get x% bonus for EACH declaration of war with a major civ?
 
You basic problem(s):
  • Mismatched file path-names between what is actually in the mod structure and what is designated in the modinfo file
  • There appear to be hidden text-control characters in the lua file. ie, it does not seem while editing the file that all the characters within the file are text-only.
  • there are bunches of syntax and other errors being reported in the lua.log coming from the other lua files that are part of the mod
 
Oh dear o.o
Do you know how to fix the first thing atleast? Because I've checked and it really seems like the structure and the modinfo file are correct o.O
I've checked everything for atleast the Science multiplier mod. Am I overlooking the error? o.o

Strange because in my Lua editor no strange characters show up apart from some dashes for comments?

Yeah I think some of the lua files I've fiddled with don't work anymore because I've screwed them up.
How did you see all of this? Is there an ingame devconsole or something?
 
whoward69's enable error logging tutorial

After error logging is enabled you generally want to look at the lua.log file for any issues related to lua files (obviously), and for XML and SQL files you want 1st to look in the Database.log file. The Databsae.log file reports errors encountered when adding or altering stuff into the game's XML/SQL database.

The file paths specified in the modinfo file do not match with the mod's actual structure. It must be an exact match (including upper-lower-case) between what is presented in the modinfo file and the actual structure of the mod.
Code:
    <File md5="19C0248CD3B8126ABDF89A75EB7A3516" import="0">[COLOR="red"]Lua/[/COLOR]IncreasedWarProduction.lua</File>

    <EntryPoint type="InGameUIAddin" file="[COLOR="Red"]Code/Lua/[/COLOR]IncreasedWarProduction.lua">
      <Name>IncreasedWarProduction</Name>
      <Description>IncreasedWarProduction</Description>
    </EntryPoint>

I never use any special "program-editor" program. I always use only a text editor (notepad in fact). I also actually never edit my files from within ModBuddy. I just use ModBuddy to create the files I will need and then exclusively I edit in Notepad from there. You have far more powerful text editing options with a simple text editor than you do with Modbuddy, plus you do not have the penalty of needing to worry about whether you have specifically told the editor program to save as direct-text only formatting.

Also, the package that ModBuddy is a component of includes LiveTuner, which is a run-time lua console. I am not all that versed in the use of LiveTuner, but at a minimum you can use it to view all the errors and print statements being sent to the lua.log file. Adding pring statements for purpose of debugging can be a godsend because otherwise you are left guessing as to what parts of your code (if any) are actually being executed at any given time.
 
Alright I'm at a pretty desperate point right now. I have no idea whether I'm even doing things right or not anymore.

I've ran the logs:
https://dl.dropboxusercontent.com/u/22015002/Logs.zip

And I'm not getting any feedback about anything regarding the mod. I have no idea if things are supposed to be working or anything. I've changed the modinfo file again with the help you provided but it doesn't seem to do anything.
 
Most of that hash is coming from having the EUI and the IGE mods activated at the same time, along with having errors in those mod-compatibility files.

IGE tends to print a lot of lines into the lua.log anyway, and only seems to get worse with EUI. Also there are some compatibility issues between EUI and IGE which may be adding to the mix. I do not use EUI anymore because of these sorts of issues, plus I just decided there was too much clutter everywhere coming from it.

These were spit into my lua.log on game loading:
Code:
[color="blue"][368968.109] GhilmanBonuses_NearEnemyCapital: Loaded = GhilmanBonuses_NearEnemyCapital
[368968.109] IncreasedWarProduction: IncreasedWarProduction.lua loaded all the way to the end[/color]
[color="red"][368968.125] Syntax Error: C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\LUA/SamanidTrait_CityConnections.lua:37: unexpected symbol near ')'
[368968.125] Runtime Error: Error loading C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\LUA/SamanidTrait_CityConnections.lua.[/color]
[color="blue"][368968.140] SamanidTrait_Culture: Loaded = SamanidTrait_Culture.lua[/color]
[color="red"][368968.140] Runtime Error: C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\Compatibility\Events and Decisions\AW_Samanid_Decisions.lua:13: attempt to call global 'HookDecisionCivilizationIcon' (a nil value)
[368968.140] Runtime Error: Error loading C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\Compatibility\Events and Decisions\AW_Samanid_Decisions.lua.
[368968.156] Runtime Error: C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\Compatibility\Events and Decisions\AW_Samanid_Events.lua:140: attempt to call global 'Events_AddCivilisationSpecific' (a nil value)
[368968.156] Runtime Error: Error loading C:\Users\Lee\Documents\My Games\Sid Meier's Civilization 5\MODS\RANDOR TEST\Compatibility\Events and Decisions\AW_Samanid_Events.lua.[/color]
This came after I pressed the "NEXT TURN" button:
Spoiler :
These are all normal and 'good' because of all the debugging print statements I placed into the lua file.
Code:
[369043.468] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 1
[369043.468] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369043.578] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 2
[369043.578] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369044.078] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 3
[369044.093] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369044.546] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 4
[369044.546] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369044.984] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 5
[369044.984] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369045.484] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 6
[369045.484] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369045.859] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 7
[369045.859] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369046.296] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 8
[369046.296] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369046.796] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 9
[369046.796] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369047.171] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 10
[369047.171] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369047.671] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 11
[369047.671] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369048.234] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 22
[369048.234] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369048.671] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 23
[369048.671] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369049.015] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 24
[369049.031] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369049.328] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 25
[369049.328] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369049.656] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 26
[369049.656] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369050.046] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 27
[369050.046] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369050.343] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 28
[369050.343] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369050.750] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 29
[369050.750] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369051.046] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 30
[369051.046] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369051.437] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 31
[369051.437] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369051.796] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 32
[369051.796] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369052.156] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 33
[369052.156] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369052.531] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 34
[369052.531] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369052.921] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 35
[369052.921] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369053.312] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 36
[369053.312] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369053.750] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 37
[369053.750] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369054.000] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 38
[369054.000] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369054.375] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 39
[369054.375] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369054.734] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 40
[369054.734] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369055.031] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 41
[369055.031] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369055.343] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 42
[369055.343] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369055.750] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 43
[369055.750] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369056.109] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 44
[369056.109] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369056.406] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 45
[369056.406] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID
[369056.750] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 63
[369056.750] IncreasedWarProduction: player is NOT CIVILIZATION_AW_SAMANID

[color="blue"][369059.250] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 0
[369059.250] IncreasedWarProduction: player is CIVILIZATION_AW_SAMANID
[369059.250] IncreasedWarProduction: player is NOT at war with any major civilization[/color]
Now compare to what I got in place of the final few lines after I made Ahmad Al_Mansur declare war on me:
Code:
[369119.765] IncreasedWarProduction: SafavidWarTimeEffects is running for playerID of 0
[369119.781] IncreasedWarProduction: player is CIVILIZATION_AW_SAMANID
[369119.781] IncreasedWarProduction: player is at war with at least one major civilization
[369119.781] IncreasedWarProduction: player does NOT have POLICY_SAFAVID_DUMMY
[369119.781] IncreasedWarProduction: the policy will be added for the player
This confirms the code is running correctly, and I was able to see the difference in the Science being generated by the civ.

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

The 'fixed' version of the mod for the issues related to the one file: http://www.mediafire.com/download/xs2bpdui858rqlr/RANDOR_TEST.zip

See especially my changes to the modinfo file and the changes made within the file IncreasedWarProduction.lua
 
Oh man it worked! Thank you so much!
Now there's only one more thing I need help with. I've got a bit of code which checks how many AI's the player is at war with, but I'm struggling to combine it with the IncreasedWarScience solution so that it gives the policy for every warcount.

Could you perhaps take a look at it and tell me how I could do it?

Warcount:

Code:
function JFD_GetNumWars(playerID)
	local player = Players[playerID]
	local numWars = 0
	for otherPlayerID = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
		local otherPlayer = Players[otherPlayerID]
		if (otherPlayer:IsAlive() and otherPlayerID ~= playerID) then
			if Teams[player:GetTeam()]:IsHasMet(otherPlayer:GetTeam()) then
				if Teams[otherPlayer:GetTeam()]:IsAtWar(player:GetTeam())) then
					numWars = numWars + 1
				end
			end
		end
	end

	return numWars
end

IncreasedWarScience:

Code:
function SafavidWarTimeEffects(playerID)

	local player = Players[playerID]

	print("SafavidWarTimeEffects is running for playerID of " .. playerID)
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_AW_SAMANID"] and player:IsEverAlive() then

		print("player is CIVILIZATION_AW_SAMANID")
		if Teams[player:GetTeam()]:GetAtWarCount(true) > 0 then

			print("player is at war with at least one major civilization")
			if not player:HasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"]) then

				print("player does NOT have POLICY_SAFAVID_DUMMY")
				print("the policy will be added for the player")
				player:SetNumFreePolicies(1)

				player:SetNumFreePolicies(0)

				player:SetHasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"], true)

			end

		else

			print("player is NOT at war with any major civilization")
			if player:HasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"]) then

				print("player had the POLICY_SAFAVID_DUMMY but it will now be removed")
				player:SetHasPolicy(GameInfoTypes["POLICY_SAFAVID_DUMMY"], false)

			end

		end

	else
		print("player is NOT CIVILIZATION_AW_SAMANID")
	end

end



GameEvents.PlayerDoTurn.Add(SafavidWarTimeEffects)

print("IncreasedWarProduction.lua loaded all the way to the end")
 
Back
Top Bottom