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

[Trigger] -give

Status
Not open for further replies.
Level 3
Joined
Aug 16, 2012
Messages
21
Toyed around with this. Couldn't even come close to figuring out how to do it. Novice triggerer. Title says it all.

I want a player to be able to type -give COLOR and the unit selected will change ownership+color to that color.

I also want it done for each faction. So you have two methods of -give'ing. May seem unnecessary, but I am stubborn and want an easy command basis for players.

So.. -give FACTION, for example, will give the selected unit to the faction named.

example: -give red
-give rome
 
Last edited:
Level 25
Joined
Jul 10, 2006
Messages
3,315
Start by creating a string array, let's call it colourText[], which you'll use to store the colour texts.

So you'll set: (run this code at map initialization)
  • Set colourText[1] = red
  • Set colourText[2] = blue
etc for the rest of the player colours

Now make a new trigger, with the following event:
  • Player - Player 1 (Red) types a chat message containing -give as A substring
  • Player - Player 2 (Blue) types a chat message containing -give as A substring
(copy this event for each player)

Then you need to check if the input text is any of the colourTexts you set earlier. Do this using a for loop.

So for each integer A from 1 to 12, check if the input text is that string, and if it is, changing ownership of selected units is easy.

I hope that's at least a little helpful; if you don't understand for loops and strings you might need a longer tutorial though...
 
Level 37
Joined
Mar 6, 2006
Messages
9,240

  • Untitled Trigger 035
    • Events
      • Player - Player 1 (Red) types a chat message containing -give as A substring
    • Conditions
      • (Substring((Entered chat string), 1, 5)) Equal to -give
    • Actions
      • Set s = (Substring((Entered chat string), 7, (Length of (Entered chat string))))
      • For each (Integer A) from 1 to 12, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • s Equal to StringArray[(Integer A)]
            • Then - Actions
              • Custom script: set bj_wantDestroyGroup = true
              • Unit Group - Pick every unit in (Units currently selected by (Triggering player)) and do (Actions)
                • Loop - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Owner of (Picked unit)) Equal to (Triggering player)
                    • Then - Actions
                      • Unit - Change ownership of (Picked unit) to (Player((Integer A))) and Change color
                    • Else - Actions
              • Skip remaining actions
            • Else - Actions


And init the array with values at map initialization.

EDIT: This is basically what the poster above said.
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
I like that you want to turn 1 command into 2.

Step 1: Variable setup
To do this, you need 2 variables: playerColor (string array) and factionName (string array).
In the trigger with as event "Map Initialization", set up the variables like this:
  • Set playerColor[1] = red
  • Set playerColor[2] = blue
  • -------- ... --------
  • Set factionName[1] = rome
  • Set factionName[2] = paris
  • -------- ... --------
Note that all of them are lower-case only, this is for later on, when we register what the players are saying.

I think the rest of that trigger should be clear.

Step 2: The command
The event is quite self-explanatory: Player (X) types -give as a substring.
"As a substring" means it can be anything that includes the text "-give".
  • Events
    • Player - Player 1 (Red) types a chat message containing -give as A substring
    • Player - Player 2 (Blue) types a chat message containing -give as A substring
    • Player - Player 3 (Teal) types a chat message containing -give as A substring
    • -------- ... --------
Now we need to know what it is that he wrote, we will use this action for that:
  • Set tempString = (String((Substring((Entered chat string), 7, (Length of (Entered chat string))))) as Lower case)
tempString is a new variable (string, no array).
Set tempString = "Convert - Convert String Case" -> "Substring".

As you can see, we make the entire text behind "-give" lower-case. This is because string comparisons are case-sensitive and we want the players to be able to write anything and not be bound to lower-case only.
(The length is from 7 to the end, because "-give" is 5 letters, behind that comes a space and then the 7th letter is the first letter of the word we're trying to look for).

We're also going to add this action (new variable: tempGroup, unit group no array):
  • Set tempGroup = (Units currently selected by (Triggering player))
That way we already know which units we want to change ownership of (if a valid command is given).

Now we're going to loop through the other strings (playerColor and factionName) to see if we can get a match:

  • For each (Integer A) from 1 to 8, do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • tempString Equal to playerColor[(Integer A)]
        • Then - Actions
          • Unit Group - Pick every unit in tempGroup and do (Actions)
            • Loop - Actions
              • Unit - Change ownership of (Picked unit) to (Player((Integer A))) and Change color
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Integer A) Less than or equal to 5
          • tempString Equal to factionName[(Integer A)]
        • Then - Actions
        • Else - Actions
Change the "from 1 to 8" for whatever there's most of (factions or players).
E.g.: if there's 8 players and 5 factions, then do as I did: loop from 1 to 8. If there are 8 players and 10 factions, then loop from 1 to 10.

Also, I don't know who the factions belong to, so I didn't really put any actions for those :S

At the end of the trigger, we want to remove the unit group:
  • Custom script: call DestroyGroup( udg_tempGroup )
I believe that should work.

Edit: Hah, I was taking a bit too long writing this I think :p
 
Level 3
Joined
Aug 16, 2012
Messages
21
Amazing. Thank you so much. The teams will be set up like Greece, if you've ever played that. A player is a faction. And there are 12 players/factions.
 
Status
Not open for further replies.
Top