Callbacks in Loops

Fearoth

New Member
Reaction score
1
Is it possible, if so is it safe to use callbacks inside loops where the loop keeps owerwriting the group which is used by the callback? Like;

JASS:
function A2 takes nothing returns nothing
    local unit a = GetEnumUnit()
    local player b = GetOwningPlayer(a)
    if GetUnitTypeId(a)=='h005' then
        call SetCameraTargetControllerNoZForPlayer(b,a,0,0,false)
    endif
endfunction

function A1 takes nothing returns nothing
    local integer a = 0
    local group b = null
    loop
        exitwhen a == 7
        set b = GetUnitsInRectOfPlayer(GetPlayableMapRect(),Player(a))
        call ForGroup(b,function A2)
        set b = null
        set a = a + 1
    endloop
    call DestroyGroup(b)
endfunction


'h005' being my helper for locking player cameras - Each player gets one in the beginning.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
if i am correct thats just a loop inside a loop, i would do it like this:

JASS:
function A1 takes nothing returns nothing
    local integer a = 0
    local group b
    local player c
    loop
        exitwhen a == 7
        set b = GetUnitsInRectOfPlayer(GetPlayableMapRect(),Player(a))    local unit a = GetEnumUnit()
        loop
            exitwhen FirstOfGroup(b) == null
            set c = GetOwningPlayer(FirstOfGroup(b))
            if GetUnitTypeId(FirstOfGroup(b))=='h005' then
                call SetCameraTargetControllerNoZForPlayer(c,a,0,0,false)
            endif
            call GroupRemoveUnit(FirstOfGroup(b))
        endloop
        set b = null
        set a = a + 1
    endloop
    call DestroyGroup(b)
endfunction


But i would remove those BJs, things in red can be replaced with whatever they do, they are simply a function in another function
 

Dirac

22710180
Reaction score
147
Yes, and what GFreak suggested is the best option, if you take aside the syntax errors, the fact that he leaks 7 groups everytime the code is performed, he doesn't use a pointer for FirstOfGroup as it should be stored in a var, he doesn't null the handles at the end of the code and the use of useless BJs
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
Back!!! I scream swinging a tree branch trying to beat the troll back under his bridge...

Just so u know I just copied and pasted and replaced the unit he used with first of group just to get the point across, I considered writing it like I was spoonfeeding him and he could just c/p and have it done, instead I decided to get the first of group point across... he is still using bjs so I figured id let him finish what he was doing and just provide the basic idea... u really need to get out more dirac, your trollskin is pale white

PS. Fully developed trolls usually read the whole post before trolling it... papa troll would be so dissapointed
 

Dirac

22710180
Reaction score
147
I don't know what does that post means, a bunch of troll role-play, i'm unfamiliar with the dramatic gender.
I wasn't being sarcastic, and what i said in my post is all true, it is the best option, you have to fix those issues.
This forum isn't WEHZ you have to completely help the user (not just partially help it) because JASS is rather harder.

If you want i can enumerate all the issues i named above inside your code

JASS:
function A1 takes nothing returns nothing
    local integer a = 0
    local group b
    local player c
    loop
        exitwhen a == 7
        set b = GetUnitsInRectOfPlayer(GetPlayableMapRect(),Player(a))    local unit a = GetEnumUnit() //<- syntax error, also BJ use
        loop
            exitwhen FirstOfGroup(b) == null
            set c = GetOwningPlayer(FirstOfGroup(b))
            if GetUnitTypeId(FirstOfGroup(b))=='h005' then
                call SetCameraTargetControllerNoZForPlayer(c,a,0,0,false) //<- use of BJs should be avoided
            endif
            call GroupRemoveUnit(FirstOfGroup(b)) //<- extensive use of FirstOfGroup native instead of local variable
        endloop
        set b = null
        set a = a + 1
    endloop
    call DestroyGroup(b) //<- b is equal to null, you're only destroying 1 group anyways when you created 7
    //<- you have to null "b" "c" and the unexistant unit variable for FirstOfGroup
endfunction
 

Fearoth

New Member
Reaction score
1
i think i got what you mean. ive changed the code to this now :

JASS:
function Starting_Camera1 takes nothing returns nothing
    local player a = GetEnumPlayer()
    local group b = CreateGroup()
    call GroupEnumUnitsInRect(b,bj_mapInitialPlayableArea,null)
    loop
        exitwhen FirstOfGroup(b)==null
        if GetOwningPlayer(FirstOfGroup(b))==a and GetUnitTypeId(FirstOfGroup(b))=='h005' and GetLocalPlayer()==GetEnumPlayer() then
            call SetCameraTargetController(FirstOfGroup(b),0,0,false)
        endif
        call GroupRemoveUnit(b,FirstOfGroup(b))
    endloop
    call DestroyGroup(b)
endfunction

function Visibility1 takes nothing returns nothing
    local force a = CreateForce()
    local integer b = 0
    loop
        exitwhen b == 7
        if GetPlayerSlotState(Player(b))==PLAYER_SLOT_STATE_PLAYING then
            call ForceAddPlayer(a,Player(b))
        endif
        set b = b + 1
    endloop
    call ForForce(a,function Visibility2)
    set b = 0
    loop
        exitwhen b == 7
        if GetPlayerController(Player(b))==MAP_CONTROL_COMPUTER then
            call ForceRemovePlayer(a,Player(b))
        endif
        set b = b+1
    endloop    
    call ForForce(a,function Starting_Camera1)
    call DestroyForce(a)
endfunction


