• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[Trigger] Simple Efficiency Question About UnitGroups

Status
Not open for further replies.
Is it better to add/remove units to a unitgroup then to create and clear a unitgroup over and over?
Which is more efficient / less laggy?
  • Hunger
    • Events
      • Time - Every 2.00 seconds of game time
    • Conditions
    • Actions
      • Set HungerGroup = (Units in (Playable map area) matching ((((Matching unit) is A Hero) Equal to True) and (((Picked unit) has buff Sound asleep ) Equal to False)))
      • Unit Group - Pick every unit in HungerGroup and do (Actions)
        • Loop - Actions
          • Unit - Set mana of (Picked unit) to ((Mana of (Picked unit)) - 1.00)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Mana of (Picked unit)) Less than or equal to 0.00
            • Then - Actions
              • Unit - Set life of (Picked unit) to ((Life of (Picked unit)) - 5.00)
              • If ((Life of (Picked unit)) Less than or equal to 15.00) then do (Cinematic - Ping minimap for (All players matching ((Matching player) Equal to (Owner of (Picked unit)))) at (Position of (Picked unit)) for 1.50 seconds, using a Warning ping of color (100.00%, 30.00%, 20.00%)) else do (Do nothing)
            • Else - Actions
              • Do nothing
      • Custom script: call DestroyGroup(udg_HungerGroup)
Would it be better to just add the units to the "HungerGroup" when they're made (and remove them from the group when they die or are removed by trigger) then to keep making the group and removing it over and over? Also, when you replace a unit would it have to be removed from the unitgroup before it is replaced and re-added after being replaced?

EDIT: I have another question I thought I might as well ask... what function do you use to get the nearest unit within a certain range of a unit? If someone could make an example trigger that would help :D.
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
Just create/destroy the group. In GUI, you usually create a group anyway using "units within range".

To check the nearest unit to a point, do:

Set Range(real) = 512 <-- 512 is the distance
Set TempUnits = Units within 512 of a Point
Pick every unit in TempUnits and do actions:
--- Set TempLoc = location of picked unit
--- If Distance between TempLoc and Point < Range then
------- Set NearestUnit = Picked Unit
--- Custom script: call RemoveLocation(udg_TempLoc)
Custom script: call RemoveLocation(udg_Point)
Custom script: call DestroyGroup(udg_TempUnits)
Nearest unit is now the nearest unit.
 
Not what I asked .

Your Group variable is seted wrong, its matching unit have a buff :p
And i don't totally understand your questions, its something if unit dies within that 2 seconds? if so just add when you set the group matching unit is alive equal to false.

No, that's completely off the point. I'm wondering if it would create less lag if you used stuff like
  • Woodle
    • Events
    • Conditions
    • Actions
      • Unit Group - Add (Triggering unit) to HungerGroup
      • Unit Group - Remove (Triggering unit) from HungerGroup
instead of
  • Woodle
    • Events
    • Conditions
    • Actions
      • Set HungerGroup = (Units in (Playable map area))
      • Custom script: call DestroyGroup(udg_HungerGroup)
  • where you set and clear it over and over.
Just create/destroy the group. In GUI, you usually create a group anyway using "units within range".

To check the nearest unit to a point, do:

Set Range(real) = 512 <-- 512 is the distance
Set TempUnits = Units within 512 of a Point
Pick every unit in TempUnits and do actions:
--- Set TempLoc = location of picked unit
--- If Distance between TempLoc and Point < Range then
------- Set NearestUnit = Picked Unit
--- Custom script: call RemoveLocation(udg_TempLoc)
Custom script: call RemoveLocation(udg_Point)
Custom script: call DestroyGroup(udg_TempUnits)
Nearest unit is now the nearest unit.

Also not what I asked, I know how to do that... I was wondering is there was a Jass function that does it simpler? Like you know how when peasents are cutting down trees they always find the nearest trees, I wanted that for a different custom spell that targets the closest unit with a certain custom value and gets the nearest one (and finds another one after that unit is dead)?
 
Last edited:
Level 5
Joined
Oct 17, 2006
Messages
151
use add/remove unit.

When you clear an entire group and say this group has 10+ units in it then what it does it manually does the "remove unit action" just repeats it for every unit in the group. So hence its easier to do the add/remove because your doing the same thing xept just once.

:thumbs_up:
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Using one unit group without destroying it and just adding the units to it via another trigger(and when necessary removing them) is far more efficient than picking units in playable map area(matching condition .. blah blah).
So this trigger would be just:
Pick all units in temp_unit_group and do actions:
...

Without destroying the group.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
No, that's completely off the point. I'm wondering if it would create less lag if you used stuff like
  • Woodle
    • Events
    • Conditions
    • Actions
      • Unit Group - Add (Triggering unit) to HungerGroup
      • Unit Group - Remove (Triggering unit) from HungerGroup
instead of
  • Woodle
    • Events
    • Conditions
    • Actions
      • Set HungerGroup = (Units in (Playable map area))
      • Custom script: call DestroyGroup(udg_HungerGroup)
  • where you set and clear it over and over.


Also not what I asked, I know how to do that... I was wondering is there was a Jass function that does it simpler? Like you know how when peasents are cutting down trees they always find the nearest trees, I wanted that for a different custom spell that targets the closest unit with a certain custom value and gets the nearest one (and finds another one after that unit is dead)?

Nope. You can write it in jass so it's more efficient, but as you don't seem to know jass, you'll have to go for this trigger. By the way, you asked for a trigger, not a jass script
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
All right then... Don't know if this works, but here goes nothing:

JASS:
function GetNearestUnitToLoc takes group g, location l, real dist returns unit
    local group tmp = CreateGroup()
    local real x = GetLocationX(l)
    local real y = GetLocationY(l)
    local real dx
    local real dy
    local unit u
    local unit nearest = null
    local real u_dist
    call GroupAddGroup(g, tmp)
    set dist = dist * dist
    loop
        set u = FirstOfGroup(tmp)
        exitwhen u == null
        call GroupRemoveUnit(tmp, u)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        set u_dist = (dx * dx + dy * dy)
        if u_dist < dist then
            set nearest = u
            set dist = u_dist
        endif
    endloop
    call DestroyGroup(tmp)
    set tmp = null
    set u = null
    return nearest
endfunction
 
Found a Get Closest Unit Thing

All right then... Don't know if this works, but here goes nothing:

JASS:
function GetNearestUnitToLoc takes group g, location l, real dist returns unit
    local group tmp = CreateGroup()
    local real x = GetLocationX(l)
    local real y = GetLocationY(l)
    local real dx
    local real dy
    local unit u
    local unit nearest = null
    local real u_dist
    call GroupAddGroup(g, tmp)
    set dist = dist * dist
    loop
        set u = FirstOfGroup(tmp)
        exitwhen u == null
        call GroupRemoveUnit(tmp, u)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        set u_dist = (dx * dx + dy * dy)
        if u_dist < dist then
            set nearest = u
            set dist = u_dist
        endif
    endloop
    call DestroyGroup(tmp)
    set tmp = null
    set u = null
    return nearest
endfunction

I was searching through the forums when I found this http://www.hiveworkshop.com/forums/f413/getclosestunit-s-59254/ it seems someone else has already made a get closest unit...
Thanks for helping anyways +rep :D
 
Status
Not open for further replies.
Top