Using a dummy improvement to generate a resource?

octo

Chieftain
Joined
Jul 3, 2013
Messages
40
Location
philadelphia
So I'm just learning the ins and outs of modding with Lua and I have been working on a civ that allows the player to plant crops on blank tiles. Would it be possible for somebody to look over this little bit of code and tell me if I'm on the right track or not?

The way it's intended to function is that when the unique improvement "Plant Corn" is finished being constructed this code puts a raw "Corn" resource on that tile and deletes the improvement. Sorry if this has already been covered in a tutorial somewhere, but I searched and hadn't found anything. :crazyeye:

Spoiler :
Code:
--Global--
local iCornPlanting = GameInfoTypes["IMPROVEMENT_PLANT_CORN_RESOURCE"]
local iOctoCorn = GameInfoTypes["RESOURCE_OCTO_CORN"]

--Corn--
function onCornCreated(playerID, plotX, plotY, improvementID)
	if (improvementID ~= iCornPlanting) then return end
	
	local pPlot = Map.GetPlot(X, Y)
	local iImp = pPlot:GetImprovementType()
	if (iImp == iCornPlanting) then
		pPlot:SetResourceType(iOctoCorn, 1)
		pPlot:SetImprovementType(-1)
	end
end
GameEvents.BuildFinished.Add(onCornCreated)
 
The general flow of the function is fine (although I personally dislike the whole one-line 'then return end' business because I find them pointless) but you do have other issues.

Code:
function onCornCreated(playerID, plotX, plotY, improvementID)
	[COLOR="Red"]if (improvementID ~= iCornPlanting) then return end[/COLOR]
	
	local pPlot = Map.GetPlot(X, Y)
	local iImp = pPlot:GetImprovementType()
	if (iImp == iCornPlanting) then
		pPlot:SetResourceType(iOctoCorn, 1)
		pPlot:SetImprovementType(-1)
	end
end

This line, as I said, is unnecessary, because if you require a match to your Improvement to run your function, it implicitly means that the function will end on its own if it doesn't match.

Code:
function onCornCreated(playerID, [COLOR="Green"]plotX[/COLOR], [COLOR="Green"]plotY[/COLOR], improvementID)	
	local pPlot = Map.GetPlot([COLOR="Red"]X[/COLOR], [COLOR="Red"]Y[/COLOR])
	local iImp = pPlot:GetImprovementType()
	if (iImp == iCornPlanting) then
		pPlot:SetResourceType(iOctoCorn, 1)
		pPlot:SetImprovementType(-1)
	end
end

Your X and Y variables are undefined, because they don't match the values you received them as. It would need to be Map.GetPlot(plotX, plotY) in order to function properly.

Code:
function onCornCreated(playerID, plotX, plotY, [COLOR="Green"]improvementID[/COLOR])
	if (improvementID ~= iCornPlanting) then return end
	
	local pPlot = Map.GetPlot(X, Y)
	[COLOR="Red"]local iImp = pPlot:GetImprovementType()[/COLOR]
	if (iImp == iCornPlanting) then
		pPlot:SetResourceType(iOctoCorn, 1)
		pPlot:SetImprovementType(-1)
	end
end

I don't understand why you need to look up the improvement when.... the function tells you the improvement!

Code:
--Global--
local iCornPlanting = GameInfoTypes["IMPROVEMENT_PLANT_CORN_RESOURCE"]
local iOctoCorn = GameInfoTypes["RESOURCE_OCTO_CORN"]

--Corn--
function onCornCreated(playerID, plotX, plotY, improvementID)
	if improvementID == iCornPlanting then	
		local pPlot = Map.GetPlot(plotX, plotY)
		pPlot:SetResourceType(iOctoCorn, 1)
		pPlot:SetImprovementType(-1)
	end
end

GameEvents.BuildFinished.Add(onCornCreated)

This should do what you want, although I believe someone mentioned recently that SetImprovementType(-1) seems to introduce some odd graphical errors until the game is reloaded (though it doesn't affect what resources are actually on the tile.) Maybe swapping that line with the SetResourceType() line would help, but I'm not sure.

Edit:
Forgot to add the GameEvent. Note that BuildFinished only works in BNW, so your mod will not be compatible with G&K or Vanilla.
 
Thanks DarkScythe!

I was particularly confused how X and Y variables worked so thanks for that. Just to be clear, do I need to keep in

Code:
GameEvents.BuildFinished.Add(onCornCreated)
?
 
Yes, you do. Apologies, as I forgot to include that line in the final code box, but I've edited my post to include it.

That line is what tells the game you want it to run your custom function whenever the game fires its own code at that point. Without it, your function will be defined, but sit there doing nothing.
 
Hm, I've tried everything but for some reason it still comes up as a blank tile when the Workers complete the construction, although it still thinks that there is an improvement on the tile. I'm a bit at a loss as to why this might be happening so I double checked to make sure the resources/improvements were all properly named (there's basically 3 more of the same type of improvement as the Corn). The resources don't show up, and the improvements don't go away...
 
Sounds like you may not have activated the Lua file properly.

We'd need your logs and/or for you to post your mod to give you a definitive answer.

However, if this is the first time you've worked with adding Lua, you might have added it incorrectly.

Ensure that the Lua is not imported into VFS, and that it is added as an InGameUIAddin from your mod's Properties page, under the Content tab. You will find it easier to select your Lua file before changing the type to InGameUIAddin.

This sets your Lua file as an "entry point" so that the game will run your Lua code in its own context, and allow your Lua to actually function.
 
Alright, I'm pretty positive I have the InGameUIAddin set correctly for the Lua. I'm kind of at a loss at this point, so I've attached a zip of the log files as well as the mod itself (with the modinfo file to show what's been imported etc). This is basically the final step for this mod and I'm really excited to finish it!
 
A couple of points:

  1. As a general rule, whenever you test mods, please make sure yours is the only one loaded. Those logs are incomprehensible because you are getting hundreds of errors from other mods, between IGE, EUI, Infoaddict, Civ Names by Policies, etc.
  2. You have way more files imported into VFS than is necessary. I suppose this doesn't outright break anything, but any XML file which is set to <UpdateDatabase> does not need to be imported.
  3. There is something wrong with your Worker's Build Icon descriptions, as they are not showing up. I haven't looked too closely at that yet, but it's something you will want to look into.
  4. The main reason why your Lua isn't working is because it's not even being loaded by the game. Sprinkling a couple print() statements is a good way to check whether anything is loading or not. As far as why this is...
  5. <EntryPoint type="InGameUIAddin" file="MidwestUA.lua"> -- This is not correct. The filename is fine, but this is telling the mod to load a file of this name from the root directory of which it doesn't exist. You have it in a Lua folder! It needs to be <EntryPoint type="InGameUIAddin" file="Lua/MidwestUA.lua"> for it to work.

After correcting the file path, it seemed to work fine for me, although again the Build Icon descriptions are bugged.

It is an exciting time when you're about to release your first mod, though, so good luck!
 
I apologize for the sloppyness of the logs. I was in a rush this morning and forgot to turn off the other mods before I started it up and totally forgot about it.

I CAN'T BELIEVE IT WAS THE FOLDER ISSUE. When I get home from work today I'll comb through everything as meticulously as possible.

THANK YOU SO MUCH.
 
Back
Top Bottom