Stupid Question...

GenEngineer

Prince
Joined
Aug 24, 2014
Messages
456
Hey Everyone,

Trying to get started on adding a few mods to the game. I think I've gotten a handle on editing the XML, but I'm having some issues with the LUA - namely, that the code as written does seem to have any effect. My current issue is that I don't know if that's because the code is broken or if it's because it's to being activated.

So how do you go about setting the LUA code to be run in the game? Everywhere I've looked suggests adding it to the Content tab of the properties of the project, but that assumes that the code is intended to edit a map or UI element. If I want to change the functionality of a building (say, make it spawn a resource nearby when built), where do I go to activate the LUA when I run the mod?
 
For Lua debugging (and many other things when it comes to modding) FireTuner is your best friend, as it contains the auto-updating lua-log (including errors and print-statements) - the tool can be found in a subfolder of the SDK. You should make yourself familiar with it if you haven't already.

Second Question: InGameUIAddin is the one you want to use for any generic code.
 
Well, if you post the code and tell up what you're trying to do people will probably be able to help you on that. ;)
 
Current State of the LUA is as follows (cobbled together from the already included code for spawning a basic resource and code from a CiV mod for spawning a resource when a building is constructed):

Spoiler :
-- Generate a Minable resource on nearby tile when Microbial Mine is Built
-- Options Are Copper, Gold, Sillica, and Minerals
local PlotBonus = {};
local PlotBonusInfo = GameInfo.PlotBonuses["PLOT_BONUS_SPAWN_BASIC_RESOURCE"];

local PossibleResourceWeights =
{
{ Resource = GameInfo.Resources["RESOURCE_SILICA"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_GOLD"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_COPPER"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_MINERALS"].ID, Weight = 1 },
};

function PlotBonus.FindValidResources(player, plot)
local validResources = {};
local team = Teams[player:GetTeam()];

for i,t in pairs(PossibleResourceWeights) do
local chance = t.Weight;
local resourceID = t.Resource;
local resourceInfo = GameInfo.Resources[resourceID];
local revealed = false;
if (resourceInfo.TechReveal == nil) then
revealed = true;
else
local revealTechInfo = GameInfo.Technologies[resourceInfo.TechReveal];
if (revealTechInfo == nil or team:IsHasTech(revealTechInfo.ID)) then
revealed = true;
end
end

if (revealed) then
local ignoreLatitude = false;
if (plot:CanHaveResource(resourceID, ignoreLatitude)) then
if (chance > 0) then
for i = 1, chance do
table.insert(validResources, resourceID);
end
end
end
end
end

return validResources;
end

function PlotBonus.IsValidForPlayerAtPlot(player, plot)
-- Cannot already have a resource
if (plot:HasResource()) then
return false;
end

-- This plot must be able to have a resource we are considering
local validResources = PlotBonus.FindValidResources(player, plot);
if (#validResources == 0) then
return false;
end

return true;
end
function buildOre(pPlayer, pCity)
local AddedTest = 0;
while AddedTest == 0 do
pCityPlot = Game.Rand(#pCity:GetNumCityPlots()-1, "Choosing a plot to spawn resource on") +1; --Grab a random plot in city range
local pSpecificPlot = pCity:GetCityIndexPlot(pCityPlot); -- Grab Plot's location
if PlotBonus.IsValidForPlayerAtPlot(player, pSpecificPlot) == true then-- Make sure plot can have resource added
local resourceDeck = PlotBonus.FindValidResources(player, pSpecificPlot); -- Grab list of addable resources
if (#resourceDeck > 0) then
-- Choose a resource
local roll = Game.Rand(#resourceDeck, "Choosing which resource to spawn from plot bonus") + 1;
local resourceID = resourceDeck[roll];
local resourceInfo = GameInfo.Resources[resourceID];
-- Spawn it
pSpecificPlot:SetResourceType(resourceID, 1);

-- Notify the player
local summary = Locale.ConvertTextKey(PlotBonusInfo.Description, resourceInfo.IconString, resourceInfo.Description);
local message = Locale.ConvertTextKey(PlotBonusInfo.Help, resourceInfo.IconString, resourceInfo.Description);
ShowMessage(pPlayer, pSpecificPlot, summary, message);
local AddedTest=1;
end
end
end
end

function PutOreSomewhere(player, city, building)
print(building)
print(GameInfoBuildingTypes.BUILDING_MICROIAL_MINE)
if building == GameInfoBuildingTypes.BUILDING_MICROBIAL_MINE then
local pPlayer = Players[player]
local pCity = pPlayer:GetCityByID(city)
if buildOre(pPlayer, pCity) == false then
print("No place to place a Mineable Resource.")
end
end
end
GameEvents.CityConstructed.Add(PutOreSomewhere)


Goal is that, when the player constructs a Microbial Mine building, a tile within the range of the city it was constructed in gains a mine-able resource (excluding Titanium because Balance). As it currently stands, I've attached the code to the Mod, but it doesn't seem to be doing anything when the Microbial Mine is constructed.
 
I assume this is the problem:

GameEvents.CityConstructed.Add(PutOreSomewhere)

That specific GameEvent was added in a patch for BNW in Civ V and CivBE seems to be built on an earlier version of the game (as evidenced by the fact that many of the earlier problems showed up in BE again), so it's likely that the GameEvent just doesn't exist in BE - which then causes the script to not do anything. Not that I'm too experienced with lua, so take it with a grain of salt, but at least that's my assumed explanation for that. ;)

Code itself looks correct though. A workaround would be to just check at the beginning of each turn and either use a dummy-building or a table to keep track of which cities already got their free resource.
 
That would explain it then.

I tried replacing the trigger with the GameEvent.BuildingProcessed trigger, seeing as that appears to be what triggers rolling for the building quests. I know it's correctly identifying that a building has been built and is running the code because I made it print the "building" variable 5 times every time it executes the code, but now the issue is that every building constructed registers as the integer 8192. Seeing as the GameEvent.BuildingProcessed is how the game triggers the building quests, there has to be a functionality to specify which building just got constructed, but I don't know what it is.

Any help would be much appreciated.
 
Right, completely forgot about that GameEvent. Anyway, no idea where that problem comes from, but I made some edits and it works fine now. (Well, this one function does - the script still runs into another error quickly after that - which I don't really have the time to look at right now. :D)

Code:
function PutOreSomewhere(player, building, city)
	-- Ryika: New Print-Text just for debug.
	print("- MicrobialScript: A Building was constructed. ID: " .. building .. " (Looking for ID: " .. GameInfo.Buildings["BUILDING_RELIC"].ID .. ".)")
	print()
		-- Ryika: Changed this to Relic to have an easier time testing it 
		if building == GameInfo.Buildings["BUILDING_RELIC"].ID then
			local pPlayer = Players[player]
			local pCity = pPlayer:GetCityByID(city)
			if buildOre(pPlayer, pCity) == false then
				print("No place to place a Mineable Resource.")
			end
	end
end
GameEvents.BuildingProcessed.Add(PutOreSomewhere)
 
I didn't realize that the variables were calling in the wrong order - with that, I've been able to get it running through the entire code, every time. Thanks!
 
Top Bottom