Adding onto a local integer?

Beetlebomb

New Member
Reaction score
43
I'd normally try and teach myself this but being that i can't convert a gui version of this into jass I have to get help on this one, guys-- haha.

This is what i got so far:

JASS:
function Cond takes nothing returns boolean
    if(not(GetSpellAbilityId() == 'AHtc' ) ) then
    return false
    endif
    return true
endfunction

function kill2 takes nothing returns nothing
    call KillUnit( GetEnumUnit())
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    local integer i = 0
    call ForGroupBJ( GetUnitsOfTypeIdAll('earc'), function kill2, function Add )
    call DestroyGroup(GetLastCreatedGroup())
    call DisplayTextToForce(GetPlayersAll(), I2S(i)+" Archers have been killed." )
endfunction

function Add takes nothing returns nothing
    set integer i = (i+1)
endfunction

//===========================================================================
function InitTrig_Remove_Archers takes nothing returns nothing
    set gg_trg_Remove_Archers = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Remove_Archers, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( gg_trg_Remove_Archers, Condition(function Cond) )
    call TriggerAddAction( gg_trg_Remove_Archers, function Trig_Remove_Archers_Actions )
endfunction


So you can kinda see what I'm trying to do. If a player uses Thunder clap it kills all archers and then displays how many were killed. I remember reading in a tutorial that the local has to be declared before the function so I put the "Add" trigger below the Actions.

Mind pointing out to me how to add onto a local integer like that? ^^ thanks!
 

Flare

Stops copies me!
Reaction score
662
You can't refer to a local from a function other than the one that it was declared in. Make a global like so

JASS:
globals
integer globalinteger
endglobals


Then, in your Add function
JASS:
set globalinteger = globalinteger + 1


Then, after your ForGroup
JASS:
set localinteger = globalinteger //localinteger just refers to the local integer (obviously)
set globalinteger = 0


As long as there are no waits in the callback function for the ForGroup, there won't be a problem with that
 

darkbeer

Beer is Good!
Reaction score
84
well, it is possible to store a local using gamecache but thats unneeded in most cases, like here ^^

using a global is as trollvottel already said by far the best you can do.
 

Flare

Stops copies me!
Reaction score
662
Check the Function List. It'll tell you exactly what arguments the function takes (and I doubt that ForGroup takes 2 callback functions)

You should really use the native rather than the BJ
JASS:
//Native function
native ForGroup takes group whichGroup, code callback returns nothing

//BJ function. This is just calling the native ForGroup, using the exact same arguments (group whichGroup, code callback)
function ForGroupBJ takes group whichGroup, code callback returns nothing
    // If the user wants the group destroyed, remember that fact and clear
    // the flag, in case it is used again in the callback.
    local boolean wantDestroy = bj_wantDestroyGroup
    set bj_wantDestroyGroup = false

    call ForGroup(whichGroup, callback)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(whichGroup)
    endif
endfunction
 

WarLuvr3393

Hmmm...too many things to play (WoW, COD4, WC3)
Reaction score
54
If you want to transfer locals from 1 function to another, I believe you can use Structs or KaTTana's HandleVars system.
 

Romek

Super Moderator
Reaction score
963
A function which another function calls has to be above the calling function.
Locals can only be used in the function they were delcared in. What would be the point in them otherwise? :p

Also, you could do this:

JASS:
function Cond takes nothing returns boolean
    return GetSpellAbilityId() == 'AHtc' //Shortened Condition
endfunction


And you could use
JASS:
function add takes integer i, returns integer
return i + 1
endfunction

to add 1 to a value and return the value :D
 

Beetlebomb

New Member
Reaction score
43
you've lost me ><.


All I'm trying to do is make it so that it counts however many archers are on the map and displays "(number of archers) have been killed" after i've used the Thunderclap ability. It then kills the archers as well.

Could you help me make a basic script for that or is that asking to much? :)

Also I know when making an integer into a string i use I2S, but what do I use for Globals? I just tried G2S and...well.. you know what happened.

This is all that I have right now:
JASS:
function Cond takes nothing returns boolean
    return GetSpellAbilityId() == &#039;AHtc&#039; 
endfunction

function kill2 takes nothing returns nothing
    call KillUnit( GetEnumUnit())
endfunction

function Add takes nothing returns nothing
    set g = g + 1
endfunction

globals
integer g
endglobals

