• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Why does my script only run once?

Status
Not open for further replies.
I'm working with a map that uses a grouping system in which you can replace one of the standard units in your group whith a "special" unit.
For finding a standard unit in a group i use this script:

JASS:
function unitReplace takes unit u, integer new returns nothing
    local location l = GetUnitLoc(u)
    local integer d = GetUnitUserData(u)
    call BJDebugMsg(I2S(d)+" SPECIAL TYPE")
    call DestroyEffectTimed(AddSpecialEffectLoc("Abilities\\Spells\\Undead\\DeathPact\\DeathPactTarget.mdl", l), 2.)
    call ReplaceUnitBJ(u, new, bj_UNIT_STATE_METHOD_RELATIVE ) //BJ OH NO!!!!
    call GroupAddUnit(udg_sq_g[d], GetLastReplacedUnitBJ())
    call SetUnitUserData(GetLastReplacedUnitBJ(), d)
    call BJDebugMsg(R2S(GetLocationY(l))+R2S(GetLocationX(l))) //those are just debug stuff
    call RemoveLocation(l)
    set l = null
endfunction

As you can see, i use the units custom value as the group array index.
The array sq_g holds the group.

Note that DestroyEffectTimed is a custom function.

This works brilliantly the first time, though after that it wont find any more units even if there are some idle. Even if the replaced unit dies, it will still only work once.

The script is triggered through this line:

  • Add Specialist
    • Events
      • Unit - A unit Uses an item
    • Conditions
      • (Item-type of (Item being manipulated)) Equal to Add Specialist
    • Actions
      • Custom script: call unitReplace(getFreeUnit(GetTriggerUnit()), 'h003')
How do i make this script work properly?
Please help.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Firstly, the problem may be in the getFreeUnit() function which you did not post.
Secondly, be sure to note that Replacing a unit destroys the orignal and makes a whole brand new unrealted unit at its position.
Finally I do not believe the unitReplace function is the problem directly as it has nothing to do with actually getting units from the group.

Thus more information is needed inorder to help you.
 
Firstly, the problem may be in the getFreeUnit() function which you did not post.
Secondly, be sure to note that Replacing a unit destroys the orignal and makes a whole brand new unrealted unit at its position.
Finally I do not believe the unitReplace function is the problem directly as it has nothing to do with actually getting units from the group.

Thus more information is needed inorder to help you.


Sorry, i think i acciddently posted the wrong code snippet even; here's GetFreeUnit:

JASS:
//==CHECK AVAILABLE UNITS==//
function getFreeUnit takes unit u returns unit
    local unit temp = null
    local group g = CreateGroup()
    if GetUnitUserData(u)!=0 then
    set g = udg_sq_g[GetUnitUserData(u)]
    loop
    exitwhen GetUnitTypeId(temp) == 'h002'
    set temp = FirstOfGroup(g)
    if GetUnitTypeId(temp) != 'h002' then
    call GroupRemoveUnit(g, temp) 
    else
    call DestroyGroup(g)
    set g = null
    return temp
endif 
    endloop
    
    endif
    call DestroyGroup(g)
    set g = null
    return temp

And also, should i remove the unit from the group before i replace it?
Or what do you mean? In my code i do guess i declared a new custom value and group belongance for last replaced unit, so it will hold the same information, will it not?
Thanks beforehand.
 
Status
Not open for further replies.
Top