[General] how-to : assign multiple regions to variable

Level 3
Joined
Apr 12, 2025
Messages
20
I haven't been able to find an answer to this specific question so I'm hoping this is the right place.

Question: Can I assign 4 different regions into a singular variable so that I can spawn creeps in each region with a single action?

More Detail: I'm working on a circle td map and each player has a spawn in their respective corners. My creep RNG is going to be quite extensive so if I can cull the amount of actions I have to write, that would be cool.
 
Sure you can, create a region-type variable with an array, then you can do stuff like that

1754272628292.png


  • Set VariableSet MyRegion[1] = Region 001 <gen>
  • Set VariableSet MyRegion[2] = Region 002 <gen>
  • Set VariableSet MyRegion[3] = Region 003 <gen>
  • Set VariableSet MyRegion[4] = Region 004 <gen>
  • -------- ------------------------------------- --------
  • For each (Integer A) from 1 to 4, do (Actions)
    • Loop - Actions
      • Unit - Create 1 Footman for Neutral Hostile at (Center of MyRegion[(Integer A)]) facing Default building facing degrees
 
Sure you can, create a region-type variable with an array, then you can do stuff like that

View attachment 544663

  • Set VariableSet MyRegion[1] = Region 001 <gen>
  • Set VariableSet MyRegion[2] = Region 002 <gen>
  • Set VariableSet MyRegion[3] = Region 003 <gen>
  • Set VariableSet MyRegion[4] = Region 004 <gen>
  • -------- ------------------------------------- --------
  • For each (Integer A) from 1 to 4, do (Actions)
    • Loop - Actions
      • Unit - Create 1 Footman for Neutral Hostile at (Center of MyRegion[(Integer A)]) facing Default building facing degrees

I might be missing something here, but how does the For Each action know what array to reference? Can I set up the region array in an initialization, and call 1-4 like you noted somewhere else? What is the difference between integer A and B, neither of these allow you to pick what array to reference.


NEVERMIND , after configuring it I've managed to answer my own question, i got it working perfectly. Thank you thank you thank you!!!
 
Last edited:
Here's a map that includes some tips on making a TD. This is intended to show you how to create a strong foundation to build upon but is not meant to be something you copy 1:1. Obviously each map is unique and has it's own needs. The most important thing here is that it shows you how to setup your map in a structured way, step by step.

Speaking of structure, I recommend avoiding the use of the For Each action when it comes to logic related to multiple Players. Instead, use a Player Group variable as they're specifically designed for this type of behavior. I show how to do this in the map.

Here's an example:
  • Player Group - Pick every player in Player_Group_Users_Playing and do (Actions)
    • Loop - Actions
      • Unit - Create 1 Ghoul for Computer at (Center of MyRegion[(Player number of (Picked player))]) facing Default building facing degrees
^ This trigger will only create units for a User if they're actively playing the game. This would be useful if you didn't want Leavers/Unused Players from spawning units.

But maybe at some point you'll change your mind, in which case the fix is extremely simple:
  • Player Group - Pick every player in Player_Group_Users_All and do (Actions)
    • Loop - Actions
      • Unit - Create 1 Ghoul for Computer at (Center of MyRegion[(Player number of (Picked player))]) facing Default building facing degrees
^ I changed the Player Group to one that contains all of your Users regardless of whether they're playing or not.

So in this particular case you would swap the Player Group variable for a different one that contains the correct set of players. OR, let's say that you were referencing this Player Group across 20 different triggers and it'd be a hassle to have to find and change each one. In that case you could simply change which players get added to it in the first place, which would update all 20 triggers in the process. In other words, in a well designed system you can change ONE thing and have it reflect across ALL of your triggers.

TLDR: Smart design like this makes it easy to change, improve, and expand upon your systems throughout the development process. It also makes the creation of triggers a breeze and more enjoyable because you have all of these pre-defined variables that you can quickly reference with confidence -> "I know for a fact that there will only ever be active users in the Player_Group_Users_Playing group".
 

Attachments

Last edited:

Thank you for sending that. I checked it out.

Could I just remove the region assignment from the variable array, if a player leaves, to null their spawn? I've already pretty extensively implemented the for each loops and it'll be a chunk of time to change it to how you have yours. But I will if I can't find a way to null the spawns on leave.

I could also make it so if a player leaves, their spawn region changes to spawn all their creeps into an arena with an overpowered giant chicken that kills them all. That might even be more entertaining.
 
Thank you for sending that. I checked it out.

Could I just remove the region assignment from the variable array, if a player leaves, to null their spawn? I've already pretty extensively implemented the for each loops and it'll be a chunk of time to change it to how you have yours. But I will if I can't find a way to null the spawns on leave.

I could also make it so if a player leaves, their spawn region changes to spawn all their creeps into an arena with an overpowered giant chicken that kills them all. That might even be more entertaining.
If you null the region then you're banking on the fact that any trigger which references it will continue to work. Warcraft 3 is often pretty good at dealing with nulls, but in some cases you might end up with issues. For example, units being created at coordinates (0,0,0), which is a default fallback. You may also have logic that depends on the (Last created unit) which will continue to run despite a unit failing to be created (no region, no unit), thus affecting some unintended unit. I'm not saying that these are certain to happen but that these are the types of issues that exist.

Replacing the For Loop with Pick Every Player isn't that difficult, they're both loops. I could do it for you if you don't mind, I'm pretty quick with the editor - assuming that you're on the latest patch (I'm also about to go to bed so it'd have to wait ~12 hours). Otherwise, no worries.
 
Back
Top