Save and Load

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
I've got this algorithm:

PHP:
local milRewardCurrent = (LoadPlayer(player, "milRewardCurrent") or 0) + 1
log:Error("%25s %15s current=%i threshold=%i", "GiveMilitaristicRewards", player:GetName(), milRewardCurrent, milRewardThreshold)

if milRewardCurrent >= milRewardThreshold then
  milRewardCurrent = milRewardCurrent - milRewardThreshold

  -- give a free unit (no more interaction with the milReward variables)
end

SavePlayer(player, "milRewardCurrent", milRewardCurrent)
PHP:
function LoadPlayer(player, key)
  return saveDB.GetValue(string.format("%s_player%s_%s", MOD_ID, player:GetID(), key))
end

function SavePlayer(player, key, value)
  return saveDB.SetValue(string.format("%s_player%s_%s", MOD_ID, player:GetID(), key), value)
end
Some output:
Code:
ERROR GiveMilitaristicRewards Montezuma current=11 threshold=10
(free unit)
ERROR GiveMilitaristicRewards Montezuma current=11 threshold=10
(free unit)
How is it possible to not reset the current value? Here's the full text:
Spoiler :
PHP:
local milRewardThreshold = UnofficialPatch.MINOR_CIV_MILITARISTIC_BASE_TURNS / rateMod
      milRewardThreshold = milRewardThreshold * GameInfo.GameSpeeds[Game.GetGameSpeedType()].TrainPercent / 100
local milRewardCurrent = (LoadPlayer(player, "milRewardCurrent") or 0) + 1
log:Error("%25s %15s current=%i threshold=%i new=%i", "GiveMilitaristicRewards", player:GetName(), milRewardCurrent, milRewardThreshold, milRewardCurrent - milRewardThreshold)
if milRewardCurrent >= milRewardThreshold then
  milRewardCurrent = milRewardCurrent - milRewardThreshold
  local availableIDs  = GetAvailableUnitIDs(capitalCity)
  local newUnitID     = availableIDs[1 + Map.Rand(#availableIDs, "InitUnitFromList")]
  local capitalPlot   = capitalCity:Plot()
  local xp            = GetCitystateYields(player, MinorCivTraitTypes.MINOR_CIV_TRAIT_MILITARISTIC, 2)[YieldTypes.YIELD_EXPERIENCE].Total

  if GameInfo.Units[newUnitID].Domain ~= "DOMAIN_LAND" then
    xp = xp * UnofficialPatch.MINOR_CIV_MILITARISTIC_XP_NONLAND_PENALTY
  end

  log:Debug("  Reward=%s  XP=%s", GameInfo.Units[newUnitID].Type, xp)
  local newUnit = M_InitUnit(player, newUnitID, capitalPlot, xp)

  local promotion = GetTrait(player).MilitaristicCSFreePromotion

  if promotion then
    newUnit:SetHasPromotion(GameInfo.UnitPromotions[promotion].ID, true)
  end
  

  local newUnitInfo = GameInfo.Units[newUnitID]
  if Game.GetActivePlayer() == player:GetID() then
    local newUnitIcon = {{"Unit1", newUnitID, 0, 0, 0}}
    local newUnitName = Locale.ConvertTextKey(newUnitInfo.Description)
    CustomNotification(
      "RewardMilitaristic",
      "New "..newUnitName,
      "A new "..newUnitName.." arrived in your [ICON_CAPITAL] Capital from your militaristic [ICON_CITY_STATE] City-State allies.",
      capitalPlot,
      0,
      0,
      newUnitIcon
    )
  end
end
SavePlayer(player, "milRewardCurrent", milRewardCurrent)
 
I don't have access to your files (and just starting with modding), but spamming more debug-code might help in this case. As far as I can tell the code you posted is correct, but I'm not a LUA-specialist so might be wrong. To verify I'd log the value of 'milRewardCurrent' just before calling SavePlayer(player, "milRewardCurrent", milRewardCurrent), to test whether SavePlayer gets the right value.

If that's ok, then log the value of 'value' inside SavePlayer. If that is also correct, the problem seems to be inside SaveDB.setValue


EDIT: Just to be sure: is the bug consistent?
 
Tried it myself by loading your mod, but I can't reproduce it. Do you have a save or a sure-fire way to make it happen?
 
I haven't been able to replicate the problem; I'm going off a bug report of a user of the mod. It might be there's some secondary problem causing this one, though it's strange it could happen at all... the algorithm seems straightforward. I guess if people keep reporting this bug I'll investigate further. :crazyeye:
 
Hmm, annoying. I suppose the user doesn't have a save either... It is indeed strange that it happens at all, that's why I'm annoyed at not being able to solve it ;)
 
~Found your utilities...so question asked and answered~

local promotion = GetTrait(player).MilitaristicCSFreePromotion

Thal....love your VEM and the GK version looks very promising!

I hate to bug you with such niggling details, but I'm trying to pull player traits for a small mod, and I can't get your method above to work for me. I can't find a GetTrait player method in the API documentation. Is this a function you developed?

I tried your other trait pull method, which also doesn't work for me.

Code:
local trait = player:GetTraitInfo()

Any suggestions?
 
I can't find a GetTrait player method in the API documentation. Is this a function you developed?
There is no need for such a function in the API because the traits are static data and you can use GameInfo and such to retrieve them.

Player -> Civilization ID (through Player.GetCivilizationType) -> Civilization type (through GameInfo.Civilizations) -> Leader type (through GameInfo.Civilization_Leaders) -> all traits types (through GameInfo.Leader_Traits)

References:
* Lua and UI Reference (look at GameInfo especially)
* Civ5Cvilizations.xml
* Civ5LeadersTables.xml

Edit: err... The GameInfo content is missing... Well... I will fix this. Basically use things like
* GameInfo.Civilizations[civID].Type to get the type from the ID
* GameInfo.Civilization_Leaders{CivilizationType = civType}().LeaderType to get the first (and unique) leader for the given civtype.
 
Back
Top Bottom