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

Need help with changing ownership of a selected unit by chat command.

Status
Not open for further replies.
Level 1
Joined
Jul 13, 2011
Messages
1
How can I change ownership of a selected unit by typing something like "-give color".

Let's say I want to give a unit of mine to player blue. So I type in the message "-give blue" and then the selected unit of mine is changed ownership to blue. However, only the player of that unit can change the ownership of that unit. So If I'm player red, I'm the only one that change my unit's ownership and no other players are able to change the ownership of my units. So like only player red is able to change ownership of their units, only player blue is only able to change ownership of his units, and so on.

Similar Example: So I want to give a unit of mine to player green, so I type in "give green" and the selected unit of mine is given to green.

How do I do this trigger in world editor?
 
Level 11
Joined
Jun 2, 2004
Messages
849
To do all of this in a single trigger is a little bothersome, but possible.

First this should be the 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
  • //and the rest of the players
Next, loop through all the units selected by the player, and if they don't own one of them, end the trigger with Skip Remaining Actions.

Next, get the substring of the entered chat message:
  • Set TempString = (Substring((Entered chat string), 7, 99))
And compare the string to each of the colors (red, green, blue, etc), then give the units to the appropriate player. This will sightly mess up if there are trailing spaces ("red " != "red") but that's probably not a big deal.
 
Level 11
Joined
Jun 2, 2004
Messages
849
Fortunately the game will stop checking characters at the end of the string. Using the string length instead of a arbitrarily large number is still better coding practice though, of course.

Amusingly it doesn't check whether you specified 0 or negative values for the first argument, though. Nice way to get segfaults.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,199
Amusingly it doesn't check whether you specified 0 or negative values for the first argument, though. Nice way to get segfaults.
Technically it starts at 0. GUI adds an offset for some... reason.

JASS:
function SubStringBJ takes string source,integer start,integer end returns string
    return SubString(source, start-1, end)
endfunction
 
Status
Not open for further replies.
Top