• 🏆 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 Set the Owner of Starting Units using GUI

Many might be wondering how to have more than the basic starting units created at their start location. There are many ways of doing this. The most common way is simply to create units at the player's start location, however this may not always work out too well, when you want several buildings to be given to the player.

If you are not entirely new to triggering, and know triggers well enough to know how a trigger functions when you see it, then you can simply skip to the bottom and replace parts of the trigger to do what you need, however you may need to read this entire tutorial in order to fully understand how the short trigger at the end works, and why parts of it are necessary.

1. The first thing we will need to do is to place whatever units we want a player to have on the map where you want them. In this example, I'm going to place a barracks somewhere close to each player's starting location.

2. Next, you will want to put all of your starting units into a unit group. We're going to use temporary groups and custom script in order to prevent memory leaks. This will add a few lines of code to your trigger, but it will free up your memory. If you only have a single barracks at each player's start location, then you can simply not include the custom script, however I'm going to include it, just in case a person might have hundreds of starting units.
Below I will include several different methods of setting your group of units. It is not necessary to include each of them; the various methods are organized by letter.

A) Now, to have the barracks be part of the starting group, I'm going to do
  • Set tempGroup = (Units of type Barracks)
B) If you wanted to have your tempGroup contain more than just this, there are multiple ways of doing so.
B)a. You could either
  • Unit Group - Add all units of (Units of type Farm) to tempGroup
B)b. Or, you could add units to the group individually, until your group contains every single unit that you want to have its ownership changed to the player starting near it.

C) Lastly, you could give all units which you want to have their ownership changed a blank aura (an aura with effects set to do nothing (0.00) and which has its targets set to Self. We will name this ability ReplaceAbility

You would then set the ability's buff to a custom buff which we will name ReplaceUnit. Now, all units with this ability will have this buff. You would then

  • Set tempGroup = (Units in (Playable map area) matching (((Matching unit) has buff ReplaceBuff) Equal to True))
Now, we should remove the ability from the unit, as, surely, you won't want the player to see this once they being playing. This can be easily done by

  • Unit Group - Pick every unit in tempGroup and do (Actions)
    • Loop - Actions
      • Unit - Remove ReplaceAbility from (Picked unit)
Now, it has set your tempGroup to all of the units with this buff which results from the ability, and removed the ability (as well as the buff) from the units.

3. Now that you have used one of the above methods to set your tempGroup, all you have to do is change the ownership of the units in this tempGroup.
Just in case you may want to add your own functions into the trigger, I'm going to break this into multiple lines to leave room for changing things.

First, we will pick each player. You may want to have a specific group of players, which you could set as a Force variable, but in this example we're just going to use All Players.

  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
We're going to use a second tempGroup (create another Unit Group variable called tempGroup2) for this part.
Now, we're going to set this group to the units in your previous tempGroup within a certain range of each player's start location. You can change the range to whatever you like.

  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Set tempGroup2 = (Units within 800.00 of ((Picked player) start location) matching (((Matching unit) is in tempGroup) Equal to True))
The way this script will run is: the game will run this trigger for each player in the game one at a time until it has run for all players.

4. Now, all you have to do is have the trigger change the owner of these new units to the player, by doing:
  • Unit Group - Pick every unit in tempGroup2 and do (Actions)
    • Loop - Actions
      • Unit - Change ownership of (Picked unit) to (Picked player) and Change color
Now, your trigger is fully functional and its actions should look something like this:
  • Set tempGroup = (Units of type Barracks)
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Set tempGroup2 = (Units within 800.00 of ((Picked player) start location) matching (((Matching unit) is in tempGroup) Equal to True))
      • Unit Group - Pick every unit in tempGroup2 and do (Actions)
        • Loop - Actions
          • Unit - Change ownership of (Picked unit) to (Picked player) and Change color
The first line of your script will vary, depending on which method you used to set your first tempGroup.

5. But wait! We're not quite done! Even though your trigger is fully functional and ready to use, it does contain a few memory leaks. In an initialization trigger, memory leaks are not a disaster, as such triggers will only be running once. If this were a repeating trigger, memory leaks would eventually crash the game. However, it is always a good idea to prevent memory leaks, no matter how seldom they might occur. This last part is very easy. All you have to do is create two lines of custom script:
  • Custom script: call DestroyGroup (udg_tempGroup)
  • Custom script: call DestroyGroup (udg_tempGroup2)
This will remove several kilobytes of stored information of these uneeded unit groups, as you most likely will never be using these particular units in a unit group throughout the rest of the game, and won't need to keep their information stored.

You probably won't want to type the customs script yourself; otherwise, you would probably be using JASS and wouldn't need to look at this tutorial, as JASS triggering is superior in efficiency, however has less of a guided interface to help for easy learning.
So, all you will need to do is create an action Custom script: and then paste: call DestroyGroup (udg_tempGroup)
into the line of custom script. You would do the same for the second line, adding a 2; call DestroyGroup (udg_tempGroup2).

Now, your trigger is all done! Depending on what methods you used to create the first tempGroup, you may need to create more lines of custom script to remove those leaks as well, however I'm simply going to provide this trigger:

  • Set tempGroup = (Units of type Barracks)
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Set tempGroup2 = (Units within 800.00 of ((Picked player) start location) matching (((Matching unit) is in tempGroup) Equal to True))
      • Unit Group - Pick every unit in tempGroup2 and do (Actions)
        • Loop - Actions
          • Unit - Change ownership of (Picked unit) to (Picked player) and Change color
      • Custom script: call DestroyGroup (udg_tempGroup2)
  • Custom script: call DestroyGroup (udg_tempGroup)

Remember, your script for the first tempGroup may vary, depending on what method you used to create it! But now, you have a system to create whatever starting units you might want! To add additional units to this trigger, you would simply
  • Set tempGroup = (Units of type Barracks)
  • Unit Group - Add all units of (Units of type Farm) to tempGroup
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Set tempGroup2 = (Units within 800.00 of ((Picked player) start location) matching (((Matching unit) is in tempGroup) Equal to True))
      • Unit Group - Pick every unit in tempGroup2 and do (Actions)
        • Loop - Actions
          • Unit - Change ownership of (Picked unit) to (Picked player) and Change color
      • Custom script: call DestroyGroup (udg_tempGroup2)
  • Custom script: call DestroyGroup (udg_tempGroup)


Notice the added line:
  • Unit Group - Add all units of (Units of type Farm) to tempGroup
You could repeat this step to add any units which you want to change into your trigger.
 
Last edited:
Top