I cannot get Lua to load for me mod at all

Captain_Russia

Chieftain
Joined
Sep 9, 2016
Messages
48
Ok first off, I hate joining site just to ask questions on my first post, but I cannot solve this problem for the life of me.
I've read everything I can but to no success, I followed the steps here http://forums.civfanatics.com/showthread.php?t=487846
I also left only print()'s in the Lua file and it STILL didn't load!

I am an experanced lua coder (a GMOD scripter), but I'm new to Civ5's stuff, and it's pulling my hairs out when it's not working. I've gotten XML's, Icons, and other things to work but not this.
o, here's what I got for the file, I'd like to point out the rest of the mod works fine.

Code:
print("Started Mental script")

local iCiv = GameInfoTypes.CIVILIZATION_MENTAL
local iCargo = GameInfoTypes.UNIT_CARGO_SHIP
local iCaravan = GameInfoTypes.UNIT_CARAVAN
local iPietyPolicy1 = GameInfoTypes.POLICY_ORGANIZED_RELIGION
local iPietyPolicy2 = GameInfoTypes.POLICY_REFORMATION
local iPietyBranch = GameInfo.PolicyBranchTypes["POLICY_BRANCH_PIETY"].ID

local iMentalPromotion = GameInfoTypes.PROMOTION_MENTAL_HOARD
local iMentalPower=0
--local policytable--policytable={}

-----policytable--policytable[0]=GameInfoTypes.POLICY_LIBERTY
---policytable--policytable[1]=GameInfoTypes.POLICY_COLLECTIVE_RULE
---policytable--policytable[2]=GameInfoTypes.POLICY_CITIZENSHIP
---policytable--policytable[3]=GameInfoTypes.POLICY_REPUBLIC
---policytable--policytable[4]=GameInfoTypes.POLICY_REPRESENTATION
---policytable--policytable[5]=GameInfoTypes.POLICY_MERITOCRACY
---policytable--policytable[6]=GameInfoTypes.POLICY_TRADITION
---policytable--policytable[7]=GameInfoTypes.POLICY_ARISTOCRACY
---policytable--policytable[8]=GameInfoTypes.POLICY_OLIGARCHY
---policytable--policytable[9]=GameInfoTypes.POLICY_LEGALISM
---policytable--policytable[10]=GameInfoTypes.POLICY_LANDED_ELITE
---policytable--policytable[11]=GameInfoTypes.POLICY_MONARCHY
---policytable--policytable[12]=GameInfoTypes.POLICY_HONOR
---policytable--policytable[13]=GameInfoTypes.POLICY_WARRIOR_CODE
---policytable--policytable[14]=GameInfoTypes.POLICY_DISCIPLINE
---policytable--policytable[15]=GameInfoTypes.POLICY_MILITARY_TRADITION
---policytable--policytable[16]=GameInfoTypes.POLICY_MILITARY_CASTE
---policytable--policytable[17]=GameInfoTypes.POLICY_PROFESSIONAL_ARMY
---policytable--policytable[18]=GameInfoTypes.POLICY_PIETY
---policytable--policytable[19]=GameInfoTypes.POLICY_ORGANIZED_RELIGION
---policytable--policytable[20]=GameInfoTypes.POLICY_MANDATE_OF_HEAVEN
---policytable--policytable[21]=GameInfoTypes.POLICY_THEOCRACY
---policytable--policytable[22]=GameInfoTypes.POLICY_REFORMATION
---policytable--policytable[23]=GameInfoTypes.POLICY_FREE_RELIGION
---policytable--policytable[24]=GameInfoTypes.POLICY_PATRONAGE
---policytable--policytable[25]=GameInfoTypes.POLICY_PHILANTHROPY
---policytable--policytable[26]=GameInfoTypes.POLICY_AESTHETICS
---policytable--policytable[27]=GameInfoTypes.POLICY_SCHOLASTICISM
---policytable--policytable[28]=GameInfoTypes.POLICY_CULTURAL_DIPLOMACY
---policytable--policytable[29]=GameInfoTypes.POLICY_EDUCATED_ELITE
---policytable--policytable[30]=GameInfoTypes.POLICY_COMMERCE
---policytable--policytable[31]=GameInfoTypes.POLICY_TRADE_UNIONS
---policytable--policytable[32]=GameInfoTypes.POLICY_NAVAL_TRADITION
---policytable--policytable[33]=GameInfoTypes.POLICY_MERCANTILISM
---policytable--policytable[34]=GameInfoTypes.POLICY_MERCHANT_NAVY
---policytable--policytable[35]=GameInfoTypes.POLICY_PROTECTIONISM
---policytable--policytable[36]=GameInfoTypes.POLICY_RATIONALISM
---policytable--policytable[37]=GameInfoTypes.POLICY_SECULARISM
---policytable--policytable[38]=GameInfoTypes.POLICY_HUMANISM
---policytable--policytable[39]=GameInfoTypes.POLICY_FREE_THOUGHT
---policytable--policytable[40]=GameInfoTypes.POLICY_SOVEREIGNTY
---policytable--policytable[41]=GameInfoTypes.POLICY_SCIENTIFIC_REVOLUTION



