• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to get the group in a for group function?

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,337
Hi,

Suppose I've got the following code.

JASS:
local integer pid

static method create takes integer pid returns nothing
  ...
  set this.pid = pid
  return this
endmethod

method foo takes unit u returns nothing
  if GetLocalPlayer() == Player(pid) then
    call SetUnitVertexColor(u, GetRandomInt(0, 255), GetRandomInt(0, 255), GetRandomInt(0, 255))
  endif
endmethod
  
static method main takes nothing returns nothing
  local unit u = GetEnumUnit()
  local myStruct my = myTable[GetUnitTypeId(u)]
  call my.foo(u)
endmethod

method do takes integer unitID returns nothing
  local group g = CreateGroup()
  set g = GetUnitsOfTypeIdAll(unitID)
  set myTable[unitID] = this
  call ForGroup(g, function main)
  call DestroyGroup(g)
  set g = null
endmethod

So I need to iterate over all units of a certain type, and then do something to each one from a struct instance.

I wanted to do GetHandleId(g) as the index for the struct, but apparently there is no native like GetEnumGroup() that can be called inside a for group function. I also thought about GetLastCreatedGroup() , but I have no idea how that would behave.

While I am certain my functions will work, I want to know if it is possible for a handle id to have the same value as a unitID--if so, then my method of using Table is not safe.

In addition, suppose the following happens (reference the above code for what the functions do).

JASS:
local integer unitID = 'hfoo'
local integer i = 0
local myStruct my
loop
  exitwhen i == TOTAL_PLAYERS
  set my = myStruct.create(i)
  call my.do(unitID)
  set i = i  + 1
endloop

Will my code be ok? That is, each player will see every footman tinted differently.
 
Handle ID's and raw codes should not collide unless you have millions of objects. No, they won't collide.

As for GetEnumGroup(), you should store the group in a variable and just retrieve it. Since it is instantaneous, you don't have to worry about MUI. It is a different story than with timers, so you can use globals as much as you'd like to pass data between functions:
JASS:
function test takes nothing returns nothing
    // do stuff with "enumGroup"
endfunction

// ... code, enumGroup = group global variable
set enumGroup = g
call ForGroup(g, function test)

As for the last part, it really depends on what the code does (particularly, "create" and "do"). If you're just tinting a footman differently, it'll be fine. Just make sure you aren't creating handles locally or anything.
 
Status
Not open for further replies.
Top