Help placing buildings with Lua (and general Lua questions)

CampBell91

Chieftain
Joined
Aug 14, 2012
Messages
2
Hi, I'm new to Civ V modding and want to do something relatively simple (to start) with Lua.

First, I don't know Lua per se but I am generally really good at looking at existing code and learning how to do something (and the more I do this, I actually learn how to code as I go along, like I did with python for Civ IV).

The problem with Lua and Civ V is I don't know where to start. For example, with Civ IV I could open up the event manager file, find the OnCityBuilt and know I can add code to that to do something when a city is built. I imagine I can do similar with Lua but I don't know the specific functions or how to word it, (i.e. what to put at the beginning of the file).

Also, Lua files have no OnModSctivated as with XML files, so how do I tell ModBuddy to use the Lua file to begin with?

As to my specific question, I want to place buildings in cities with Lua.

Say I want to do this in my capital as soon as it's built.

From looking at code I see that to find the capital you write something like:

Code:
for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do
  local pPlayer = Players[iPlayer];
  if (pPlayer:IsEverAlive()) then
    if (pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_XXX) then
      local pCity = pPlayer:GetCapitalCity();

What would I put before that bit of code? Do I have define the capital first, then do when the capital is built add such and such?

Also

How do I place a building after that, is it something like?

Code:
	  if (pCity ~= nil) then
       		 pCity:SetBuildingType(GameInfoTypes["BUILDINGCLASS_GARDEN"]);

Also, one last question (sorry for such a long post) how do I set it to log errors and where do I see the log.

Thanks in advance for all your help!
 
The problem with Lua and Civ V is I don't know where to start.

The place to start is the Lua object reference page on the modding wiki, which lists all available Lua functions. Some of them aren't documented very well, but that wiki will at least get you close to functional.

Also, Lua files have no OnModSctivated as with XML files, so how do I tell ModBuddy to use the Lua file to begin with?

InGameUIAddin. This is explained in Kael's modding guide, which you should already be familliar with; I'd suggest reading through his section on Lua modding again, because it explains exactly how to set this all up.

As to my specific question, I want to place buildings in cities with Lua.

The function you want is City:SetNumRealBuildings(). Again, check the wiki for the exact syntax, but it's basically (building ID, number of copies). Some buildings can be placed in a city multiple times to stack effects, but for your purposes you'll only want 1 or 0 for the value.

Also, one last question (sorry for such a long post) how do I set it to log errors and where do I see the log.

First, find your user directory. In Win7, this'll be in My Documents/My Games/Sid Meier's Civilization V/, and it'll be somewhere comparable on other OS's. This is NOT the same location as where Steam placed the resource files.

In that directory, there's a file "config.ini". Open it in a standard text editor.
Near the top is a line "EnableTuner = 0"; change it to a 1.
About two-thirds of the way down is a line "LoggingEnabled = 0". Change that to a 1. Near that line will be a whole other bunch of variables with "Log" in the name; change whichever ones sound interesting to a 1, but to catch basic errors you only need to turn on the first logging flag.

Now, in that directory is also a Logs/ subdirectory; that's where your logfiles will be placed. Generally speaking, you only care about three logfiles: xml.log, Database.log, and Lua.log. I'd suggest running the game once without any mods, so that you can see what those logs look like when everything's working. (The core game has a bunch of errors and warnings in it, so you want to know which ones those are so that you won't panic when you see those same errors in a modded game.)

Also in that user directory is a MODS/ subdirectory, which is where all of your mods are placed. If you ever download a mod from this board, and not from the in-game browser, this is where you have to put the files to make them work.
 
InGameUIAddin. This is explained in Kael's modding guide, which you should already be familliar with; I'd suggest reading through his section on Lua modding again, because it explains exactly how to set this all up.

But I'm not changing the user interface, so why would I change InGameUIAddin?
 
But I'm not changing the user interface, so why would I change InGameUIAddin?

The term is misleading in a way, but Spatz is right. You need to ensure you follow the steps in the modders guide as this enables the game to recognize your new code changes and import them into the game.

To answer your code question, what you need is :

Code:
city:SetNumRealBuilding(buildingType, 1)
 
But I'm not changing the user interface, so why would I change InGameUIAddin?

Like he said, it's just the one that does what we want, despite its name. Technically, you don't NEED InGameUIAddin; pretty much any of the entry point types in the Content tab will work. It's just that if you want to add Lua functions that trigger on one of the existing events, you need to load it through that tab. InGameUIAddin is just sort of the default Content addin type, so we just use that one so as to not conflict with other mods; most of the other options have the potential to trigger specific mods in bad ways, like DiploCornerAddin.

(Interestingly, most simple UI mods won't need that addin command, as they'll use the VFS instead.)

Also, I should give one additional note: if you use SetNumRealBuilding to add a building, there's no problem. But if you use it to DELETE a building (by setting the number to zero), then make sure you unslot any specialists in that building first. (There's a DoTask Lua command for that, which I can find when I get home.) If you don't, then bad things will happen.
 
Back
Top Bottom