Tried to get rid of BJs but i really have to use bj_mapInitialPlayableArea. Couldn't find a better way to refer to it.

And a question : Does the usage of GetLocalPlayer() leak?
 

luorax

Invasion in Duskwood
Reaction score
67
JASS:
function A1_Filter takes nothing returns boolean
    local player p
    if GetUnitTypeId(GetFilterUnit())=='h005' then
        set p=GetOwningPlayer(GetFilterUnit())
        if (GetLocalPlayer()==p then
            call SetCameraTargetController(GetFilterUnit(),0,0,false)
        endif
        set p=null //not necessary, but it's a good practice
    endif
    return false
endfunction

function A1 takes nothing returns nothing
    call GroupEnumUnitsInRect(udg_EnumGroup,bj_mapInitialPlayableArea,function A1_Filter)
endfunction


"udg_EnumGroup" is a global group created in the variable editor. Or if you have JASSHelper:

JASS:
library foo

    globals
        private constant group ENUM_GROUP=CreateGroup()
        private player TempPlayer
        private unit TempUnit
    endglobals

    private function filter takes nothing returns boolean
        if GetUnitTypeId(GetFilterUnit())=='h005' then
            set TempUnit=GetFilterUnit()
            set TempPlayer=GetOwningPlayer(TempUnit)
            if GetLocalPlayer()==TempPlayer then
                call SetCameraTargetController(TempUnit,0,0,false)
            endif
        endif
        return false
    endfunction

    private function periodic takes nothing returns nothing
        call GroupEnumUnitsInRect(ENUM_GROUP,bj_mapInitialPlayableArea,function filter)
    endfunction

endlibrary
 

Dirac

22710180
Reaction score
147
@luorax
Probably you didn't know this but [ljass]bj_lastCreatedGroup[/ljass] is an already initialized group you can use for quick group enumeration, no need to initialize a new one for every code :) (make sure you don't destroy it and that it's empty at all times)
 

luorax

Invasion in Duskwood
Reaction score
67
I know it, don't worry :) But I'm not sure if he not uses GUI on his map, that's why I'm not suggesting it.

EDIT:

Yes, it can be done this way:

JASS:
library foo

    private function filter takes nothing returns boolean
        if GetUnitTypeId(GetFilterUnit())=='h005' then
            set bj_lastCreatedUnit=GetFilterUnit()
            set bj_forceRandomCurrentPick=GetOwningPlayer(bj_lastCreatedUnit)
            if GetLocalPlayer()==bj_forceRandomCurrentPick then
                call SetCameraTargetController(bj_lastCreatedUnit,0,0,false)
            endif
        endif
        return false
    endfunction

    private function periodic takes nothing returns nothing
        call GroupEnumUnitsInRect(bj_lastCreatedGroup,bj_mapInitialPlayableArea,function filter)
    endfunction

endlibrary


But this might break your GUI triggers (or even your JASS/vJASS ones if they happen to use BJ's)
 

Fearoth

New Member
Reaction score
1
I used GUI but im trying to do it from scratch and in JASS. And it appears i'll have quite a hard time doing it :)

Did a few more modifications. Here it is now :

JASS:
function A1 takes nothing returns nothing
    local force a = CreateForce()
    local integer b = 0
    local group c = CreateGroup()
    loop
        exitwhen b == 7
        if GetPlayerSlotState(Player(b))==PLAYER_SLOT_STATE_PLAYING then
            call ForceAddPlayer(a,Player(b))
        endif
        set b = b + 1
    endloop
    call ForForce(a,function Visibility2)
    set b = 0
    loop
        exitwhen b == 7
        if GetPlayerController(Player(b))==MAP_CONTROL_USER then
            call GroupEnumUnitsInRect(c,bj_mapInitialPlayableArea,null)
            loop
                exitwhen FirstOfGroup(c)==null
                if GetOwningPlayer(FirstOfGroup(c))==Player(b) and GetUnitTypeId(FirstOfGroup(c))=='h005' and GetLocalPlayer()==Player(b) then
                    call SetCameraTargetController(FirstOfGroup(c),0,0,false)
                endif
                call GroupRemoveUnit(c,FirstOfGroup(c))
            endloop
        endif
        set b = b+1
    endloop    
    call DestroyForce(a)
    call DestroyGroup(c)
endfunction
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
Fearoth as dirac raged about, in the loop:

JASS:
loop
    exitwhen FirstOfGroup(b)==null
    if GetOwningPlayer(FirstOfGroup(b))==a and GetUnitTypeId(FirstOfGroup(b))=='h005' and GetLocalPlayer()==GetEnumPlayer() then
        call SetCameraTargetController(FirstOfGroup(b),0,0,false)
    endif
    call GroupRemoveUnit(b,FirstOfGroup(b))
endloop

//====================================================================================

local unit u
loop
    set u = FirstOfGroup(b)
    exitwhen u == null
    if GetOwningPlayer(u)==a and GetUnitTypeId(u)=='h005' and GetLocalPlayer()==GetEnumPlayer() then
        call SetCameraTargetController(u,0,0,false)
    endif
    call GroupRemoveUnit(b,u)
endloop


U do want to set those to a local as a local variable is faster than calling a function, which is what [ljass]FirstOfGroup[/ljass] is
 

UndeadDragon

Super Moderator
Reaction score
447
Please get back on topic. This is a help forum, not a rage-at-each-other forum, you are taking over Fearoth's thread when he was just looking for help.

@Fearoth I have cleaned up this thread to just leave posts that are useful to you.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top