function Trig_Remove_Archers_Actions takes nothing returns nothing
    local integer i = 0
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function kill2 )
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function Add )
    set i = g
    call DestroyGroup(GetLastCreatedGroup())
    call DisplayTextToForce(GetPlayersAll(), G2S(g)+&quot; Archers have been killed.&quot; )
    set g = 0
endfunction


//===========================================================================
function InitTrig_Remove_Archers takes nothing returns nothing
    set gg_trg_Remove_Archers = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Remove_Archers, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( gg_trg_Remove_Archers, Condition(function Cond) )
    call TriggerAddAction( gg_trg_Remove_Archers, function Trig_Remove_Archers_Actions )
endfunction
 

Beetlebomb

New Member
Reaction score
43
A function which another function calls has to be above the calling function.
Locals can only be used in the function they were delcared in. What would be the point in them otherwise? :p

Also, you could do this:

JASS:
function Cond takes nothing returns boolean
    return GetSpellAbilityId() == &#039;AHtc&#039; //Shortened Condition
endfunction


And you could use
JASS:
function add takes integer i, returns integer
return i + 1
endfunction

to add 1 to a value and return the value :D

I'll try that, ty lol.
 

Beetlebomb

New Member
Reaction score
43
JASS:
globals
integer i
endglobals

function Add takes nothing returns integer
    return i + 1
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function kill2 )
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function Add )
    call DisplayTextToForce(GetPlayersAll(), I2S(i)+&quot; Archers have been killed.&quot; )
    set i = 0
endfunction


The Text still doesn't display at all when I add in the I2S(i)+ Can someone explain to me why this is happening?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
JASS:
globals
integer i
endglobals

function Add takes nothing returns integer
    return i + 1
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function kill2 )
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function Add )
    call DisplayTextToForce(GetPlayersAll(), I2S(i)+&quot; Archers have been killed.&quot; )
    set i = 0
endfunction


The Text still doesn't display at all when I add in the I2S(i)+ Can someone explain to me why this is happening?

So it doesn't display text at all, even without the I2S?

Try this if the above question is false:
JASS:
globals
integer i = 0
endglobals

function Add takes nothing returns integer
    return i + 1
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function kill2 )
    call ForGroupBJ( GetUnitsOfTypeIdAll(&#039;earc&#039;), function Add )
    call DisplayTextToForce(GetPlayersAll(), I2S(i)+&quot; Archers have been killed.&quot; )
    set i = 0
endfunction
 

Beetlebomb

New Member
Reaction score
43
The above question was false, so i tried what you said and now it shows a number: 0; and that's it. lol

It's still not counting the archers.

Edit: Purge?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Show us your kill function. And maybe try this?
JASS:
globals
    integer i = 0
endglobals

function Add takes nothing returns nothing
    set i = i + 1
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    local group g = GetUnitsOfTypeIdAll(&#039;earc&#039;)
    local group g2 = GetUnitsOfTypeIdAll(&#039;earc&#039;)
    call ForGroup(g, function kill2 )
    call ForGroup(g2, function Add )
    call DestroyGroup(g)
    call DestroyGroup(g2)
    set g = null
    set g2 = null
    call DisplayTextToForce(GetPlayersAll(),I2S(i)+&quot; Archers have been killed.&quot; )
    set i = 0
endfunction
 

Romek

Super Moderator
Reaction score
963
JASS:
globals
    integer i = 0
endglobals

function Add takes nothing returns nothing
    set i = i + 1
endfunction

function Trig_Remove_Archers_Actions takes nothing returns nothing
    local group g = GetUnitsOfTypeIdAll(&#039;earc&#039;) //only need 1 group
    call ForGroup(g, function Add )
    call ForGroup(g, function kill2 ) // Kill the archers AFTER you&#039;ve counted them...
    call DestroyGroup(g)
    set g = null
    call DisplayTextToForce(GetPlayersAll(),I2S(i)+&quot; Archers have been killed.&quot; )
    set i = 0
endfunction


That should work now. Assuming kill2 is just a simple "Kill GetEnumUnit()"
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Whoops, my mistake. :p At first, i thought the rawcodes were different so I accidentally made it two times each :p
 

Beetlebomb

New Member
Reaction score
43
Thank you all so much, it works great! There is 1 problem though. It's counting dead bodies too so the numbers are always wrong.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top