CanHaveUpgrade doesn't work?

Typhlomence

Magical Tomomo
Joined
Mar 19, 2014
Messages
394
Location
Brisbane, Australia
I wanted to use a hook onto CanHaveUpgrade to only allow a certain unit to upgrade under specific circumstances. However, it seems like the game still allows me to upgrade the unit even if my hooked function goes down a path that returns false.

Even if hook a dummy function to CanHaveUpgrade that only returns false and should therefore prevent anything from upgrading, if I understand the function correctly, the game seems to ignore the result. It doesn't work when I use CL's Malaysia mod, either - I can upgrade workers to Pesilats even if there's no enemy unit within two tiles.

So does this hook actually work? I did clear my cache just to see if that was the issue, and still no luck... Or am I just misunderstanding how to use it?
 
What's the corresponding CanHaveAnyUpgrade event doing?
 
I wanted to use a hook onto CanHaveUpgrade to only allow a certain unit to upgrade under specific circumstances.

Doh! Read the whole post William!

If you don't want a unit that could otherwise be upgraded at this particular moment to upgrade, you need to hook CanHaveAnyUpgrade and return false

By the time CanHaveUpgrade is called, the game core is going to upgrade the unit, the CanHaveUpgrade event permits you to determine which one of N upgrades it can have.
 
Ah! So I use CanHaveAnyUpgrade to determine whether the unit is able to upgrade at all, and CanHaveUpgrade is for choosing between upgrades, then? I didn't know a unit could have multiple upgrade choices.

Anyway, while I have to test whether my code allows the upgrade at the right time, it at least blocks it when the unit shouldn't upgrade now, so thank you whoward! :)

XiHJNP9.png

(It would be nice if I could put a warning in the tooltip saying why it can't upgrade, but I expect that needs some sort of UI mod.)
 
I didn't know a unit could have multiple upgrade choices

See my "Units - Multiple Upgrades" mod.

Re the tooltip - if you're using the VMC or CP DLL, you don't need a UI upgrade as you can do it all with custom mission events
 
So CanHaveUpgrade isn't a boolean for if a unit can upgrade or not?
 
It's an event that returns a boolean that determines if the unit can have the specific upgrade passed as one of the parameters.
 
It's an event that returns a boolean that determines if the unit can have the specific upgrade passed as one of the parameters.

So it tells you if the unit can ever upgrade to the specific UnitClass, rather than blocking it from upgrading to that unit until certain parameters are met?
 
It doesn't TELL you anything.

Test Any/All events ASK if something can happen - "can this unit upgrade to this class at the moment"
 
It doesn't TELL you anything.

Test Any/All events ASK if something can happen - "can this unit upgrade to this class at the moment"

So if I wrote this:

Code:
function WhowardTest(playerID, unitID, upgradeUnitClass, upgradeUnitType)
	local pPlayer = Players[playerID]
	local pUnit = pPlayer:GetUnitByID(unitID)
	if pUnit:GetUnitType() == GameInfoTypes.UNIT_KNIGHT and upgradeUnitType == GameInfoTypes.UNITCLASS_CAVALRY then
		if pPlayer:GetJONSCulture() >= 100 then
			return true
		else
			return false
		end
	end
end

GameEvents.CanHaveUpgrade.Add(WhowardTest)

Then it would allow the Knight to upgrade to the Lancer if the player's culture is greater than or equal to 100. However, if the culture was less than 100, then the Knight would have to wait until Cavalry. Is that what it's for?
 
Then it would allow the Knight to upgrade to the Lancer if the player's culture is greater than or equal to 100.
No.

Refer to the "Units - Multiple Upgrades" mod to see how to use this event.
 
Back
Top Bottom