What happens when LUA cannot do something?

Enginseer

Salientia of the Community Patch
Supporter
Joined
Nov 7, 2012
Messages
3,674
Location
Somewhere in California
Say there is LUA coding that creates Dummy1 and Dummy2 automatically in the capital.

If Dummy1 never existed in the XML files, would the LUA coding still spawn Dummy2 in the capital, pretending it's not there, and continue with the rest of the coding, or would the game throw away the LUA file like it does with XML?
 
Depends what the code does and how it's used. If you're getting a value and then using it assuming it's present, you'll get a "nil value" error and the code will terminate.

But, you can write code to detect the missing building and take appropriate action - eg if the code is in an event handler, only add the handler if the building is in the game.
 
Depends entirely on the usage within the lua script.[edit] As whoward has already noted. Reading comprehension -- it used to be such a thing :sad:

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


Code:
local iBuildingID = GameInfoTypes["BUILDING_GORSNITCHTS"]
function OPnCityFounded(iPlayer, iCityX, iCityY)
	local pPlayer = Players[iPlayer]
	if not pPlayer:IsHuman() then return end
	local pCityPlot = Map.GetPlot(iCityX, iCityY)
	local pCity = pCityPlot:GetPlotCity()

	--add a free founded city building
	pCity:SetNumRealBuilding(iBuildingID, 1)
end
GameEvents.PlayerCityFounded.Add(OPnCityFounded)
You get Floating Gardens in the city

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

Code:
local iBuildingID = GameInfoTypes["BUILDING_GORSNITCHTS"]
function TestGorsnitchts(iPlayer)
	print("the ID# of BUILDING_GORSNITCHTS is :")
	print(iBuildingID)
end
GameEvents.PlayerDoTurn.Add(TestGorsnitchts)
You get output such as this to the lua log
Code:
[316979.328]Lua Script1: the ID# of BUILDING_GORSNITCHTS is :
[316979.328]Lua Script1: nil

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

A print statement such as this
Code:
print("the ID# of BUILDING_GORSNITCHTS is " .. iBuildingID)
gives a Runtime Error for trying to concatenate a nil value
 
Back
Top Bottom