Kill units in a group effeciently

Matrixboy

New Member
Reaction score
1
Hello there everyone. I used to be quite adept at JASS, and left a while ago. I am taking up a new map project now, however. Trying to get back in the groove, I started with something really simple. A "if the player slot wasn't filled, remove their hero." I did a long line of if statements to solve this, with it acting on map initialization. It went from that, to finally these two functions that I have it at now.

Code:
function remove_player_unit takes integer x returns nothing
    local unit character = udg_character[x] *
    call TriggerSleepAction( 120.00 )
    call PingMinimap (GetUnitX(character), GetUnitY(character), 2.00)
    call KillUnit( character )
    set character = null 
endfunction

and

Code:
function check_empty takes nothing returns nothing
local integer x = 0
    loop
    exitwhen x==12
      if ( GetPlayerSlotState(Player(x)) != PLAYER_SLOT_STATE_PLAYING ) then
                call remove_player_unit(x)
        endif
        set x = x+1
    endloop 
endfunction

* udg_character is a global array that has been preset with each corresponding player's hero. (e.g. udg_character[0] is red's hero, udg_character[11] is brown's). A variable is used because this unit _will_ change over time. Transformations and etc. are going to be applied, leaving it as a separate unit.

Two functions are used in this case, because I want to be able to call remove_player_unit in other situations (i.e. if someone is kicked, not just doing one check at the initialization).


I have one question, however. Some of the players have multiple heroes. Mainly purple and yellow. Purple's will go back and forth between one and two, and yellow will go from three to one, and remain that way. Is there any way I can efficiently write "kill all units owned by player(x)" or "kill all units in group X?" I am trying to keep the code as efficient as possible, as it is going to be quite a big map.

thanks in advance!

Travis
 
Well, you should look into vJass. Those godforsaken udg_ globals are unneeded.

The most efficient way to kill all units of a player?

This comes close.

JASS:
globals
    group KillGroup = CreateGroup()
endglobals

function KillAllUnits takes nothing returns boolean
    call KillUnit(GetFilterUnit())
    return false
endfunction

function Whatever takes nothing returns nothing
    call GroupEnumUnitsOfPlayer(KillGroup, Player(0), Filter(function KillAllUnits))
endfunction

This requires NewGen, because normal Jass won't accept freeform globals. I've also neglected to use scopes or anything, to make it easier to understand.
 
Well, you should look into vJass. Those godforsaken udg_ globals are unneeded.

The most efficient way to kill all units of a player?

This comes close.

JASS:
globals
    group KillGroup = CreateGroup()
endglobals

function KillAllUnits takes nothing returns boolean
    call KillUnit(GetFilterUnit())
    return false
endfunction

function Whatever takes nothing returns nothing
    call GroupEnumUnitsOfPlayer(KillGroup, Player(0), Filter(function KillAllUnits))
endfunction

This requires NewGen, because normal Jass won't accept freeform globals. I've also neglected to use scopes or anything, to make it easier to understand.


I have a copy of newgen open right now, studying the differences. I also have the vjass reference manual opened up for a little reading material. However, you state that udg_variables are bad. I completely agree, however how is the newgen method of globals any better? From what I can tell, all it does is it carries it to the top of the .j file, as that's where globals are really defined. In my case, I cannot find an option other than using globals, unless I went for one of the handle methods that carry locals over, which I would need to do a considerable amount of reading on.

Thank you for recommending me vjass/newgen, it seems like it'll make a lot of writing this easier. And thank you for the code example, I'll use that as a base to impliment what I'm trying to do. Thanks :)
 
Well, think of it this way. With udg_ variables, porting code from map to map is a pain. You would have to open the GUI variable editor and create the variables with strict names and instructions. With freeform globals, you can make your variables without needing to depend on the user (and fill the WE Help forums with threads on why the code won't work...).
 
Well, think of it this way. With udg_ variables, porting code from map to map is a pain. You would have to open the GUI variable editor and create the variables with strict names and instructions. With freeform globals, you can make your variables without needing to depend on the user (and fill the WE Help forums with threads on why the code won't work...).

Sounds a good enough reason to switch :)

Thanks for all of your help azlier.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      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