C++/Lua Request Thread

lua code, needs to be specified as VFS=false and needs a "InGameUIAddin" in the "Content" tab of Modbuddy. The modinfo file should show import="0" for the file, and there should be an "entrypoint" such as this:
Code:
  <EntryPoints>
    <EntryPoint type="InGameUIAddin" file="LUA/YourLuaFileName.lua">
      <Name>CityStrategicResources</Name>
      <Description>CityStrategicResources</Description>
    </EntryPoint>
  </EntryPoints>
Actual lua code required should be:
Code:
local iBuildingCapitalStrategicID = GameInfoTypes.BUILDING_RESOURCES_DUMMY
local civilisationID = GameInfoTypes.CIVILIZATION_YOUR_CIV_NAME_GOES_HERE

function CapitalCityStrategicsForGreatWorks(playerID)
	local pPlayer = Players[playerID]
	if pPlayer:GetCivilizationType() == civilisationID then
		local pCapitalCity = pPlayer:GetCapitalCity()
		pCapitalCity:SetNumRealBuilding(iBuildingCapitalStrategicID, pCapitalCity:GetNumGreatWorks())
	end
end
GameEvents.PlayerDoTurn.Add(CapitalCityStrategicsForGreatWorks)
This will, of course, only work in the player's capital city. I have tested and it is not necessary to specify the Great Work type for GetNumGreatWorks() when you want the total of all great works types. I have not tested whether you can specify the type of Great Work to only "count" the great works of a specific type.

It will be necessary for you to change "CIVILIZATION_YOUR_CIV_NAME_GOES_HERE" to the correct designation for your civilization as you named it in the XML

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

XML for the building:
  • Needs (obviously) to be placed in an XML file within the mod
  • The XML file needs an "<OnModActivated>" -- "<UpdateDatabase>" in the ModBuddy "Actions" tab

Spoiler building definition in XML :
Code:
<GameData>
	<Buildings>
		<Row>
			<Type>BUILDING_RESOURCES_DUMMY</Type>
			<BuildingClass>BUILDINGCLASS_RESOURCES_DUMMY</BuildingClass>
			<Cost>-1</Cost>
			<FaithCost>-1</FaithCost>
			<PrereqTech>NULL</PrereqTech>
			<GreatWorkCount>-1</GreatWorkCount>
			<ArtDefineTag>NONE</ArtDefineTag>
			<MinAreaSize>-1</MinAreaSize>
			<NeverCapture>true</NeverCapture>
			<HurryCostModifier>-1</HurryCostModifier>
			<IconAtlas>BW_ATLAS_1</IconAtlas>
			<PortraitIndex>19</PortraitIndex>
			<Description>TXT_KEY_BUILDING_RESOURCES_DUMMY</Description>
		</Row>
	</Buildings>
	<BuildingClasses>
		<Row>
			<Type>BUILDINGCLASS_RESOURCES_DUMMY</Type>
			<DefaultBuilding>BUILDING_RESOURCES_DUMMY</DefaultBuilding>
			<Description>TXT_KEY_BUILDING_RESOURCES_DUMMY</Description>
			<NoLimit>true</NoLimit>
		</Row>
	</BuildingClasses>
	<Building_ResourceQuantity>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_ALUMINUM</ResourceType>
			<Quantity>1</Quantity>
		</Row>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_COAL</ResourceType>
			<Quantity>1</Quantity>
		</Row>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_URANIUM</ResourceType>
			<Quantity>1</Quantity>
		</Row>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_IRON</ResourceType>
			<Quantity>1</Quantity>
		</Row>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_HORSE</ResourceType>
			<Quantity>1</Quantity>
		</Row>
		<Row>
			<BuildingType>BUILDING_RESOURCES_DUMMY</BuildingType>
			<ResourceType>RESOURCE_OIL</ResourceType>
			<Quantity>1</Quantity>
		</Row>
	</Building_ResourceQuantity>
	<Language_en_US>
		<Row Tag="TXT_KEY_BUILDING_RESOURCES_DUMMY">
			<Text>Strategic Resources</Text>
		</Row>
	</Language_en_US>
</GameData>
---------------------------------------------------------------------------------------------------------------------------------

