• 🏆 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!

[Spell] Unit interacting with a created object

Status
Not open for further replies.
Level 3
Joined
Aug 17, 2009
Messages
33
Hello,

I am currently trying to create a Spell which by chance creates little objects randomly placed around a hero (with a certain ability/buff) when attacking another unit. When the hero comes in contact with these objects (which disappear after 10 seconds) he will get healed for a certain amount, consuming the object.

I could successfully create a piece of code which creates these objects (here I call them Healing Spheres) how I want them with a 1/3 chance upon every auto-attack the hero performs. They are placed within a rectangle with a width and height of 600 centered around the hero.

Code:
function Trig_Healing_Spheres_Conditions takes nothing returns boolean
    if ( not ( UnitHasBuffBJ(GetAttacker(), 'B000') == true ) ) then
        return false
    endif
    return true
endfunction

function Trig_Healing_Spheres_Actions takes nothing returns nothing
    local unit u = GetAttacker()
    local location l = GetUnitLoc(u)
    local location randomLocation = GetRandomLocInRect(RectFromCenterSizeBJ(GetUnitLoc(GetAttacker()), 600.00, 600.00))
   
    if (GetRandomInt(0, 2) == 0) then
        call CreateUnitAtLoc(GetOwningPlayer(u), 'u000', randomLocation, 0)
    endif   
endfunction

//===========================================================================
function InitTrig_Healing_Spheres takes nothing returns nothing
    set gg_trg_Healing_Spheres = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Healing_Spheres, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Healing_Spheres, Condition( function Trig_Healing_Spheres_Conditions ) )
    call TriggerAddAction( gg_trg_Healing_Spheres, function Trig_Healing_Spheres_Actions )
endfunction

Thanks to the local variables I can have multiple identical heroes perform attacks and each create these spheres on their own.

With a simple GUI I pick every healing sphere every second of game time and subtract one hitpoint (from their starting 10) with kills them after 10 seconds.

This only leaves me with the consumption of the spheres missing. I basically want the hero (or any hero with the necessary buff to create them) to be able to walk over to a live healing sphere and as soon as they come "in contact/within a certain range" consume the sphere and receive healing. Though I don't know how I am supposed to approach this.

I know you can create an event which triggers as soon as any unit comes within a certain range of a certain unit, but since I create these spheres in-game there is no global variable I can use to refer to any of the spheres. What I think I would need is an event which triggers as soon as any unit comes within a certain range of any other unit and be able to refer to these units as unit1 and unit2 so I can check unit1 for the buff and unit2 if it is a sphere. Then I could kill unit2 and gives healing to unit1.

I thought creating an array of units to temporarily store the spheres in may be impractical since I don't want to have an upper arbitrary limit on them.

I hope I was able to bring my idea across. Any help is appreciated, thank you.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Your trigger leaks.

JASS:
function Trig_Healing_Spheres_Conditions takes nothing returns boolean
    return UnitHasBuffBJ(GetAttacker(), 'B000')
endfunction

function Trig_Healing_Spheres_Actions takes nothing returns nothing
    local unit u = GetAttacker()
    local location l = GetUnitLoc(u)
    local rect r = RectFromCenterSizeBJ(l, 600.00, 600.00)
    local location randomLocation = GetRandomLocInRect(r)
 
    if (GetRandomInt(0, 2) == 0) then
        call CreateUnitAtLoc(GetOwningPlayer(u), 'u000', randomLocation, 0)
    endif 

    call RemoveLocation(l)
    call RemoveRect(r)
    call RemoveLocation(randomLocation)
 
    set u = null
    set l = null
    set r = null
    set randomLocation = null
endfunction

//===========================================================================
function InitTrig_Healing_Spheres takes nothing returns nothing
    set gg_trg_Healing_Spheres = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Healing_Spheres, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Healing_Spheres, Condition( function Trig_Healing_Spheres_Conditions ) )
    call TriggerAddAction( gg_trg_Healing_Spheres, function Trig_Healing_Spheres_Actions )
endfunction
I did not remove the BJs, only the leaks.

In your GUI trigger which handles duration you can also search for nearby units and heal/remove the items as appropriate.
 
Level 3
Joined
Aug 17, 2009
Messages
33
Okay I thought this may be an inefficient solution but I put a pick every of my hero-type into the pick every sphere while storing the sphere (picked unit) one in a global variable. I first made the mistake of "killing" the sphere instead of removing it which made the death animation of it still give health, but that's fixed now.

It's working like a charm, thank you for the help!
 
Status
Not open for further replies.
Top