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

Need to use a function in any other trigger

Status
Not open for further replies.
Level 2
Joined
Jan 2, 2013
Messages
24
Hello everyone!
As the title states, I'd like to be able to create functions that can be used by other functions without being forced to put them in the same trigger everytime. How is it possible? I've tried to find some info about libraries and scopes, thinking that would help, but I wasn't really successful. I would appreaciate even more if someone could post like a guide or something about libraries and scopes as I wasn't really able to find anything!
Also, I have a little question: Why is jass compiler telling me there is a syntax error here?

JASS:
function GUICFilter takes location tloc, real rrmin returns boolean
        if (IsUnitInRangeLoc(GetFilterUnit(), tloc, rrmin)) then
            return false
        endif
        return true
endfunction
    
function GetUnitsInCrown takes location loc, real rmin, real rmax returns group
        local group UinCr
        set UinCr = GetUnitsInRangeOfLocMatching(rmax, loc, Condition(function GUICFilter(loc,rmin)))
        return UinCr
        
endfunction

I was just trying to create a function that would pick units in a "circular crown" (I see in English you say annulus, lol :D). but it says that there is a syntax error in the line "set UinCR = GetUnits ...", one of the last lines. I really don't understand! Probably I'm missing some knowledge about when and where you can give arguments to a condition function..
Thank you in advance for your help!
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Condition() is the wrong function, you want Filter()

you can combine those two lines into

local group UinCr = GetUnitsInRangeOfLoc....

you also shouldnt EVER use locations in JASS. change the "loc" argument to two, one for X and one for Y and do GroupEnumUnitsInRange
 
Level 2
Joined
Jan 2, 2013
Messages
24
Condition() is the wrong function, you want Filter()

you can combine those two lines into

local group UinCr = GetUnitsInRangeOfLoc....

Wow thanks for the quick reply!
It still says Syntax Error with Filter. Btw, I have another trigger with a similar filtering function and it works even though I used Condition instead of FIlter! Weird! What's the difference between them anyway??
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Wow thanks for the quick reply!
It still says Syntax Error with Filter. Btw, I have another trigger with a similar filtering function and it works even though I used Condition instead of FIlter! Weird! What's the difference between them anyway??

idk i always use filter. post the line that gives u the syntax error
 
Level 2
Joined
Jan 2, 2013
Messages
24
JASS:
set UinCr = GetUnitsInRangeOfLocMatching(rmax, loc, Condition(function GUICFilter(loc,rmin)))
This is the line!
I know I shouldn't use locations but apparently I'm just too stubborn and keep on using locations and then remove them :D I'll start following your advice anyway from now on!
Thus, I wrote this using the function you told me:

JASS:
function GUICFilter takes real x, real y, real rmin returns boolean
        local location loc = Location(x,y)
        if (IsUnitInRangeLoc(GetFilterUnit(), loc, rmin)) then
            return false
        endif
        call RemoveLocation(loc)
        set loc = null
        return true
endfunction
    
function GetUnitsInCrown takes real x, real y, real rmin, real rmax returns group
        local group g
        call GroupEnumUnitsInRange(g, x, y, rmax, Filter(function GUICFilter(x,y,rmin)))
        return g
endfunction

I created a local loc because I feared that just putting Location(x,y) inside IsUnitInRange might leak (I have no idea if it would), just tell me if it was stupid. Anyway, apparently there is a syntax error in this one as well, still in the "call GroupEnumUnitsInRange(g, x, y, rmax, Filter(function GUICFilter(x,y,rmin)))" line. What am I doing wrong? :/

:( is there no way other then using globals??
 
Last edited by a moderator:
Level 19
Joined
Aug 8, 2007
Messages
2,765
:( is there no way other then using globals??

Use the edit button next time.

but yes, Maker is correct, but it only enums units that pass the condition in the first place?

(you can just throw it into a library if ud want to use vJass)
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
Something like this:
JASS:
function GetUnitsInCrown takes location loc, real rmin, real rmax returns group
    local unit u
    local group UinCr = GetUnitsInRangeOfLocMatching(rmax, loc, null)
    local group g = CreateGroup()
    loop
        set u = FirstOfGroup(UinCr)
        exitwhen u == null
        if IsUnitInRangeLoc(GetFilterUnit(), tloc, rrmin) then
            call GroupAddUnit(g, u)
        endif
        call GroupRemoveUnit(UinCr, u)
    endloop
    call DestroyGroup(UinCr)
    return g
endfunction

But don't use a loc, use coordinates. You can filter out dead units, structures etc.
 
Level 2
Joined
Jan 2, 2013
Messages
24
Use the edit button next time.
Oh sorry, I should have guessed it!
So ok I'll use globals, but what about libraries? That was the main issue of this thread! I have no idea how to use them and what to use them for! Also, is it libraries the feature that will allow me to create functions usable by whatever other trigger?? How??
Another little thing: to state globals, instead of using the user interface ( i hate udg_....) can I write into a blank trigger "globals.........endglobals" and put inside the globals I want to decleare? Do I have to put them into a library??
As you can see, I really don't know how to use stuff different from functions (scope, method, private..) so even a link would be more then ok! :D
 
Level 2
Joined
Jan 2, 2013
Messages
24
WOW it looks like that link contains everything I need! Ehm.. Just one last question :D
So if I have undestrood correctly, your function adds to a group all units in max range, then, using FirstofGroup() (i didn't know this command, ty :D), you filter the units again into a group that will be returned. The only thing that bothers me is that the condition in the loop is "units are in range of minr". Wouldn't this select units that are inside di minimum range, insted of the opposite? because I needed the function to get units that are closer then max range and farther then minrange!
 
Level 2
Joined
Jan 2, 2013
Messages
24
In that case you can use if not IsUnitInRangeLoc(GetFilterUnit(), tloc, rrmin) then
Yes of course :) I was just making sure I had understood correctly! Well, thank you very much! You've been TOTALLY satisfactory! The best I can do is give you rep I guess so I'll do it! Thank you again! :)
 
Status
Not open for further replies.
Top