• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Trigger] Spliting strings?

Status
Not open for further replies.
Level 22
Joined
Feb 3, 2009
Messages
3,292
Hello, I need help with making an alternative split function which works similar than in programming languages.

Say we have a command -say which takes 2 arguments (name, word)

Example usage in game:

-say Footman Yes my Lord

From the above it should save "Footman" into a variable ie. name and "Yes my Lord" into a variable ie. word.

I need this to make a command for the map which will write for the above example "Footman" in the color of the triggered player, followed by ": " and finally "Yes my Lord" in white after it.

Any ideas on how to go about making it?
 
Loop through the chat string until you find the first space character and knowing the command
Once you've reached the first, start adding characters to a string. When you've reached the second, save the rest of the chat string into another variable.

Finally, you'll have 2 variables.
One with the value "Footman" and the other with the value "Yes My Lord"
 
I recommend you use Romek's Explode String function:
http://www.thehelper.net/forums/showthread.php/120136-Explode-String

It is very useful. For example:
JASS:
call ExplodeString( GetEventPlayerChatString(), " ", 9 )

That will split it up into 9 parts. If you think there will be more parts, then you can just change it in the system to however many there possibly will be. Just read through the thread and you should have more than enough information to make it suit your needs. =) If you need me to, I can tweak it to make it more GUI friendly.

EDIT: Here you go:
JASS:
library ExplodeString
//  ________________________________________
// +----------------------------------------+
// |      E X P L O D E   S T R I N G       |
// +----------------------------------------+
// |            By Romek - v3               |
// |      Minor modifications by Purge      |
// |________________________________________|
// +----------------------------------------+

// CREATE A GLOBAL STRING ARRAY NAMED "ExplodedString"
    globals
        private integer size = 0
    endglobals
    
    function CountExplodedStrings takes nothing returns integer
        return size
    endfunction

    function ExplodeString takes string source, string separator, integer maxamount returns integer
        local integer i = 0
        local integer last = 0
        local integer first = 0
        local integer length
        local boolean insep = true
        loop
            exitwhen size == 0
            set udg_ExplodedString[size] = ""
            set size = size - 1
        endloop
        if maxamount < 1 then
            set maxamount = 1
        endif
        set maxamount = maxamount - 1
        if separator == "" or separator == null then
            set separator = " "
        else 
            set separator = SubString(separator, 0, 1)
        endif 
        set size = 0
        set source   = separator + source
        set length   = StringLength(source)
        loop
            exitwhen i > length
            if SubString(source, i, i + 1) == separator or i == length then
                if not insep then
                    set last = i + 1
                    set insep = true
                    if size  == maxamount then
                        set udg_ExplodedString[size] = SubString(source, first, length)
                        exitwhen true
                    endif
                    set udg_ExplodedString[size] = SubString(source, first, last - 1)
                    set size  = size  + 1
                endif
            elseif insep then
                set insep = false
                set first = i
            endif
            set i = i + 1
        endloop
        return size
    endfunction

endlibrary

Just make a string array named "ExplodedString". Then you would just use it like this:
  • Example
    • Events
      • Player - Player 1 (Red) types a chat message containing <Empty String> as A substring
    • Conditions
    • Actions
      • Custom script: call ExplodeString( GetEventPlayerChatString(), " ", 20 )
      • Custom script: set udg_StringCount = CountExplodedStrings()
      • For each (Integer A) from 0 to StringCount, do (Actions)
        • Loop - Actions
          • Game - Display to (All players) the text: ExplodedString[(Integer A)]
It is untested, but it should work.

From there, you would just modify it to suit your trigger needs.
 
Last edited:
Status
Not open for further replies.
Top