• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Find a unit previously targeted by an order

Status
Not open for further replies.
Level 4
Joined
Jun 8, 2007
Messages
89
I am making a map similar to the worm war map that comes as a scenario with Warcraft 3: TFT. Anyway, I'm having trouble removing just one unit from the "worms" tail end. I currently have a global array to store the head of the worm for each player, and a global array to store the tail unit (last unit only) for each player. When I add a segment to the worm, I set that new segment to the tail variable after ordering the new segment to follow the segment in front of it. My question is this:
Is there any way that I could get the unit that is currently targeted (being followed) by the tail unit? I need to be able to remove a single unit from the end of the worm. Or maybe is there some other way that I am overlooking?

-Thanks in advance
 
Level 9
Joined
Mar 25, 2005
Messages
252
Sounds like an issue best solved with dynamic arrays.

In this case you need an array that holds the amount of segments of a worm and another array that holds all segments of a worm.
JASS:
globals
    integer array Worm_length // amount of segments
    unit array Worm_segment
endglobals

Here is how you would add a segment to a worm:
JASS:
function WormAddSegment takes player whosWorm, unit segment returns nothing
    local integer playerId = GetPlayerId(whosWorm)
    set Worm_length[playerId] = Worm_length[playerId] + 1
    call IssueTargetOrderById(segment, 851986, Worm_segment[playerId*1024 + Worm_length[playerId]])
    set Worm_segment[playerId*1024 + Worm_length[playerId]] = segment
endfunction

and here is how you would get the tail unit:
JASS:
function WormGetTail takes player whosWorm returns unit
    return Worm_segment[GetPlayerId(whosWorm)*1024 + Worm_length[GetPlayerId(whosWorm)]]
endfunction

and here is how you would remove the tail unit:
JASS:
function WormRemoveTail takes player whosWorm returns unit
    local integer playerId = GetPlayerId(whosWorm)
    set Worm_length[playerId] = Worm_length[playerId] - 1
    return Worm_segment[playerId*1024 + Worm_length[playerId] - 1]
endfunction
Actually the above function just removes the tail only from the worm - not from the game. It however also returns the removed unit so if you want to get rid of it completely you can always call KillUnit(WormRemoveTail(whosWorm))

Here is a function you might or might not need:
JASS:
function WormGetSegmentAt takes player whosWorm, integer n returns unit
    return Worm_segment[GetPlayerId(whosWorm)*1024 + n]
endfunction

WormGetSegmentAt(whosWorm, 1) would return the first segment.

Using dynamic arrays is just one of many ways your problem can be solved, but I believe that this is the best solution. Oh and btw you need JassHelper for this to work because of the globals block. However they can easily be replaced with user defined ones (as in globals made with the GUI).

PS: I checked that all of these pass a syntax check but I can't be sure that they work ingame
 
Level 4
Joined
Jun 8, 2007
Messages
89
Yeah, I was trying to stick to an array that could do it for all 12 players, but I guess this works. Thanks

*EDIT* I guess I should have read the whole thing. That works pretty much perfectly for what I want. Thanks.
 
Status
Not open for further replies.
Top