Unit will upgrade if they reach certain levels.

EricNgui

CPU of 1980's Planeptune
Joined
Jul 23, 2014
Messages
138
Location
Liverpool, UK
Hello. Is everyone have manage to implent a way that a unit will upgrade if they reach certain levels? For example, Samurai will need to reach Level 2 to upgrade to Samurai 2.0. Thanks. :)
 
I've not tried this myself, but I've looked at how JFD implemented the upgrade from Bersaglieri to Garibaldino in his Sardinia-Piedmont civilization (since I wanted to use a similar thing myself eventually) and I know something like this can be done in Lua, at least.

However, how you implement it depends on what you want to do. There are a couple of possible routes you can go down:
  • Upgrade to Samurai 2.0 immediately when level 2 is reached. I don't know whether there's a GameEvent for gaining experience or reaching the next level, but you could check through each of the player's units at the end of their turn and convert eligible Samurai to Samurai 2.0 if they're at level 2.
  • Allow the player to choose a promotion that upgrades the unit (i.e. JFD's Bersaglieri to Garibaldino method). You would first create a promotion that doesn't do anything but can be chosen, add a hook to CanHavePromotion(), and then only allow the promotion you made to be picked if the unit is a Samurai and if it is at the required level. You would then add a hook to UnitPromoted(), and if the player picked the upgrade promotion, you would convert the unit to a Samurai 2.0.
In either case, though, you'll need to use the Convert() method to upgrade the unit. This doesn't actually convert a unit to a new type, but copies the unit's attributes (things like experience, promotions and damage) to a unit you specify before removing the original one. So you would have to create a Samurai 2.0 unit on the same tile as the original Samurai, and then use Convert() on the original unit with the new unit as a parameter.

(I've made an assumption that the Samurai 2.0 does not have some other prerequisite such as a technology or (extra) resource - that shouldn't be too hard to account for, though. You could potentially add some sort of cost as well, if you so desire.)

I apologise if you don't know much Lua and therefore much of that goes over your head, but I don't think there's a non-Lua way to do this, I'm afraid. I'm sure someone more knowledgable about modding will correct me if there is, though. :mischief:

EDIT: I noticed that you changed "Samurai 2.0" to "Musketman" while I was composing this post. If you want a Samurai to only be able to upgrade to a Musketman if it's at level 2, but otherwise upgrade like a normal unit, you may be able to hook onto CanHaveUpgrade(), check the level, and allow the upgrade if it's at level 2, but I've not seen that function used before so I can't say how well that would work (and I don't know if you need to check if the technology and gold required is correct too).
 
Top Bottom