BNW Trade Route Table contents

Mrblbshrtz

Chieftain
Joined
Jul 29, 2014
Messages
53
My mod requires modification of BNW's trade routes system, but in order to do so, I need to figure out what Player:GetTradeRoutes() is. It returns tables, so far I have figured this out by analysing the TradeRoutesOverview.lua:

It returns a table, it has one entry for each trade route, though you can't index it by using ints, I had to use ipairs (something I don't quite understand). Each entry is a table. It has in it this (so far what I have gleaned):
  • FromCity: Returns a table- how can I find out the contents of this table without knowing the keys- will this require ipairs?
  • ToCity: As above
  • FromID
  • ToID
  • Domain: Land or water
  • FromCiv
  • ToCiv
  • ToCityName: Returns the Name! Yay
  • FromCityName: Same as above
  • From*: GPT, Production, Food, Religion, Pressure, Science
  • To*: GPT, Production, Food, Religion, Pressure, Science
  • TurnsLeft

One thing I'm looking for is some kind of unique identifier of that trade route, as each trade routes position in the table will change as trade routes expire/ are removed. Another thing, is what is the contents of that city table?

If you guys could answer any of the questions, that'd be great, but I thought I'd put this up mainly if anyone else may have use for it - I certainly did.
 
That's pretty much it for the contents of each trade route entry, from the source code

  • Domain - DomainTypes.DOMAIN_LAND or DomainTypes.DOMAIN_SEA (int)
  • TurnsLeft - turns left before the trade route can be reassigned (int)
  • FromCivilizationType - eg GameInfoTypes.CIVILIZATION_ENGLAND (int)
  • FromID - from player ID (int)
  • FromCityName - from city name (string)
  • FromCity - from city (Lua pCity object)
  • ToCivilizationType - to player civ type (int)
  • ToID - to player ID (int)
  • ToCityName - to city name (string)
  • ToCity - to city (Lua pCity object)
  • FromGPT - route yield (int)
  • ToGPT - route yield (int)
  • ToFood - route yield (int)
  • ToProduction - route yield (int)
  • FromScience - route yield (int)
  • ToScience - route yield (int)
  • ToReligion - to religion type (or -1) (int)
  • ToPressure - to pressure (int)
  • FromReligion - from religion type (or -1) (int)
  • FromPressure - from pressure (int)
  • FromTourism - from tourism (int)
  • ToTourism - to tourism (int)

The table of trade routes is built every time the UI uses it, so as you've noticed the individual trade routes move around within it. Unfortunately as Firaxis didn't need to locate trade routes individually there is no unique identifier provided (you can construct one from Domain+FromPlayer+FromCity+ToPlayer+ToCity+ToFood but it's pretty ugly)
 
No. (Although you can change the from/to city by re-routing the trade route!)
 
I'm trying to add a slider in the UI when you establish a new trade route- right underneath the city name and above the list of bonuses. My first step would be making a non interactive slider - the value stays at 50%. So far I've made duplicates of ChooseInternationalTradeRoutePopup.xml and lua in my mod, and got the lua imported into VFS and the xml updating the database. It works fine when I use all the original code, but when I try and add these lines:

XML:
bonuses moved down so slider fits
Code:
<Slider ID="ProductionSlider" Style="Slider" Length="350" Anchor="L,T" Offset="70,25" />
			<Label Anchor="L,T" Offset="70,45" Font="TwCenMT16" ColorSet="Beige_Black_Alpha" FontStyle="Shadow" WrapWidth="420" ID="Bonuses" />

LUA:
This code placed inside (right at the bottom of) the for loop in the function RefreshData():

Code:
		table.insert(g_Model, tradeRoute);
		Controls.ProductionSlider:SetValue(fProd)

But this is what happens:

Spoiler :


Along with a lua error saying I am trying to index field 'ProductionSlider' a nil value. I'm following the slider tutorial, so I can't see why it's nil, or why my code doesn't work, or why ProductionSlider is nil. Any help would be greatly appreciated.
 
It will depend where in the XML you added the control. Zip and attach the complete mod (or your test version of it that demos the problem)
 
The slider is within an <Instance> tag so "Controls." will never work (as that refers to static controls on the page). As you will get one control per created instance, you'll need to use the reference you got to the specific instance (from the line "local itemInstance = g_ItemManagers[tradeRoute.Category]:GetInstance();") You're also trying to create the control in RefreshData() whereas you should be doing it in DisplayData().

And one other thing I spotted (there may be more), you've not set VFS=true for the ChooseInternationalTradeRoutePopup.xml file
 
Yep, that worked- I'm now going to try and get it interactive, and get it to save a value that another lua script in the same mod can use (I think I use MapModData for this?). Will update with my progress.
 
Anything UI related is slow by definition (while waiting for the user to do something) so you may as well just persist the value from the sliders straight into the Modding database and read the values back out in the other Lua file. If you'll be reading the values often, you can read-cache them, and use a lua event to notify the cache to clear itself when a user has changed a value via the slider. See Get/SetPersistentProperty() in TurnsRemaining.lua in the Steam Punk scenario for the basics.
 
Everything's working fine and dandy atm. And it does look pretty, if I do say so myself. I got the data sharing thing sorted by using global variables in one lua file, and "including" that file in the other lua. I'm a bit pressed for time atm, so your insight helped A TON. Unfortunately, I'll have to put further work on hold for a few days.
 
The include() feature in Civ is an enhancement provided by Civ and not a standard Lua feature. It is not like "include" in almost every other language implementation on the planet, but it is "copy the code from that file into this file at this point". If you include file A with functions X and Y into files M and N you will end up with three copies of X and Y - one each in A, M and N. Any variables declared at file scope within A will not be global, you will end up with three copies of them. The only truly global variable (which fortunately is a table) is MapModData.
 
OK, how would I get my ChooseInternationalTradeRoutePopup.lua to call a function in another file, and provide parameters?
And then, would I have to use MapModData to pass data between the two "game logic" luas in my mod? How does this exactly work?

EDIT: Also, how would I make it so that caravans can only travel on railroads, and that if one is broken, it ends the trade route? Also, two functions that you used in your UI - Trade routes Enhancements (Unit:EndTrader() and UnitRecallTrader()) - are they native to Civ5 / BNW, or are they part of your dll?
 
I think I've figured out how to use MapModData. And I've figure out a hacky way to do the first thing.
 
Top Bottom