--This stuff was borrowed and modified from other Civ, in this case:
--Hakurei Shrine

-------------------------------------------------------------------------------------------------------------------------
-- GetStrongestMilitaryUnit
-------------------------------------------------------------------------------------------------------------------------
function GetStrongestMilitaryUnitStrengthM(pPlayer, bIgnoreResources, ...)
	local tUnit = {["Combat"] = 0}
	for iKey, sCombatType in pairs(arg) do
		for row in GameInfo.Units("CombatClass = \'" .. sCombatType .. "\'") do
			if pPlayer:CanTrain(row.ID, bIgnoreResources) and row.Combat > tUnit.Combat then
				tUnit = row
			end
		end
	end
	return tUnit.Combat
end

print("Mentals Lua: GetStrongestMilitaryUnitStrengthM")

function MentalStrength(pPlayer,iPower)
	if (pPlayer:IsAlive()) then
		for pUnit in pPlayer:Units() do
			if (pUnit:IsHasPromotion(iMentalPromotion)) then
				local iBaseCombatStrength = pUnit.Combat
				local iBaseRangedCombatStrength = pUnit.RangedCombat
				local iModifiedCombatStrength = math.ceil(iBaseCombatStrength * (1+(iPower/10)))
				local iModifiedRangedCombatStrength = math.ceil(iBaseRangedCombatStrength * (1+(iPower/10)))
				pUnit:SetBaseCombatStrength(iModifiedCombatStrength)
			end
		end	

			if pPlayer:IsHuman() then
		pPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, "This is a test: Unit Power at: "..iMentalPower, -1, -1);
	end
	
	end
end

print("Mentals Lua: MentalStrength")

function UpdateMentalPolicy(iPlayer)
print("Testing")
	local pPlayer = Players[iPlayer]
	if (pPlayer:IsAlive()) then
	iMentalPower=0
iMentalPower=pPlayer:GetNumPolicies()
print("Mental has "..iMentalPower.." policies")
MentalStrength(pPlayer,iMentalPower)
end
end

print("Mentals Lua: UpdateMentalPolicy")


GameEvents.PlayerAdoptPolicy.Add(UpdateMentalPolicy);
GameEvents.PlayerDoTurn.Add(UpdateMentalPolicy);

print("Mentals Lua: Hooks are loaded!")

print("Mentals Lua is loaded and ready!")

don't mind the all the comments, I didn't notice GetNumPolicies() existed when I wrote that, so I went for a harder approuch/work around
oh and the mod is for BNW
And I am sorry for this being my first post I just REALLY want to solve this problem!
 
We need the full mod (see second link in my sig), not just code snippets
 
Check how you've enabled logging (first link in my sig), as it works for me

Code:
[558051.218] MentalLua: Started Mental script
[558051.218] MentalLua: Mental's Lua: Hooks are loaded!
[558051.218] MentalLua: Mental's Lua is loaded and ready!
 
Well, I guess it's good to know the mod is loading the Lua after all!
But even then, the prints wouldn't be recorded to the log OR show up on fine tuner.
But if it did work, that must mean there's a syntax error in the coding itself.
Ok first, I sent the version that bare prints, here's the one with the coding snippet mentioned above: https://www.dropbox.com/s/t7gld37yyt6txi1/Mentals_Forces (v 1).zip?dl=0

I have enabled Logging, but maybe I missed something, please help me debug this :)
 
Syntax errors:
Code:
local iBaseCombatStrength = [COLOR="Red"]pUnit.Combat[/COLOR]
local iBaseRangedCombatStrength = [COLOR="red"]pUnit.RangedCombat [/COLOR]
Variable pUnit is a Pointer or Object to a specific Unit and needs to be indexed using ":" structuring with valid "Unit" methods as outlined here. Combat and RangedCombat are columns from within the <Units> XML-table rather than methods within the "Unit" portion of the API.