If you wanted to make the lua script run for any/all cities a player has (and not just the capital), the lua script would be changed to:
Code:
local iBuildingCityStrategicID = GameInfoTypes.BUILDING_RESOURCES_DUMMY
local civilisationID = GameInfoTypes.CIVILIZATION_YOUR_CIV_NAME_GOES_HERE

function CityStrategicsForGreatWorks(playerID)
	local pPlayer = Players[playerID]
	if pPlayer:GetCivilizationType() == civilisationID then
		for pCity in pPlayer:Cities() do
			pCity:SetNumRealBuilding(iBuildingCityStrategicID, pCity:GetNumGreatWorks())
		end
	end
end
GameEvents.PlayerDoTurn.Add(CityStrategicsForGreatWorks)
There would be no need to change the XML for the building.

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

@Salamandre

Lua scripts such as these create within themselves all the conditions necessary for placing a dummy building into a city, so it is not necessary (and is usually counter-productive) to make the building dependant on a tech, policy, or other exclusion/dependancy as part of the XML of the dummy building.

Machiavelli's PGB tables were meant by Machiavelli to be used as templates to help shorten the work needed by a mod-maker when the mod-maker wants adopting certain policies to grant certain buildings -- but in any other case Machiavelli's PGB isn't of any real use. Instead we as mod-makers just create in XML the dummy-building we need, and make our lua scripts reference the XML-name of the dummy building, as I did in this line of code:
Code:
local iBuildingCapitalStrategicID = GameInfoTypes.BUILDING_RESOURCES_DUMMY
 
Lees, thanks. you mean I should ignore the lua code given before and replace with yours?
Yes, you would use the code I presented instead of any earler versions suggested.

And wgat is the "your civ name here" tag, I don't know my civ name, it could change from game to game.
Well, both GPuzzle and I were assuming you were wanting this for a specific civilization.

The only way to make this easily adaptable regardless of which civ you play as is to change the lua script to this instead of what I showed before:
Code:
local iBuildingCapitalStrategicID = GameInfoTypes.BUILDING_RESOURCES_DUMMY

function CapitalCityStrategicsForGreatWorks(playerID)
	local pPlayer = Players[playerID]
	if pPlayer:IsHuman() then
		local pCapitalCity = pPlayer:GetCapitalCity()
		pCapitalCity:SetNumRealBuilding(iBuildingCapitalStrategicID, pCapitalCity:GetNumGreatWorks())
	end
end
GameEvents.PlayerDoTurn.Add(CapitalCityStrategicsForGreatWorks)
And this code would be only good for the empire's capital city, will only work correctly for single-player games, and will work for whichever civ you choose to play as.
 
Fantastic, now will start testing. Thanks a lot guys, this was the pinnacle of my mod, as with one city only, the chance of getting all strategic resources to win is very slim. Now with the player's effort on great works, he will have a chance.

@Edit: ok, testing. How do we get the dummy building to show? I started the game, create an artist, filled the palace greatwork slot, no dummy building showing, no resources added. No log errors, files are correctly installed.


@Edit2: dunno why, adding those two files messed all my civ/. Mods show UPDATE almost all, I "updated" then IGE isn't working anymore, deleted cache, reloaded game, same, all screwed, buildings disappeared. :confused:

@Edit 3, deleted both files, now all working. So there is definitely something in that xml or lua text which is badly conflicting with other data, no clue which.
 
I cleaned all and remake. Now mods work but the greatworks/resource not. Here is the mod, if any can help...(lua and xml are in folder Greatworks)

However when I add the building with IGE, the resource counter increases (iron for instance). But when I add greatworks, nothing is triggered.
 

Attachments

I cleaned all and remake. Now mods work but the greatworks/resource not. Here is the mod, if any can help...(lua and xml are in folder Greatworks)

However when I add the building with IGE, the resource counter increases (iron for instance). But when I add greatworks, nothing is triggered.
Did you execute "next turn" ? The lua event the code is hooked to needs next turn to be processed.

Also, the tech that reveals each of the resources needs to be discovered, otherwise the 'free' resource qty don't show up.

I added the lua and xml files into a test mod I use -- everything looks to be working correctly for me.
 
It does not show in the city view because the building is hidden. Remove the line
Code:
<GreatWorkCount>-1</GreatWorkCount>
from the XML of the building and it will no longer be created as a hidden building, and will show in the capital.

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

