How to get class/metatable names?

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
How can we determine the class of a function? I'd like to replace one of the core API functions, GetYield. Like...

Code:
local meta = getmetatable("???")
meta.oldGetYield = meta.GetYield
meta.GetYield = function(self, yieldType)
  print("Found")
  return self:oldGetYield(yieldType)
end

I don't know the name of the class to put in "???". I tried "Plot" and "CvPlot", neither worked. I thought the class metatables might be stored in _R, but _R is nil.
 
You don't getmetatable on a string but on a pointer I think. Plot is exposed anyways, so you can just edit the Plot table. I'm not sure if you can override the functions from Lua but you can try.

Either way, it's safer to leave vanilla function bugs alone and create your own GetPlotYield function
 
The problem is I'd like to manually increase the yield of a particular tile. Since there's no SetYield, my thought was to intercept GetYield... a personal GetPlotYield function works for all aspects of the UI but the yield display on the tile itself. I couldn't find a function that handles that with searches for words like 'yield' in the api and files.

Altering 8 UI files just to make one small change also sorta bugs me. :crazyeye:
 
The problem is I'd like to manually increase the yield of a particular tile. Since there's no SetYield, my thought was to intercept GetYield...

Nice idea, and that's something I wish they'd put in the game from the start. I've wanted to have my advanced Terraformer unit be able to just boost a tile's yield by standing on it each turn, in addition to the worker actions, but I haven't been able to get that to work because of the lack of a good SetYield function.

But if you can't get the full Lua hack to work, you might be able to get the effect you want in another way. For instance, you could add a custom Feature with no graphics that adds the yields you want, and simply place that yield on the tile you want modified through the appropriate events. (This'd have problems stacking with forests/jungles, and you wouldn't have a lot of flexibility in the yields in question unless you added a whole set of these.) If this is supposed to be a temporary effect, then just remove the feature at the right time.

I did something similar in my own mod; I wanted my new Empath specialist's great person to place a new Monolith terrain improvement (analogous to the Academy/Manufactory/etc.) that added Happiness. So all I did was trap the OnImprovementCreated event, check to see if the placed improvement was IMPROVEMENT_MONOLITH (+2 food, so that the AI will see the tile as valuable and not try to replace it with some other improvement), and if so, place FEATURE_MONOLITH on the same tile with the happiness benefits I wanted (since Features include natural wonders, and so have a happiness option) and -2 food (to cancel out the improvement). It's not pretty, but it seems to work well. If you don't want to play around with Features, you can also do this through tweaking other tables, like Plot type, although that's usually tied to a slew of graphical effects. So, you end up getting the yield boost you want, without having to actually edit the tile's base yield.

I can think of two other ways to do this (unbuildable, repeatable buildings, or a flat Lua yield hack), but both of those would require altering all of the yield UI routines.
 
Is it possible to vary the yield of a feature on a per-tile basis?

What this is actually for is I replaced the Maritime food mechanic with one that's limited to the 5 largest cities. Right now I just manually add this food to the city directly each turn after calculating all modifiers for it (Floating Gardens +15%), but this is a clumsy solution that requires changing a lot of UI files and doesn't show the yield on the city's base tile like the vanilla mechanic does. It works, I'm just wondering if there's a more elegant solution. :)

The reason I thought of overriding the plot metatable's getyield function is my brother did something similar, when he wanted to override a 'get player name' function in HL2 Gary's Mod to create disguises. There's a lot of features exclusive to just CiV's or GM's versions of Lua, but some things are similar, so it doesn't hurt to explore possibilities. :)
 
If that's what you're doing, then my second suggestion would be MUCH better. Create a new "Food Shipment" building type that adds X food, and give the building a cost of -1 so that the player can't build it. There's a field in the BuildingClasses table, "NoLimit", which should allow you to put as many of them in a city as you want if you need to make some variation (like having the capital get twice as much as the others). Then, when the effect is over, just loop through your cities and remove all building(s) of that class.

Building yields, like tile yields, get multiplied by all of the usual multipliers, so you don't have to worry about coding up anything else.
 
Game.SetPlotExtraYield is able to change the yield of a plot.
 
That actually works for growth the next turn and everything? :eek:

I saw it but suspected it's one of the broken / nonimplimented functions... not used anywhere and similar functions for population do not work. There's so many of those!

Is it a permanent change, just until the next turn, etc... and what's the parameter usage?
 
That actually works for growth the next turn and everything? :eek:

I saw it but suspected it's one of the broken / nonimplimented functions... not used anywhere and similar functions for population do not work. There's so many of those!

Is it a permanent change, just until the next turn, etc... and what's the parameter usage?

http://wiki.2kgames.com/civ5/index.php/Lua_Game_Objects/Game#SetPlotExtraYield

I haven't tested it extensively but from what I did it seemed to be added fine to the city functions if worked so yes, I think so.
 
Back
Top Bottom