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

[JASS] Function question

Status
Not open for further replies.
Level 3
Joined
Dec 20, 2007
Messages
44
I'm wondering a couple things, first of all will this function actually work, and also will it leak.

Basically the idea behind the function is to return a unit group of all the units that would lie inside a cone, so that cone-based attacks can be triggered properly.


JASS:
function GetCone takes unit caster, real distance, real maxspread, real minspread, group units returns nothing
    local group g = CreateGroup()
    local unit u = null
    local real x = GetUnitX(caster)
    local real y = GetUnitY(caster)
    local real face = GetUnitFacing(caster)
    
    local real fakeX = (maxspread/(maxspread-minspread)*distance)*Cos(face+355/113) + x  //create a fake point that for the tip of a full cone
    local real fakeY = (maxspread/(maxspread-minspread)*distance)*Sin(face+355/113) + y  //rather than the truncated one given
    
    local real finalSpread = (maxspread-minspread)/(2*distance)  //calculates the spread angle
    
    call GroupEnumUnitsInRange(g,x,y,distance,Filter(null))  
    
    loop  
        set u = FirstOfGroup(g)  
        exitwhen u == null  
        call GroupRemoveUnit(g,u)  

        if RAbsBJ((GetUnitY(u) - fakeY)/(GetUnitX(u) - fakeX)) < RAbsBJ(finalSpread) then
            call GroupAddUnit(units,u)
        endif
    endloop  
    
    set g = null
    set u = null
endfunction

EDIT: updated with a couple changes/fixes
 
Last edited:
Level 11
Joined
Feb 18, 2004
Messages
394
#1: You can do:
JASS:
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g,u)

        // Loop code here
    endloop
which is a bit cleaner.


#2: You don't null the "units" variable. A way around this is to design your function like a GroupEnum function, where it adds units to a group passed to it as a parameter.

As for the math, I'm too lazy to check it.
 
Level 3
Joined
Dec 20, 2007
Messages
44
I also have another question, when I add units to a group is the group then sorted so the first unit in the group is the first unit added? i.e. if I add the units in a specific order, then parse through them using the FirstOfGroup, GroupRemoveUnit commands like I did above, will they be pulled out in the same order?
 
Status
Not open for further replies.
Top