Need some advanced Lua help

cephalo

Deity
Joined
Jul 26, 2007
Messages
2,058
Location
Missouri, USA
So I've been looking at the lua code for the map scripts, and the AssignStartingPlots.lua file is quite different than the one in Civ5. There are things in there that I would like to tweak, without having to copy the whole file.

The AssignStartingPlots class actually does all of it's work inside the constructor. If I want to change one of it's internal functions, is there a way that I can just override them somehow instead of replacing the whole class?

It would be nice if my map script would reflect any changes made to it by Firaxis, except for the tiny bits I want to change. It could still break I guess, but I prefer to not to copy and paste massive chunks of code.
 
If you want to create your own script, you have to replace the call by something of your own.

I think the guys who wrote that code could have done it cleaner, but it is quite easy to adapt without rewritign or copying everything.
The job is done in :
local start_plot_database = AssignStartingPlots.Create(args)
[ Nitpicking: Note that start_plot_database is never used, so it is actually better to do
AssignStartingPlots.Create(args)
since the return value is not used. ]
So, indeed, all the work is done in Create.
But think of it not as a constructor, it's just a method, and forget the return value, it's useless.

What you want to do is create a local table like they do in Create, and then call __InitStartingData on it which does all the job. Just replace whatever method you want.
For instance, say you want to replace the __StartBiasTerrains method because you don't like it and would like to make your own (say change the range from 2/3 to 1 plot or whatever).
Instead of calling AssignStartingPlots.Create(args), you do:
local assignPlotTable = {
-- Core Process member methods
__InitStartingData = AssignStartingPlots.__InitStartingData,
__SetStartMajor = AssignStartingPlots.__SetStartMajor,
... basically copy everything from the AssignStartingPlots.lua
instead of doing
__StartBiasTerrains = AssignStartingPlots.__StartBiasTerrains, however, write:
__StartBiasTerrains = MyBetterMethod,
Then just call
assignPlotTable:__InitStartingData()

This way, any change to the other methods they do in AssignStartingPlots.lua you will eventually benefit from.
The downside is if they add some methods, then you'll have to also add them in your init.
I wish they had written it this way:
local start_plot_database = AssignStartingPlots.Create(args)
databse:__InitStartingData()
It would have been absolutely trivial to change the methods.
You could try it :
local start_plot_database = AssignStartingPlots.Create(args)
start_plot_database.__StartBiasTerrains = MyBetterMethod,
databse:__InitStartingData()
But I'm not sure what happens if you call __InitStartingData twice.

If the game supports calling player:SetStartingPlot(v); several times, then you can do it this way, it will be better than copying the whole table in your code. I'm too lazy to test it, though.
 
Top Bottom