I wish we could trace execution...

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
So I have a function:

PHP:
playerCityRewards = {}

function GetCityRewards(majorCivID, doUpdate)
  if doUpdate then
    print("Update")
    playerCityRewards[majorCivID] = {}
    ...do stuff...
    playerCityRewards[majorCivID][cityIDs[k]] = {food=v, culture=0, spawn=0}
  else
    print("Do nothing")
  end
  print(Players[majorCivID]:GetName().." is "..tostring(playerCityRewards[majorCivID]))
  return playerCityRewards[majorCivID]
end
I call it twice:

GetCityRewards(majorCivID, true)
GetCityRewards(city:GetOwner())

The output is:

Update
Ramkhamhaeng is table: 15BF0938
Do nothing
Ramkhamhaeng is nil

These are the only times the playerCityRewards variable is referenced. The global variable's defined one moment, I do nothing, and it becomes nil... arggh! :wallbash:

What did I do wrong?
 
Not enough info. From where are you calling? In which context is the calling function and the called function?
 
Here's the entirety of it. The "Modded" functions replace the originals. GiveMinorCivAllianceRewards is called at the start of each turn and builds the table. The other functions work fine if I set doUpdate=true for every one of them (rebuilding the whole table), but otherwise they always get a nil.

Or for a more readable version, see the attached folder's UP_General.lua

PHP:
function GiveMinorCivAllianceRewards()
  print("GiveMinorCivAllianceRewards()")
  for majorCivID,majorCiv in pairs(Players) do
    if IsValidPlayer(majorCiv) and not majorCiv:IsMinorCiv() then
      for cityID,v in pairs(GetAllCityRewards(majorCivID, true)) do
        local city = majorCiv:GetCityByID(cityID)
        print(city:GetName()..": "..city:GetFood().." +"..v.food)
        city:ChangeFood(v.food)
        if city:GetFood() >= city:GrowthThreshold() then
          city:ChangePopulation(1,true)
        end
      end
    end
  end
end

playerCityIDs = {}
playerCityRewards = {}
minorCivRewards = {}

function ModdedFoodDifferenceTimes100(city)
  return math.ceil(city:FoodDifferenceTimes100() + GetCityRewards(city)*100)
end

function ModdedFoodDifference(city)
  return math.ceil(city:FoodDifference() + GetCityRewards(city))
end

function GetCityRewards(city)
  local rewards = GetAllCityRewards(city:GetOwner(), true)
  rewards = rewards and rewards[city:GetID()]
  return rewards and rewards["food"] or 0
end

function GetAllCityRewards(majorCivID, doUpdate)
  if doUpdate then
    --print("Update")
    playerCityRewards[majorCivID] = {}
    local majorCiv = Players[majorCivID]
    local minorCivRewards = GetMinorCivAllianceRewards(majorCivID, doUpdate)
    
    local foodForCities = {}
    local food = minorCivRewards["food"]
    local noVeryUnhappyGrowth = (GameDefines["UNHAPPY_GROWTH_PENALTY"] == -100)
    if food > 0 and not (noVeryUnhappyGrowth and majorCiv:IsEmpireVeryUnhappy())  then
      local cityIDs = GetLargestCityIDs(majorCivID, doUpdate)
      local leaderType = GameInfo.Leaders[majorCiv:GetLeaderType()].Type
      local traitType = GameInfo.Leader_Traits("LeaderType ='" .. leaderType .. "'")().TraitType
      
      local modifier = 1 + GameInfo.Traits[traitType].CityStateBonusModifier / 100
      if majorCiv:IsEmpireUnhappy() then
        modifier = modifier * (1 + GameDefines["UNHAPPY_GROWTH_PENALTY"] / 100) 
      elseif majorCiv:IsEmpireVeryUnhappy() then
        modifier = modifier * (1 + GameDefines["VERY_UNHAPPY_GROWTH_PENALTY"] / 100)
      end
      food = math.ceil(food * modifier)
      
      --print(majorCiv:GetName().." maritime food: "..food.."  cityIDs: "..#cityIDs)

      local cityIndex = 1
      for cityIndex = 1, GameDefines["MARITIME_FOOD_CITIES"] do
        if cityIDs[cityIndex] ~= nil then
          table.insert(foodForCities, 0)
        end
      end
      --print(#foodForCities)
      cityIndex = 1
      while food > 0 do
        foodForCities[cityIndex] = foodForCities[cityIndex] + 1
        food = food - 1
        --print(cityIndex.." "..foodForCities[cityIndex])
        cityIndex = cityIndex % #foodForCities + 1
      end
      for k,v in pairs(foodForCities) do
        playerCityRewards[majorCivID][cityIDs[k]] = {food=v, culture=0, spawn=0}
      end
    end
  end
  --print(Players[majorCivID]:GetName().." rewards table = "..tostring(playerCityRewards[majorCivID]))
  return playerCityRewards[majorCivID]
end
 
9 times out of 10, this means you're trying to call a local across files.
 
Back
Top Bottom