This code:
Code:
local iBaseCombatStrength = pUnit.Combat
only works when pUnit has been set to the value for a particular <Row> from within table <Units>, as like this:​
Code:
local pUnit = GameInfo.Units["UNIT_BATTLESHIP"]
local iBaseCombatStrength = pUnit.Combat
But then that would only give you the information for that particular unit-row definition from within the <Units> table. Plus then there's the issue of confusion at that point when reading the code as to whether "pUnit" is being used as a proper object for an individula unit within the game, or whether it is a table holding all the data for that one specific XML <Row> from within table <Units>.

If you were wanting to grab info from the <Units> xml table for that unit-type, what you want would be like as:​
Code:
function MentalStrength(pPlayer,iPower)
	if (pPlayer:IsAlive()) then
		for pUnit in pPlayer:Units() do
			if (pUnit:IsHasPromotion(iMentalPromotion)) then
				local tUnitData = GameInfo.Units[pUnit:GetUnitType()]
				local iBaseCombatStrength = tUnitData.Combat
				local iBaseRangedCombatStrength = tUnitData.RangedCombat
				local iModifiedCombatStrength = math.ceil(iBaseCombatStrength * (1+(iPower/10)))
				local iModifiedRangedCombatStrength = math.ceil(iBaseRangedCombatStrength * (1+(iPower/10)))
				pUnit:SetBaseCombatStrength(iModifiedCombatStrength)
			end
		end	
		if pPlayer:IsHuman() then
			pPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, "This is a test: Unit Power at: "..iMentalPower, -1, -1);
		end
	end
end

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

If you were not getting "runtime" errors in the lua.log file related to "indexing" errors of one kind or another, I suspect you have not yet enabled logging correctly.

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

editedit -- I'm also a little confused by what is going on in function GetStrongestMilitaryUnitStrengthM but it might just be a different way of doing things that I've not tried to use. Everybody seems to have a different way to skin the lua cat. IE, I don't get where arg is coming from nor being defined.
 
Syntax errors:
Code:
local iBaseCombatStrength = [COLOR="Red"]pUnit.Combat[/COLOR]
local iBaseRangedCombatStrength = [COLOR="red"]pUnit.RangedCombat [/COLOR]
Variable pUnit is a Pointer or Object to a specific Unit and needs to be indexed using ":" structuring with valid "Unit" methods as outlined here. Combat and RangedCombat are columns from within the <Units> XML-table rather than methods within the "Unit" portion of the API.

This code:
Code:
local iBaseCombatStrength = pUnit.Combat
only works when pUnit has been set to the value for a particular <Row> from within table <Units>, as like this:​
Code:
local pUnit = GameInfo.Units["UNIT_BATTLESHIP"]
local iBaseCombatStrength = pUnit.Combat
But then that would only give you the information for that particular unit-row definition from within the <Units> table. Plus then there's the issue of confusion at that point when reading the code as to whether "pUnit" is being used as a proper object for an individula unit within the game, or whether it is a table holding all the data for that one specific XML <Row> from within table <Units>.

If you were wanting to grab info from the <Units> xml table for that unit-type, what you want would be like as:​
Code:
function MentalStrength(pPlayer,iPower)
	if (pPlayer:IsAlive()) then
		for pUnit in pPlayer:Units() do
			if (pUnit:IsHasPromotion(iMentalPromotion)) then
				local tUnitData = GameInfo.Units[pUnit:GetUnitType()]
				local iBaseCombatStrength = tUnitData.Combat
				local iBaseRangedCombatStrength = tUnitData.RangedCombat
				local iModifiedCombatStrength = math.ceil(iBaseCombatStrength * (1+(iPower/10)))
				local iModifiedRangedCombatStrength = math.ceil(iBaseRangedCombatStrength * (1+(iPower/10)))
				pUnit:SetBaseCombatStrength(iModifiedCombatStrength)
			end
		end	
		if pPlayer:IsHuman() then
			pPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, "This is a test: Unit Power at: "..iMentalPower, -1, -1);
		end
	end
end

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

If you were not getting "runtime" errors in the lua.log file related to "indexing" errors of one kind or another, I suspect you have not yet enabled logging correctly.

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

editedit -- I'm also a little confused by what is going on in function GetStrongestMilitaryUnitStrengthM but it might just be a different way of doing things that I've not tried to use. Everybody seems to have a different way to skin the lua cat. IE, I don't get where arg is coming from nor being defined.

Oh, I See how I messed up now.

the GetStrongestMilitaryUnitStrengthM() already acquired the tabledata and I missed that part.

