Question about data types returned by methods

Ethidium

Chieftain
Joined
Feb 29, 2016
Messages
16
I'm looking at some of the Plot methods - does anyone know why methods like Plot:IsCity or Plot:IsTradeRoute return an integer, rather than a boolean? Is the latter the number of trade routes that pass through a given plot? If so, then what does IsCity return? Surely you can't have more than one city per plot?
 
They don't? My bad - they're listed in the wiki as ints, so I was going off that. Thanks for setting the record straight. I'm actually trying to get a count of the number of trade routes that pass through a given tile - any ideas?
 
They don't? My bad - they're listed in the wiki as ints, so I was going off that. Thanks for setting the record straight. I'm actually trying to get a count of the number of trade routes that pass through a given tile - any ideas?

You could loop through all players and use pPlayer:GetInternationalTradeRoutePlotToolTip(pPlot), then count the number of lines in the resulting text.
 
Cool, thanks for the input! If that's a "tenuous best", does that mean it'll be a computationally expensive and inefficient way to do things? You say the C++ has a method exactly for this that we can't get at with lua... is there another way to use that method? (...he asked, fully aware of whoward's DLL superpowers)

Ideally I'd like cities to get gold for trade units passing through the city, or through trade post-improved plots worked by the city. Maybe that can be done by looking at when caravans are on a city plot instead?
 
Any lua is computationally expensive relative to C++, so it'll depend on how often you call it - once per player per turn is not going to kill the system. Just make sure that if the specific civ isn't in play not to hook the event(s).

"Tenuous best" - loop all players, call an api to get a number of text strings, loop strings checking for non-empty ones and count them ... when there's a C++ method that does it for you. Not what I'd call "best"!

V76 of my DLL (not out yet) exposes that method to the Lua API

If you want to know when a caravan passes through a city, use the UnitSetXY event handler, check for the plot being a city, check for the unit being a caravan, if both true, do something. Again, make sure you only hook the event if the civ is in play as UnitSetXY can get called many hundreds of times per turn mid to late game.

If you want to add gold for worked improvements, you can loop all the city plots at the start of the civ players turn, check if it's worked, check for the improvement, and just add gold to the treasury - there'd be no tooltip info, but that may or may not be a big deal - you could always send a notification "You received X gold from taxes on trading-posts"
 
So, This is what I came up with so far:
Spoiler :
function TradeInCities(iPlayerID, iUnitID, iX, iY)
local pPlot = Map.GetPlot(iX, iY)
local pPlayer = Players[iPlayerID]
local pUnit = pPlayer:GetUnitByID(iUnitID)
if pUnit:GetUnitType() == GameInfoTypes["UNIT_CARAVAN"] then --or GameInfoTypes["UNIT_CARGO_SHIP"] then
if pPlot:IsCity() then
local pPlotOwner = pPlot:GetOwner()
pPlotOwner:ChangeGold(100)
end
end
end

GameEvents.UnitSetXY.Add(TradeInCities)


I get two errors which I don't understand, and are probably related:

attempt to index local pPlotOwner (a number value)
attempt to index local pPlot (a nil value)

I will keep working on this, but what I principally don't understand is how pPlot can have no value - pPlot is only called when a caravan moves, and a caravan surely has to move onto a plot that has an x and y value?
 
Ok, so it should have been:
local pPlotOwner = Players[pPlot:GetOwner()]

It works now (super OP, but I'll balance it later so that the payout is dependent on the length of the route)

However, still getting the occasional error about indexing pPlot... what's happening there, please? Do I just check pPlot with an if statement, or will it cause problems down the line?


edit: aha, beat me to it - thanks Klisz!
 
  1. UnitSetXY does not only fire when a unit moves from tile #23 to tile #24. There are additional conditions under which it fires. It also fires when a unit is created, for example.
  2. You are getting occasional firings of UnitSetXY when there is no usable info for X,Y being sent to the event, so the Map.GetPlot(iX, iY) method is giving you 'nil' for pPlot. I would wrap the rest of the code in a check for whether pPlot got valid data:
    Code:
    local pPlot = Map.GetPlot(iX, iY)
    if pPlot then
           --rest of your code would go here
    end
 
Awesome, thanks for that. I'll if-squash that error and balance out the yields, and the trade part will be finished :)
 
Back
Top Bottom