• 🏆 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!

[General] Unit group variable does not auto generate?

Level 4
Joined
Feb 1, 2024
Messages
37
When I declare a variable as an array, most of which will auto generate new variable if it crosses the maximum slot.

Such as when I declared a Point array with 2 point variables but I use four, it will generate the new points for me. But this doesn't seem to be the case for Unit Group, and new Unit Group slot undeclared will not be used.
 
Level 25
Joined
Sep 26, 2009
Messages
2,381
I think you are mixing different things together. The only auto-generation happens when initializing arrays on map start. You initialize only the first N positions in the array, the number N is given by what you set in 'Size' field.
However the vast majority of data types do not have a default value, so they will never initialize in array. Only the following types have default values:
  • boolean
  • countdown timer
  • dialog
  • integer
  • player group
  • real
  • unit group

Point array was never initialized with values: Points do not have default value, and most likely they never had since they are immutable, i.e. you cannot change coordinates of existing point, you can only create new point, so it makes no sense to initialize an array with points that have no value whatsoever.
Unit group arrays are initialized up to what you set in Size - this was always the case and can be confirmed by a trigger like this:
  • Test
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • For each (Integer idx) from 1 to 5, do (Actions)
        • Loop - Actions
          • Custom script: if udg_groups[udg_idx] == null then
          • Game - Display to (All players) the text: (Index + ((String(idx)) + is null))
          • Custom script: else
          • Game - Display to (All players) the text: (Index + ((String(idx)) + is not null))
          • Custom script: endif
  • groups = unit group variables set to size 2
  • idx = integer variable
As expected, the script will print "Index _ is not null" for indexes 1 and 2 and print "Index _ is null" for indexes 3-5.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,565
There are certain variable types that require you to manually set their Array Size if you wish for the "auto-generation". I believe these are:

Unit Groups, Player Groups, and Timers.

I could be forgetting one or two.

If you wish to do things efficiently then you can keep the Size at 1 and instead design your triggers to manually create these objects as needed:
  • Custom script: set udg_MyUnitGroup[5] = CreateGroup()
^ That will create a Unit Group at index [5] which you can use without issues. This is useful when combined with Unit Indexing and other methods since you don't have to worry about having enough initialized. It also saves you from creating a ton of these objects that will go unused.

The other types:
  • Custom script: set udg_MyPlayerGroup[5] = CreateForce()
  • Custom script: set udg_MyTimer[5] = CreateTimer()
Points are setup to automatically resize the Array and create a new Location() for you as needed.
 
Top