• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Can I Clone A Unit ?

Status
Not open for further replies.
Level 9
Joined
Apr 28, 2009
Messages
538
It's possible to clone a unit?

The clone should have the exact amount of hp, mana, hp max, mana max and also, it should have the exact same items in inventory of course

Cand a hero be cloned too?
Thanks.
 
Level 11
Joined
Feb 14, 2009
Messages
884
You can create a unit with triggers, and give it the same items as the original unit. You can adjust hp, mana and whatever else you need by using triggers too.
 
Level 9
Joined
Apr 28, 2009
Messages
538
i want the clone to have the same items and etc.
Also, i want the clone to have a different attack type.
 
Level 13
Joined
Mar 4, 2009
Messages
1,156
set UNIT = (unit that you want to copy)
create 1 unit of type (UNIT)
set mana of (last created unit) to mana of UNIT
set health of (last created unit) to health of UNIT
set experience of (last created unit) to experience of UNIT
create 1 item of type (item carried by UNIT in inventory slot 1) and give it to last created unit
<repeat for other slots>
 
Level 16
Joined
Jul 21, 2008
Messages
1,121
Here is the cloning function:
JASS:
function CloneUnit takes unit target, player owner returns unit
    local real x = GetUnitX(target)
    local real y = GetUnitY(target)    
    local unit u = CreateUnit(owner, GetUnitTypeId(target), x, y, GetUnitFacing(target))
    local integer item1 = GetItemTypeId(UnitItemInSlot(target, 0))
    local integer item2 = GetItemTypeId(UnitItemInSlot(target, 1))
    local integer item3 = GetItemTypeId(UnitItemInSlot(target, 2))
    local integer item4 = GetItemTypeId(UnitItemInSlot(target, 3))
    local integer item5 = GetItemTypeId(UnitItemInSlot(target, 4))
    local integer item6 = GetItemTypeId(UnitItemInSlot(target, 5))
    local integer level 
    if IsUnitType(target, UNIT_TYPE_HERO) then
        set level = GetHeroLevel(target) 
        if level > 1 then
            call SetHeroLevel(u, level, false)
        endif    
    endif   
    call SetWidgetLife(u, GetWidgetLife(target))
    call SetUnitState(u, UNIT_STATE_MANA, GetUnitState(target, UNIT_STATE_MANA))
    call UnitAddItemById(u, item1)                
    call UnitAddItemById(u, item2)
    call UnitAddItemById(u, item3)
    call UnitAddItemById(u, item4)
    call UnitAddItemById(u, item5)
    call UnitAddItemById(u, item6)
    return u
endfunction

It returns unit, so you can store the clone to unit variable.

  • -------- Copy clone function into map custom script --------
  • -------- It can be called in two ways from GUI --------
  • Custom script: set udg_Clone = CloneUnit(WhichUnit, WhichPlayer)
  • Custom script: CloneUnit(WhichUnit, WhichPlayer)
 
Level 9
Joined
Apr 28, 2009
Messages
538
Child_0f_Bodom sadly, i don't know jass. I understand the code however but i don't know how can i set "WhichUnit" to "triggering unit" and "WhichPlayer" to "triggering player"

This code will ONLY be called when a unit aquires an item. If the class of the item is...let's say..pergament then the function will be called.

And one more thing: could you modify the code so that the cloned unit is removed instantly?
Thanks.
 
Level 16
Joined
Jul 21, 2008
Messages
1,121
Triggering Player is GetTriggeringPlayer()
Triggering Unit is GetTriggerUnit()
And owner of Triggering Unit is GetOwningPlayer(GetTriggerUnit())

And one more thing: could you modify the code so that the cloned unit is removed instantly?

Since cloned unit can be stored in variable like this
  • Custom Script: Set udg_<YourUnitVariableName> = CloneUnit(GetTriggerUnit(), GetOwningPlayer(GetTriggerUnit()))
there is no point in addind "remove clone" to my script because you can simply do it via GUI trigger.
 
Level 9
Joined
Apr 28, 2009
Messages
538
and it's possible the make clone have different attack type?
(ex: the cloned unit have chaos attack type, but the clone have pierce attack type)
 
Level 9
Joined
Jul 3, 2008
Messages
495
Why so much triggers work, when you just can use the game cache.

Remember to create a game cache first.

  • Game Cache - Store (Triggering unit) as Clone of MyMap in (Last created game cache)
  • Game Cache - Restore Clone of MyMap from (Last created game cache) for Player 1 (Red) at (Center of (Playable map area)) facing 0.00
 
Status
Not open for further replies.
Top