Non functional event/lua

CivilizationAce

Warlord
Joined
Jan 17, 2017
Messages
240
So having got all my ducks in a row (or so I thought) I added a .lua file to my mod. I added it to the in-Game Actions — AddGameplayScripts. However, not even the first print statement result appears in the Lua Console when I take a barbarian encampment. Here are the relevant parts of the code:
Code:
function OnLoadScreenClose()
    Events.ImprovementActivated.Add(OnImprovementActivated)
end

function OnImprovementActivated(iX, iY, iPlayer, moot1, iImprovementIndex, moot2, moot3, moot4)
    print("OnImprovementActivated start")
    pPlayer ~= -1 and Players[iPlayer] or Map.GetPlot(iX,iY):GetOwner()
    if iImprovementIndex == GameInfo.Improvements["IMPROVEMENT_BARBARIAN_CAMP"].Index then
        print("About to GenerateUnit")
        GenerateUnit(pPlayer,1)        -- 1/30 in final build
    end
end

Events.LoadScreenClose.Add(OnLoadScreenClose)
Anyone got any idea about what I've done wrong please?
 
1. Any errors in Lua.log? 2. What is the line after print and before if supposed to do? 3. Put print into OnLoadScreenClose so you will know that you got there. 4. Just in case, put entire OnImprovementAcivated function before OnLoadScreenClose. It's really bad practice to put functions calls before actual function definition.
 
looks like you were wanting this but just oopsed
Code:
local pPlayer = ((iPlayer == -1) and Players[Map.GetPlot(iX,iY):GetOwner()] or Players[iPlayer])
like as this
Code:
local iBarbCamp = GameInfo.Improvements["IMPROVEMENT_BARBARIAN_CAMP"].Index
function OnImprovementActivated(iX, iY, iPlayer, moot1, iImprovementIndex, moot2, moot3, moot4)
	print("OnImprovementActivated start")
	if iImprovementIndex == iBarbCamp then 
		print("Barbarian Camp Cleared")
		local pPlayer = ((iPlayer == -1) and Players[Map.GetPlot(iX,iY):GetOwner()] or Players[iPlayer])
	        print("About to GenerateUnit")
		GenerateUnit(pPlayer,1)        -- 1/30 in final build
	else
		print("The Improvement was not a Barbarian Camp")
	end
end
function OnLoadScreenClose()
    Events.ImprovementActivated.Add(OnImprovementActivated)
end
Events.LoadScreenClose.Add(OnLoadScreenClose)
You just have to make sure the code within function GenerateUnit(arg1,arg2) is written to recieve a player object rather than a player ID # as arg1
 
Back
Top Bottom