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.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      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