HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

jass group problem

02-26-2006, 05:52 PM#1
Jiminiyainoda
Ok, im making some custom functions.
The first 1 damages a unit.
Code:
function DamageUnit takes unit who, real amount returns nothing
call SetUnitLifeBJ( who, ( GetUnitStateSwap(UNIT_STATE_LIFE, who) - amount ) )
endfunction
Yes, this function seems pointless. But i rather have my own functions than use others.

Next function:
Code:
function DamageGroup takes group who, real amount returns nothing
call ForGroupBJ(who, function DamageUnit(GetEnumUnit(), amount))
endfunction

When i test it, i get the error "Expected ' "
Which i have no idea what it means.
Could anybody help please?
02-26-2006, 06:14 PM#2
shadow1500
for this:
Collapse JASS:
function DamageGroup takes group who, real amount returns nothing
call ForGroupBJ(who, function DamageUnit(GetEnumUnit(), amount))
endfunction
you cant use parmeters for code variables, just function <func name>

for this
Collapse JASS:
function DamageUnit takes unit who, real amount returns nothing
call SetUnitLifeBJ( who, ( GetUnitStateSwap(UNIT_STATE_LIFE, who) - amount ) )
endfunction
u dont get bounty/exp/credit for units killed by this damage function, just use the native UnitDamageTarget.

o and use the jass tag!
02-26-2006, 06:15 PM#3
TaintedReality
I thought when using ForGroupBJ you need to have a function that takes nothing returns nothing, then inside that function use GetEnumUnit(). Could be wrong as I just started JASS and that's not how I loop anyways, but..pretty sure you can't have a function with arguements as one of the arguements in ForGroupBJ.

Edit: Aw, you beat me =P. Guess I was right though.
02-26-2006, 07:07 PM#4
Jiminiyainoda
When i mean, DamageUnit. I mean a unit may now always damage a other unit. I mean it as in a trigger action. Not a spell or attack action.
I dont completyl understand what you mean by "you cant use parmeters for code variables, just function <func name>"
All i understand their is "you can use paramenters for 'what?', just 'what?' "

Any other suggestions or other understanding?
02-26-2006, 07:31 PM#5
Anitarf
You can only use functions that take nothing and return nothing for a code parameter.
02-26-2006, 07:39 PM#6
Jiminiyainoda
So is their no way around this problem?
All i want to do is damage a group, but not use 3 function, just 2.
Nevermind il remake my code.

Thanks for help people, i will give rep
02-26-2006, 09:06 PM#7
TaintedReality
Collapse JASS:
function DamageGroup takes group who real amount returns nothing
    local group g = CreateGroup()
    local unit u
    call GroupAddGroup(who, g)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        DamageUnit(u, amount)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
endfunction

That's how I learned to loop through groups, I think it's easier anyways.
02-26-2006, 09:48 PM#8
Anitarf
Just don't forget to set g to null at end of function (no need to set u to null, as it already has to be null for the function to exit the loop) to avoid the local variable leak.
02-27-2006, 04:37 PM#9
Jiminiyainoda
Wow, great thx :D
Works just how i wanted it too. Rep given

Going 1 step futher, is their a way to do what TaintedReality did, but using a condition.
Like, damage group, but only enemies of a unit or player?

Such as
Collapse JASS:
IsPlayerEnemy(GetOwningPlayer(GetAttackedUnitBJ()), GetOwningPlayer(GetAttacker())) == true

But so it only damages units that match that condition?
02-27-2006, 05:36 PM#10
Vexorian
BTW shouldn't you be using the damage natives instead? At least they give experience/bounty correctly...
02-27-2006, 06:07 PM#11
Jiminiyainoda
Yea i should really, but by damage unit, i mean a other unit may not always damage the unit using this function.
So really its damaging a unit, with no unit damaging, so no credit, just lose HP, no damage types.
I should just use the natives to set unit HP less.
But i just want to learn how to do it for future refrence
Although it is a pain, and confusing
02-27-2006, 10:45 PM#12
TaintedReality
Well when you call the function the 'who' parameter (the group to be damaged) should already take care of that condition. But yes you could do that if you wanted to do it inside the function rather than before. I think you need to assign the handles to variables first though so that doesn't leak, rather than just putting them into the functions like that (I just learned to JASS so not completely sure what leaks and what doesn't).
02-28-2006, 05:33 AM#13
Anitarf
A leak is when you fail to destroy a temporary object, so it stays in memory even after you no longer need it. Temporary objects are most often locations and unit groups.

Then there's the local leak, you need to set all handle object local variables to null before the end of function. This does not apply to function parameters, only locals.
02-28-2006, 03:12 PM#14
Jiminiyainoda
Ah yes, how stupid of me.
Iv tried what you said, it looks like it will work, but dosent.
Il be making another stupid mistake

Collapse JASS:
function AreUnitsEnemy takes nothing returns boolean
    return ( IsPlayerEnemy(GetOwningPlayer(udg_unitVar), GetOwningPlayer(GetFilterUnit())) == true )
endfunction

Collapse JASS:
function DamageGroup takes group who, real amount returns nothing
    local group g = CreateGroup()
    local unit u
    call GroupAddGroup(who, g)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call DamageUnit(u, amount)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g = null
endfunction

Collapse JASS:
function PredictCounter takes unit u, integer chance returns nothing
if ( chance >= GetRandomInt(1, 100) ) then
        call SetUnitAnimation(u, "spin")
        set udg_unitVar = u
        call DamageGroup(GetUnitsInRangeOfLocMatching(150.00, GetUnitLoc(u), Condition(AreUnitsEnemy() == true)))
        call TriggerSleepAction( 1.00 )
        call ResetUnitAnimation( u )
        call SetUnitLifeBJ( u, ( GetUnitStateSwap(UNIT_STATE_LIFE, u) + GetEventDamage() ) )
    else
    endif
endfunction

Ok, my error is when i call the function DamageGroup(), it says it has invalid argument type [boolean], but....

the function AreUnitsEnemy() , returns a boolean, and the DamageGroup() checks if it is a boolean (==true) , which to me, cause im only just starting JASS, says eek.

Any suggestions?
02-28-2006, 04:44 PM#15
HatewarE
You can't retun a comparsion. You need to make an if statement like:
Collapse JASS:
function AreUnitsEnemy takes nothing returns boolean
    if ( IsPlayerEnemy(GetOwningPlayer(udg_unitVar), GetOwningPlayer(GetFilterUnit())) == true ) then
        return true
    endif
    return false
endfunction

you can only return variables or other things of the type the function returns. In this function e.g. you return a boolean. And the only values of type bolean are true or false. You can ofc also return a varible of type boolean filled with the value true or false but that would be stupid in this case.

Another thing that causes the error is that DamageGroup() doesn't take any booleans as parameter but you said that AreUnitsEnemy() returns a boolean for it. So you have to either let DamageGroup() take a boolean and make it like
Collapse JASS:
function DamageGroup takes group who, real amount, takes boolean b returns nothing
    local group g = CreateGroup()
    local unit u
    if (b == true) then
        call GroupAddGroup(who, g)
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            call DamageUnit(u, amount)
            call GroupRemoveUnit(g,u)
        endloop
        call DestroyGroup(g)
        set g = null
    endif
endfunction

or you can include the boolean into a trigger that checks with an if statement wheter AreUnitsEnemy() is true and calls Destroy Group or whatever you want if it is.

And btw, I don't think you need to set g in DamageGroup to null because if you destroy the group g it's already null. Would make more sense if you set u to null.