LUA convert

kassambique

Chieftain
Joined
Jun 1, 2011
Messages
4
Hello, I'm trying to convert a unit (change the owner civ). Do you know I can proceed, I've tried many ways like using the SendGiftUnit function or a hypothetical convert function I read about in a reference but nothing seems to work. Can somebody can me ?
 
I know the 1066 scenario does it; I've not looked at it in detail. They do some fairly complex stuff with it.
 
I know the 1066 scenario does it; I've not looked at it in detail. They do some fairly complex stuff with it.

The 1066 code doesn't allow for prior experience, I have a function in my "Units - Herdsmen" mod which converts a unit from one type to another type for the same player, a quick change for same type for a different player prorduces

Code:
function convertUnitOwner(pUnit, pNewOwner)
  print(string.format("convertUnit(%s, %s)", pUnit:GetName(), pNewOwner:GetName()))

  local pNewUnit = pNewOwner:InitUnit(pUnit:GetUnitType(), pUnit:GetX(), pUnit:GetY())
  pNewUnit:SetOriginalOwner(pUnit:GetOwner());
  pNewUnit:SetDamage(pUnit:GetDamage())
  pNewUnit:SetExperience(pUnit:GetExperience())
  pNewUnit:SetLevel(pUnit:GetLevel())

  for unitPromotion in GameInfo.UnitPromotions() do
    local iPromotionID = unitPromotion.ID;
        
    if (pUnit:IsHasPromotion(iPromotionID)) then
      if (pNewUnit:IsPromotionValid(iPromotionID)) then
        pNewUnit:SetHasPromotion(iPromotionID, true)
      end
    end
  end

  return pNewUnit
end

This doesn't allow for saved data (ScriptData is broken for units anyway), but if you're storing data for the unit in the modding db be aware that the unit id WILL change

HTH

William
 
Back
Top Bottom