Creating units?

How can I create X units on a plot, and give them Y experience?

You can create as many units as you like on a plot, regardless of stacking limit, the player just won't be able to finish their turn until they have moved them. If this is an issue, the pUnit:JumpToNearestValidPlot() method is your friend.

To create a unit with experience, just create the unit and give it some experience!

Code:
function createUnit(pPlayer, pPlot, sNewUnitType, iExperience)
  local pNewUnit = pPlayer:InitUnit(GameInfoTypes[sNewUnitType], pPlot:GetX(), pPlot:GetY())

  pNewUnit:SetExperience(iExperience)

  return pNewUnit
end

If you don't want the unit to immediately request promotions, you'll also need to set the level

Code:
function setLevel(pUnit, iExperience)
  local iLevel = 1
  local iLevelIncrement = iLevel * 10

  while (iExperience > iLevelIncrement) do
    iExperience = iExperience - iLevelIncrement
    iLevel = iLevel + 1
    iLevelIncrement = iLevel * 10
  end

  pUnit:SetLevel(iLevel)
end

HTH

W
 
I didn't realize InitUnit had a return value, thank you, that was the missing information I needed. I'd been looping through units on the plot to search for the recently-created unit but that sometimes had problems. :)
 
If you have them, there is a lot of useful code in the Spanish/Inca and Viking scenarios that deal with unit creation
 
Code:
function setLevel(pUnit, iExperience)
  local iLevel = 1
  local iLevelIncrement = iLevel * 10

  while (iExperience > iLevelIncrement) do
    iExperience = iExperience - iLevelIncrement
    iLevel = iLevel + 1
    iLevelIncrement = iLevel * 10
  end

  pUnit:SetLevel(iLevel)
end
HTH

W[/QUOTE]

what does SetLevel do? Could you give me some short description?
 
unit:SetLevel(x) is different from SetLevel(unit, x)

I'd call the latter SetLevelByXP(unit, x) for clarification. The level and experience of a unit are independent values set separately. If you set the level, it has no effect on experience, and vice versa. The function whoward69 copied above basically figures out "if a unit has X experience, what level should it be?"
 
so unit level is just a number of used promotion? if i set unit level to e.g 2 unit:SetLevel(2) it means that unit will get first/next promotion at 30 experience? or it result that unit will be ask for first necessary promotion at 30 experience and first, second promotion could be save even when saving promotion is not checked?
 
so unit level is just a number of used promotion? if i set unit level to e.g 2 unit:SetLevel(2) it means that unit will get first/next promotion at 30 experience? or it result that unit will be ask for first necessary promotion at 30 experience and first, second promotion could be save even when saving promotion is not checked?

I did do a lot of digging into promotions, xp and levels, and how they interact, but have forgotten most of it!

I modded the Live Tuner Selected Unit panel to add the ability to pick promotions and set the level (which is what I used to explore the functionality with) - see http://forums.2kgames.com/showthread.php?108142-MOD-Live-Tuner-Panels

 
Back
Top Bottom