I actvated those 2 things whoward69 said to to enable logging and I still didn't get any logs then

A I mentioned before, and credited, the lua for GetStrongestMilitaryUnitStrengthM() was borrowed from the Touhou - Hakurei Shrine Civ, it has a code that get's the strongest trainable unit, and the arg thing is rather complicated but it works; I renamed the function and added an M to the end just in case function names conflict with one another in Civ 5 (just a programming precaution)

The second function was also borrowed but modified, and I clearly messed it up :p

The final function was written from scratch.

Yeah, I'm good with Lua but I'm new to all the quirks Civ 5 abides by so it makes sense how I missed this.
 
my issue with this
Code:
for iKey, sCombatType in pairs(arg) do
is that it wants to be an iteration through an lua table called "arg" but I don't see either where you have defined such a table nor passed it to function GetStrongestMilitaryUnitStrengthM. For that matter I don't see anywhere you have actually used function GetStrongestMilitaryUnitStrengthM.

[edit] I said "issue" but I really should have said "confusion"
 
my issue with this
Code:
for iKey, sCombatType in pairs(arg) do
is that it wants to be an iteration through an lua table called "arg" but I don't see either where you have defined such a table nor passed it to function GetStrongestMilitaryUnitStrengthM. For that matter I don't see anywhere you have actually used function GetStrongestMilitaryUnitStrengthM.

[edit] I said "issue" but I really should have said "confusion"

That's because I'm not using it yet.

And as I mentioned, I didn't write that, it came from another Civ, and this is how it's used:

local iBaseCombatStrength = GetStrongestMilitaryUnitStrength(pPlayer, false, "UNITCOMBAT_MELEE", "UNITCOMBAT_GUN")

from the Hakurei Shrine Civ's lua file.
I'ts really confusing, it puzzled me for a moment, but it does work.

here's the source civ in case you wanna see for yourself: http://steamcommunity.com/sharedfiles/filedetails/?id=721710206&searchtext=shrine
 
I actvated those 2 things whoward69 said to to enable logging and I still didn't get any logs then

Then you need to debug the debugging first. Without logs, debugging any XML/SQL/Lua errors is going to be VERY time consuming and painful.

What version of the game is it?
 
Then you should be getting various log files under ~Documents\My Games\Sid Meier's Civilization 5\Logs - the four important ones are database.log, lua.log, xml.log and stopwatch.log - without those modding is painfully slow
 
Then you should be getting various log files under ~Documents\My Games\Sid Meier's Civilization 5\Logs - the four important ones are database.log, lua.log, xml.log and stopwatch.log - without those modding is painfully slow

Ugh tell me about it, modding would be impossible without debug tools!
So um, the Lua one didn't even report an error for my file, if the lua script fails to load for some reason, would it say anything in the other files?

Also is there a file that saves the currently activated mods? I want to try to test my mod it without any other mods, but I want an easier way to reactivate them, since trying to load a modded game and let it do it automatically often crashes.
 
Are you not getting any log files or are you getting log files that don't appear to contain your print output?
 
Are you not getting any log files or are you getting log files that don't appear to contain your print output?

I'm getting logs alright, from the game and from other mods.
But not my own script, not even when it was point basic.

I haven't tested it yet since I made the changes Lee posted, hopefully it'll work now, or atleast show some logs :/
 
Sounds like you're confused as to when the print output will appear. The game core does not execute a mod's Lua files until AFTER the map has been initialised ... so you will need to select your civ in the setup screens after the Mod Browser screen and then start a game. If you watch the lua.log file, you'll see all the map generation stuff and then the output from your lua prints
 
Sounds like you're confused as to when the print output will appear. The game core does not execute a mod's Lua files until AFTER the map has been initialised ... so you will need to select your civ in the setup screens after the Mod Browser screen and then start a game. If you watch the lua.log file, you'll see all the map generation stuff and then the output from your lua prints

I've seen the other mods output their intit prints() (eg Equestria) but otherwise I dodn't see any of mine.

My question is, if script fails at anypoint, does the even the first prints fail too? Because I still wasn't seeing ANYTHING related to errors on my file in the logs
 
I see what is going on now, I just tend to do as:
Code:
function GetStrongestMilitaryUnitStrength(pPlayer, bIgnoreResources, ...)
	local tUnit = {["Combat"] = 0}
	for iKey, sCombatType in pairs({...}) do
		for row in GameInfo.Units("CombatClass = \'" .. sCombatType .. "\'") do
			if pPlayer:CanTrain(row.ID, bIgnoreResources) and row.Combat > tUnit.Combat then
				tUnit = row
			end
		end
	end
	return tUnit.Combat
