First steps in Lua

Bosparan

Chieftain
Joined
Oct 27, 2014
Messages
23
Hello,

I've recently been trying to augment an XML mod of mine with some Lua:
It is a simple Mod Test Kit ("Mod Test Helper" in the Steam Workshop), that grants some light-weight, low impact tools to test mods (FireTuner is a bit heavy metal for some repeated runs at a minor Xml mod). All it really does right now is add some additional buildings. Now I'd like a way to get rid of those buildings again. Easier said than done, since somehow you can't sell/demolish buildings.

Which left me with the idea that Lua might be able to do something for me.

Why not have a check run every turn for player-cities that meet a certain criterion (say: Another building that serves as flag for demolishing), and then demolishes all the buildings my mod adds to the game?
Sounded like a good idea, but I have absolutely no experience doing Lua. Reading up on reference material - no tutorials for CivBE yet, but plenty for Civ 5, which appears to be fairly similar - I happily started my quest for building annihilation.
Some frustration later I had something that sounded workable. Ran it, it failed, compared to other mods, fixed some Lua language errors, tried again, looked up more reference, fixed some more language errors, ... AndSoOnAndSoOn ...
Oh, and Firetuner didn't say a thing, it's as if the lua files never loaded at all (and yes, it's set to Import into VFS).

Soo ... I've finally given up on fixing it myself and have come asking for help. Would anybody look over this and tell me where I messed up? (And feel free to recommend/explain better ways for me to reach my goal - I just pieced things together as I found them in the reference here in the Modiki mostly)

Code:
function DemolishBuildings (player)
	print("Demolish Buildings function was called");
	local pPlayer = Players[player];
	
	if (pPlayer:IsHuman() == 1) then
		print("Human PLayer detected");
		for city in pPlayer:Cities() do
			print("Processing a city");
			if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_DEMOLISHER"])) then
				print("Demolisher was found in city");
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_CULTURE"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_CULTURE"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_ENERGY"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_ENERGY"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_EXPERIENCE"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_EXPERIENCE"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_FOOD"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_FOOD"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_HEALTH"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_HEALTH"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_PRODUCTION"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_PRODUCTION"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_SCIENCE"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_SCIENCE"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_UNLOCKER"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_UNLOCKER"], 0);
				end
				if (city:IsHasBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_DEMOLISHER"])) then
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_BOS_MOD_TEST_DEMOLISHER"], 0);
				end
			end
		end
	end
end

GameEvents.PlayerDoTurn.Add( DemolishBuildings );

Any insights would be most welcome.
Cheers and thanks in advance,
Bosparan
 
You don't want VFS=true, you want to set an Entry Point in the mod properties, i.e., in most cases it'll be InGameUIAddin (note, choose the filename first before you choose InGameUIAddin, or the file won't appear in the dropdown box).
 
Hi Nutty,

thanks a lot for the advice - and that certainly explains why it never showed when I tried out that menu, I always chose InGameUIAddin first - the function is properly loaded now and added to the event, called once per turn for each player.

Doesn't do a thing for beyond printing the first line though and it wrecks the UI a bit: The unit screen in the lower left corner won't show, throwing this error in FireTuner each time I select a unit:
Code:
Runtime Error: Assets\UI\InGame\WorldView\UnitPanel.lua:106: Could not find method to obtain action icon.
stack traceback:
	[C]: in function 'error'
	Assets\UI\InGame\WorldView\UnitPanel.lua:106: in function 'HookupActionIcon'
	Assets\UI\InGame\WorldView\UnitPanel.lua:280: in function 'UpdateUnitActions'
	Assets\UI\InGame\WorldView\UnitPanel.lua:1182: in function 'OnInfoPaneDirty'
	[C]: in function 'func'
	[C]: in function '(anonymous)'
Any idea on how I'm failing there? Must be in how I handle the PlayerID input this is supposed to have ...

Cheers,
Bosparan
 
I don't think "if (pPlayer:IsHuman() == 1) then" works. It's:
Code:
if (pPlayer:IsHuman() == true) then
or, more simply:
Code:
if (pPlayer:IsHuman()) then

Either way, though, that doesn't explain the error you're having. If the Lua file isn't active, then those errors don't occur?
 
Hi Nutty,

I used "== 1" based on the IsHuman() documentation from Civ 5 Modiki. I'll give using bool a try.

Yes, that error only occurs if I set the Lua to active. If I remove that loading entry, it'll load the rest of the mod without trouble (which is pure XML).

Thanks Again
 
Hi Nutty,

regarding the comparing with an Integer: That page documented the output type of IsHuman() to be Integer, so comparing with an integer appeared to make sense.

As for posting the mod in its entirety ...
Now this is the funny thing:
I did add some more print(...) lines, to tell me the original input and the output for IsHuman(). Nothing more.
Now it all works like a charm... (and yes, IsHuman() was missdocumented, it most certainly was a boolean output)

Either something totally odd happened ... or I messed up rebuilding from Modbuddy.

Thanks again for your help and time, I might well have tried running into the same wall for some more hours without you.

Cheers and thanks,
Bosparan
 
I've been getting behavior like that too, where it seems I have to quit completely out of the game sometimes [rather than just return to the main menu after making a change] or I get odd behavior. Civ5 didn't behave that way (unless you had both new and old versions of your mod in the Mods folder).
 
Nice work! This will make it so much easier to test out mods! Thanks!
 
Top Bottom