- Joined
- May 22, 2007
- Messages
- 30
Ok I'm new to JASS so go easy on me. I want to create a function that will replace my unit with another when it uses an item, and when they drop the item it will turn them back to the original unit. Only problem is I need to make sure they give the unit's items to the unit replacing them.
I found ReplaceUnitBJ but I don't understand how to get it to work,
...and of course after that comes the nulling but I still need to know: how do you get this to work? I have tried the tutorials but I still can't understand.
What I do know is I need to set it to loop through the inventory even if it isn't a hero.
I just need to know how to make it get my replaced unit (ID of 'z000') from my original unit (ID of 'hpea', the peasant). This may seem blindingly obvious to some people but I have been stuck on this for awhile. If anyone could point me to anything that has all the handles, integers, reals, and all the other variables and explanations of each I'd appreciate it.
And if you know a better way to replace a unit and keep their items please tell me >.< although more than likely you'll yell at me about your way and how its better.
I found ReplaceUnitBJ but I don't understand how to get it to work,
JASS:
function ReplaceUnitBJ takes unit whichUnit, integer newUnitId, integer unitStateMethod returns unit
local unit oldUnit = whichUnit
local unit newUnit
local boolean wasHidden
local integer index
local item indexItem
local real oldRatio
// If we have bogus data, don't attempt the replace.
if (oldUnit == null) then
set bj_lastReplacedUnit = oldUnit
return oldUnit
endif
// Hide the original unit.
set wasHidden = IsUnitHidden(oldUnit)
call ShowUnit(oldUnit, false)
// Create the replacement unit.
if (newUnitId == 'ugol') then
set newUnit = CreateBlightedGoldmine(GetOwningPlayer(oldUnit), GetUnitX(oldUnit), GetUnitY(oldUnit), GetUnitFacing(oldUnit))
else
set newUnit = CreateUnit(GetOwningPlayer(oldUnit), newUnitId, GetUnitX(oldUnit), GetUnitY(oldUnit), GetUnitFacing(oldUnit))
endif
// Set the unit's life and mana according to the requested method.
if (unitStateMethod == bj_UNIT_STATE_METHOD_RELATIVE) then
// Set the replacement's current/max life ratio to that of the old unit.
// If both units have mana, do the same for mana.
if (GetUnitState(oldUnit, UNIT_STATE_MAX_LIFE) > 0) then
set oldRatio = GetUnitState(oldUnit, UNIT_STATE_LIFE) / GetUnitState(oldUnit, UNIT_STATE_MAX_LIFE)
call SetUnitState(newUnit, UNIT_STATE_LIFE, oldRatio * GetUnitState(newUnit, UNIT_STATE_MAX_LIFE))
endif
if (GetUnitState(oldUnit, UNIT_STATE_MAX_MANA) > 0) and (GetUnitState(newUnit, UNIT_STATE_MAX_MANA) > 0) then
set oldRatio = GetUnitState(oldUnit, UNIT_STATE_MANA) / GetUnitState(oldUnit, UNIT_STATE_MAX_MANA)
call SetUnitState(newUnit, UNIT_STATE_MANA, oldRatio * GetUnitState(newUnit, UNIT_STATE_MAX_MANA))
endif
elseif (unitStateMethod == bj_UNIT_STATE_METHOD_ABSOLUTE) then
// Set the replacement's current life to that of the old unit.
// If the new unit has mana, do the same for mana.
call SetUnitState(newUnit, UNIT_STATE_LIFE, GetUnitState(oldUnit, UNIT_STATE_LIFE))
if (GetUnitState(newUnit, UNIT_STATE_MAX_MANA) > 0) then
call SetUnitState(newUnit, UNIT_STATE_MANA, GetUnitState(oldUnit, UNIT_STATE_MANA))
endif
elseif (unitStateMethod == bj_UNIT_STATE_METHOD_DEFAULTS) then
// The newly created unit should already have default life and mana.
elseif (unitStateMethod == bj_UNIT_STATE_METHOD_MAXIMUM) then
// Use max life and mana.
call SetUnitState(newUnit, UNIT_STATE_LIFE, GetUnitState(newUnit, UNIT_STATE_MAX_LIFE))
call SetUnitState(newUnit, UNIT_STATE_MANA, GetUnitState(newUnit, UNIT_STATE_MAX_MANA))
else
// Unrecognized unit state method - ignore the request.
endif
// Mirror properties of the old unit onto the new unit.
//call PauseUnit(newUnit, IsUnitPaused(oldUnit))
call SetResourceAmount(newUnit, GetResourceAmount(oldUnit))
// If both the old and new units are heroes, handle their hero info.
if (IsUnitType(oldUnit, UNIT_TYPE_HERO) and IsUnitType(newUnit, UNIT_TYPE_HERO)) then
call SetHeroXP(newUnit, GetHeroXP(oldUnit), false)
set index = 0
loop
set indexItem = UnitItemInSlot(oldUnit, index)
if (indexItem != null) then
call UnitRemoveItem(oldUnit, indexItem)
call UnitAddItem(newUnit, indexItem)
endif
set index = index + 1
exitwhen index >= bj_MAX_INVENTORY
endloop
endif
// Remove or kill the original unit. It is sometimes unsafe to remove
// hidden units, so kill the original unit if it was previously hidden.
if wasHidden then
call KillUnit(oldUnit)
call RemoveUnit(oldUnit)
else
call RemoveUnit(oldUnit)
endif
set bj_lastReplacedUnit = newUnit
return newUnit
endfunction
What I do know is I need to set it to loop through the inventory even if it isn't a hero.
I just need to know how to make it get my replaced unit (ID of 'z000') from my original unit (ID of 'hpea', the peasant). This may seem blindingly obvious to some people but I have been stuck on this for awhile. If anyone could point me to anything that has all the handles, integers, reals, and all the other variables and explanations of each I'd appreciate it.
And if you know a better way to replace a unit and keep their items please tell me >.< although more than likely you'll yell at me about your way and how its better.