end
And apparently in newer versions of lua the "arg" thing has been deprecated except for purposes of back-compatibility. It looks like in lua5.2 (as I recall Civ5 uses lua5.1) it may be necessary to do as:
Code:
function GetStrongestMilitaryUnitStrength(pPlayer, bIgnoreResources, ...)
	local tUnit = {["Combat"] = 0}
	local arg = {...}
	for iKey, sCombatType in pairs(arg) do
		for row in GameInfo.Units("CombatClass = \'" .. sCombatType .. "\'") do
			if pPlayer:CanTrain(row.ID, bIgnoreResources) and row.Combat > tUnit.Combat then
				tUnit = row
			end
		end
	end
	return tUnit.Combat
end
Which is essentially what I tend to do anyway.

Though I've never gotten that second parameter "bIgnoreResources" for Player:CanTrain(row.ID, bIgnoreResources) to actually do anything so far as whether the player has the required resources for the unit, regardless of whether set "true" or set "false". Not sure that is what that parameter position is for.

William's API shown as XML merely lists that 2nd argument of Player:CanTrain() as being "bContinue":
Code:
  <api object="Player" method="CanTrain">
      <arg pos="2" type="UnitTypes" name="eUnit"/>
      <arg pos="3" type="bool" name="bContinue"/>
      <arg pos="4" type="bool" name="bTestVisible"/>
      <arg pos="5" type="bool" name="bIgnoreCost"/>
      <arg pos="6" type="bool" name="bIgnoreUniqueUnitStatus"/>
      <ret type="bool"/>
  </api>
The final parameter "bIgnoreUniqueUnitStatus" does as far as I can tell correctly check for whether the unit is a unique unit appropriate to that player.

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

Just for future reference, "William" is whoward69. He prefers William to stuff like who, who-ward, whoward, Howard, Lord God of all Civ5 Modding, etc. when speaking "to" him on the forum.

[edit]
Spoiler :
He really objects to being called etc. :)
 
I've seen the other mods output their intit prints() (eg Equestria) but otherwise I dodn't see any of mine.

My question is, if script fails at anypoint, does the even the first prints fail too? Because I still wasn't seeing ANYTHING related to errors on my file in the logs

If your lua script fails to load because of a fatal error such as syntax, you will get an error message in the lua log (both in the lua.log file and in the "realtime" LiveTuner console) reporting this error and generally the line number of the error within the file will be given. You will not in such a case see anything else from your lua file, including all print statements. You will only get such an error message, though, if the game attempts to load your code and fails to successfully load it.

If you are seeing nothing, not even print statements, then either the file is not setup correctly in Modbuddy, or you do not have logging enabled correctly, or you are not looking in the correct error-logging file. I've seen people trying to check for error messages in the game's base game-file folders rather than in the folder C:\Users\YourComputerUserNameHere\Documents\My Games\Sid Meier's Civilization 5\Logs. If you aren't seeing anything (even the output of print statements) in LiveTuner (also called Fire Tuner) but are seeing messages for other lua files from other mods, this would make me tend to want to check that the file is set up correctly in your mod in ModBuddy.
 
If your lua script fails to load because of a fatal error such as syntax, you will get an error message in the lua log (both in the lua.log file and in the "realtime" LiveTuner console) reporting this error and generally the line number of the error within the file will be given. You will not in such a case see anything else from your lua file, including all print statements. You will only get such an error message, though, if the game attempts to load your code and fails to successfully load it.

If you are seeing nothing, not even print statements, then either the file is not setup correctly in Modbuddy, or you do not have logging enabled correctly, or you are not looking in the correct error-logging file. I've seen people trying to check for error messages in the game's base game-file folders rather than in the folder C:\Users\YourComputerUserNameHere\Documents\My Games\Sid Meier's Civilization 5\Logs. If you aren't seeing anything (even the output of print statements) in LiveTuner (also called Fire Tuner) but are seeing messages for other lua files from other mods, this would make me tend to want to check that the file is set up correctly in your mod in ModBuddy.

Yeah that's the thing right there, though I've followed all the instructions (logging, adding the lua file as content) and whoward69 said all the prints were being prints. I still didn't see anything myself.

Normally if the lua fails to load at anypoint I'd see an error, but I don't see anything at all.

you know, I posted to the updated file with the full lua, maybe you could help me debug it? Please?

EDIT: just tested with the updated lua, still not working at all, nothing :mad:
 
Top Bottom