Lua help for a building

Uighur_Caesar

Comandante en Jefe
Joined
Mar 14, 2015
Messages
1,227
Location
Florida
I've been working on my first mod recently and I've done everything except the building effect. I need Lua for it to work and I have no prior knowledge of programming. I've looked at some tutorials and other mods Lua but I've been unable to make it work. How would I make a building that gives you a boost to science that is equal to half of the city's faith production?
 
Then your code would look something like this, with a comment after each to explain what the line is doing:
Code:
GameEvents.PlayerDoTurn.Add(																-- This is called the "event hook". It tells the game when it should actually run the code; until then, it will be functional but won't do anything. This particular one fires when a player, be they human or AI, begins their turn.
function(iPlayer)																			-- Everything in most languages is managed by reusable chunks of code called "functions" (or "methods"). Functions take parameters, and here they have to match up with the ones that the event hook takes - in this case, a player ID, which is literally a number.
	local pPlayer = Players[iPlayer]														-- This is saying that there exists a table called Players, in which you plug in a value and it returns a player. The value we're plugging in is iPlayer - remember, it's a number. We don't even need to know what number exactly this is, since it's already accomodated for by the line above, and until we force the game to only respond to when iPlayer corresponds to a particular value, this will work for every player. For the record, the human player's ID is always 0.
	for pCity in pPlayer:Cities() do														-- Run the following code for each of pPlayer's cities:
		if (pCity:IsHasBuilding(GameInfoTypes.BUILDING_YOUR_BUILDING_NAME_HERE)) then		-- For each of pPlayer's cities: Does your building exist in it? If so...
			local iFaithPerTurn = pCity:GetFaithPerTurn()									-- This will retrieve the amount of faith that the city is generating each turn.
			local halfOfThat = math.ceil(iFaithPerTurn / 2)									-- ...And this will find half of it. Why math.ceil instead of just straight dividing by 2? The method that actually gives the research boost requires an integer, so we have to account for if the faith per turn is an odd number and half of it ends up being something like 2.5. math.ceil always rounds the decimal up; use math.floor if you want to round it down instead.
			local currentTech = pPlayer:GetCurrentResearch()								-- Will find the current tech pPlayer is researching.
			local pTeamTechs = pPlayer:GetTeam():GetTeamTechs()								-- Techs are weird, since they're not managed by players but by teams - and actually by a the team's collective science tracker called TeamTechs. This will find pPlayer's team, and then find pPlayer's team's TeamTechs.
			pTeamTechs:ChangeResearchProgress(currentTech, halfOfThat, iPlayer)				-- The moment of truth! pTeamTechs will give a boost of science equal to halfOfThat to the curent research project currentTech. I'm not sure why iPlayer is in there TBH, but the method does take this parameter.
		end																					-- every if, for, while, and other key words must be accompanied by a subsequent "end". Some languages do this for you; Lua, unfortunately, does not.
	end																						-- 
end)																						-- Parenthesis needed to close the begin parenthesis immediately following the event hook.

If you're feeling brave you can condense it, although if you've never had any programming experience before I wouldn't recommend it...
Spoiler :
Code:
GameEvents.PlayerDoTurn.Add(
function(iPlayer)
	local pPlayer = Players[iPlayer]
	for pCity in pPlayer:Cities() do
	if not (pPlayer:IsHasBuilding(GameInfoTypes.BUILDING_YOUR_BUILDING_NAME_HERE)) then return end
	pPlayer:GetTeam:GetTeamTechs():ChangeResearchProgress(pPlayer:GetCurrentResearch(), math.ceil(pCity:GetFaithPerTurn() / 2), iPlayer)
end)
 
Top Bottom