How do I change a building's PrereqTech from Lua?

General Tso

Panzer General
Joined
Oct 12, 2007
Messages
1,548
Location
U. S. of A.
As the title says how do I change a building's PrereqTech from Lua? More specifically - I would like to dynamically enable Courthouse's (so that they can be built without any requirements). It needs to be done from within Lua not in a XML file. Is this possible? I would think that this would be a simple matter but I've spent a lot of time searching and I can't find anything.
 
That looks like it's exactly what I need. Thanks!
 
Unfortunately that function only allow me to disable a building by returning false. Returning true just allows buildings that are already enabled to be built. Why the *?/# didn't the developers allow us to control whether a building should be built? I guess they where afraid we couldn't handle the concept of allowing a very long list of buildings in the the city screen.
 
You could make the building(s) you want to be dynamically available have no tech prereq and handle when it/they can be built entirely from LUA.
 
You could make the building(s) you want to be dynamically available have no tech prereq and handle when it/they can be built entirely from LUA.

As discussed in several other threads, that's exactly what you must do.

The Lua hooks are all ANDed together with the original condition from the base game, so if any one of them returns false (including the original base condition) the building can't be built. There is no (fully implemented) OR condition (although most of the stubs are present they don't work / arn't used) to allow construction if any one returns true.

So to override the base condition you must fully disable it (in this case by removing the pre-req tech) and then re-implement the bits you want (if any) along with your new OR condition(s) in Lua.

It's not particularly hard, just a major PITA. And you also have to be aware that any other mod installed that changes pre-req techs for buildings and/or adds its own Lua hook(s) will almost certainly break all your lovely logic
 
Thanks for the responses everybody. I was getting frustrated with the situation so I wasn't thinking as clearly as I should have been. I'll implement the combination XML and Lua method - looks like it should work OK.
 
I have another question that's related to this subject. Is it correct to assume that none of the data located inside the database (such as a building's PrereqTech) can be changed from within Lua? Everything that I've read indicates that is the case but I haven't seen anything that states it as 100% fact.
 
technically you can change them, but the DB is cached on loading and the game don't/can't update the cached data after, so you'll have to check the change with your own code.
 
Thank you. I guess that's why I didn't see a definite yes or no when I was searching.
 
And there was much wailing and gnashing of teeth, for the cache was so simple it was laughable

Code:
> for row in DB.Query("SELECT Type, Combat FROM Units WHERE Type='UNIT_WARRIOR'") do print(row.Type, row.Combat) end
 UNIT_WARRIOR	8

> for _ in DB.Query("UPDATE Units SET Combat=10 WHERE Type='UNIT_WARRIOR'") do end

> for row in DB.Query("SELECT Type, Combat FROM Units WHERE Type='UNIT_WARRIOR'") do print(row.Type, row.Combat) end
 UNIT_WARRIOR	10

> u = GameInfo.Units.UNIT_WARRIOR; print(u.Type, u.Combat)
 UNIT_WARRIOR	8
 
And there was much wailing and gnashing of teeth, for the cache was so simple it was laughable
Are there any speed issues or other implications?
 
I'm not surprised that the DLL caches DB values rather than looking them up as needed. I do this often in Lua. Even GameInfo is horribly slow (see #7 here) compared to reading a local Lua table, but reading the actual DB would be much much slower. I haven't tested it, but I'd guess 1000x slower than caching the info.

C++ is faster than Lua (isn't it? ;)) so the difference would be even greater.

And that's just reading. Writing to DB has to wait for your hard drive to rotate around (twice! assuming you have one that rotates) for each transaction. That's not too bad for a mod if you can organize your writes into one transaction per turn (a transaction can have 1000s of inserts if done correctly). But imagine if the DLL had to write to the DB, then wait for your hard drive to rotate around twice, before it does something else.

At first, I was also puzzled by the lack of effect of DB changes. I was also puzzled by the name DebugDataBase. Both make sense if you realize that DB info is cached before it is used and that DebugDataBase is only there to tell you what data the game is using.

Edit (better analogy): Changing DebugDataBase is like using your finger to move the needle on your speedometer. You can do it, but that won't change the speed of your car. (Feel free to call that bad design if you like...)
 
That sounds unbearable. Yuk, yuk, yuk. :D
 
Back
Top Bottom