The code works just fine for me. I directly copied the contents of the two files (xml and lua) from your mod into a test mod I use.

I then used IGE to give a Great Artist, and used it to create a Great Work into the capital. Processing "NEXT TURN", and checking in IGE shows that the building has been added to the capital city. Then using IGE to give myself the Horseback Riding and Bronzeworking Techs made the Horse and the Iron show as an available resource.

If you are not doing so, start an entirely new game to test the function of the code.

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

I did not even attempt to test the code with your entire mod running because there is just way too much stuff going on in that mod, and when I test for a piece of code working I test for that and only that piece of code.
 
ok, will test again with the other mods disabled, thanks for kind help.
I just tested using your entire mod and without any changes in the code of your mod. The strategic resources from great works is working just fine. You have a buncch of errors being reported to lua and database logs coming from your mod, but none of them are affecting this portion of the code.
 
I deleted cache because this disables all mods so its faster for me (civ 5 doesn't have a mod preset list, thats really stupid at this game level) to enable only mine and IGE and guess what: IGE doesn't work any longer after deleting cache. 15 minutes lost to open/reopen the game, and now what to do? Delete/redownload?.

At this point I start to think very bad things about firaxis guys but I will stop here and remain civilized.
 
okay, I tested with ONLY my mod and IGE and nothing happens. At this point it looks to me paranormal activity when you keep saying it works on your side.:crazyeye:

I mean, the two values of horses and iron in the upper screen bar remain at 0, with the palace great work.



I will try to make some national wonders giving those resources, the greatworks is a dead end I see. Thanks anyway.
 
Hello everyone. just getting into Lua coding and all that, and I've run into a problem I can't figure out. As part of a civ's trait, I want them to get a free building (in this case shrine) when settling a new city on a particular feature (in this case jungle). The main problem I'm running into is that those features (forest, jungle, marsh) are removed when a city is settled on them. That means that using the GameEvents.PlayerCityFounded wouldn't work (I believe?), and I can't think of/find what would. I can do the rest of the code, probably, but I need to know what event to hook into with my function, and depending on the event, how to get the x/y of the new city's tile.
 
@Salamandre
That's a very unspecific definition you gave. Is this supposed to be a custom civ that has the ability to get free monuments, or do the other players get free monuments when they settle their cities? If this is supposed to be a civilization's trait, firstly, that is very powerful since they'll almost always have a free monument on turn 1. Second, you shouldn't assume that every other player will be AI, since the AI could be playing the civ as well, and the ability should still work.

But, assuming you want this as a trait for a specific civ, and that it's supposed to be something like "whenever another player settles a city, your cities get a free monument," here you go. I wouldn't necessarily trust my code until someone else here looks it over, but it's probably at least enough to get you started.

Code:
local iCiv = GameInfoTypes.CIVILIZATION_YOUR_CIV
local iMonument = GameInfoTypes.BUILDING_MONUMENT

function FreeMonument(iPlayer, x, y)
	local pPlayer = Players[iPlayer]
	for i, pCiv in pairs(Players) do
		if pCiv:GetCivilizationType() == iCiv then
			if pPlayer ~= pCiv then
				for pCity in pCiv:Cities() do
					pCity:SetNumRealBuilding(iMonument, 1)
				end
			end
		end
	end
end
GameEvents.PlayerCityFounded.Add(FreeMonument)
 
Thanks, will try it but need for any civ :thumbsup:

Sorry for not being enough explicit, nothing to get behind the lines :rolleyes: : I want AI players (all, any) in my single player games to always get free monument on first day they settle a new city. I play only OCC so before you show me the code I was using monument as free building, era ancient. But I also get it in my city thus the lua is better.
 
Then this is probably closer to what you're looking for.

Code:
local iMonument = GameInfoTypes.BUILDING_MONUMENT

function FreeMonument(iPlayer, x, y)
	local pPlayer = Players[iPlayer]
	if pPlayer:IsHuman() then return end
	for pCity in pPlayer:Cities() do
		pCity:SetNumRealBuilding(iMonument, 1)
	end
end
GameEvents.PlayerCityFounded.Add(FreeMonument)
 
The code doesn't allow for civs that have UB variants of the monument, eg Ethiopia

The solution depends on whether or not your using a modded DLL
 
Back
Top Bottom