• Check out the results of the Techtree Contest #19!
  • 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.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

Chat message strings to variables

Status
Not open for further replies.
Level 4
Joined
Jul 11, 2007
Messages
82
Hi, I need some help making a trigger than take 2 parts from a chat message and stores them as string variables. In the map I'm making, I'm now implementing a "clan match" type system. The host needs to set the match with the command "-match XXX YYY" where the X's and Y's are the name of two teams. The string variable will default to "Team 1" and "Team 2", but I would like to be able to change them to something else.

So the event would be:
  • Player - Player 1 (Red) types a chat message containing -match as A substring
And then it needs to find the two words after than message and set each of them into a variable. Also, if the message doesn't contain exactly 3 parts (-match firstname secondname) then an error message should come up or something.

I asked about another similar trigger before in this thread, which may be similar to this one.
Player - Player 1 (Red) types a chat message containing -score as A substring
 
Rawr.

Returns the last array slot used in the words array as an integer. Stores different words in the global string array 'words'.

s is the input string, split is the string which is a split between words (In your case, a space).

JASS:
globals
    string array words
endglobals

function SplitString takes string s, string split returns integer
    local integer i = 0
    local integer lw = 0
    local integer l = StringLength(s)
    local integer ls = StringLength(split)
    local integer count = 0
    if SubString(s,l-ls,l) == split then
        set s = SubString(s,0,l-ls)
    endif
    loop
        exitwhen i >= l
        if SubString(s,i,i+ls) == split then
            set words[count] = SubString(s,lw,i)
            set count = count + 1
            set lw = i + ls
            set i = lw - 1
        endif
        set i = i + 1
    endloop
    set words[count] = SubString(s,lw,l)
    return count
endfunction

In your case,

JASS:
call SplitString("-match XXX YYY"," ")

//would result in

words[0] = "-match"
words[1] = "XXX"
words[2] = "YYY"
 
JASS:
//TrgCamera
//functions
function TrgCamera_Actions takes nothing returns nothing
   local integer a = StringLength(GetEventPlayerChatString())
   local real b = S2R(SubString(GetEventPlayerChatString(), 8, a))
    call SetCameraFieldForPlayer(GetTriggerPlayer(), CAMERA_FIELD_ZOFFSET, b, 1.00)
    call SetCameraFieldForPlayer(GetTriggerPlayer(), CAMERA_FIELD_ANGLE_OF_ATTACK, -90, 1.00)
endfunction

//Trigger
function Init_TrgCamera takes nothing returns nothing
    set TrgCamera = CreateTrigger()
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(1), "-camera", false )
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(3), "-camera", false )
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(5), "-camera", false )
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(6), "-camera", false )
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(8), "-camera", false )
    call TriggerRegisterPlayerChatEvent( TrgCamera, Player(10), "-camera", false )
    call TriggerAddAction( TrgCamera, function TrgCamera_Actions )
endfunction
//End TrgCamera

That's mine, purpleroot's is probably [certainly] better though. Mine is just looking at 2 substrings, it then converts the number part for use in setting the camera, I also set the Camera angle of attack to looking directly down.

But Purpleroot's is better.
 
Thank you very much Purple. This is the trigger I use to start it, but it won't correctly show the names of the teams. I'm sure it's just a simple mistake I made.

JASS:
function Trig_Untitled_Trigger_002_Actions takes nothing returns nothing
    call SplitString(GetEventPlayerChatString()," ")
    call TriggerSleepAction(1.0)
    call DisplayTimedTextToForce( GetPlayersAll(), 3.00, ( "Team 1 is named " + words[1] ) )
    call DisplayTimedTextToForce( GetPlayersAll(), 3.00, ( "Team 2 is named " + words[2] ) )
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_002 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_002 = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Untitled_Trigger_002, Player(0), "-match", false )
    call TriggerAddAction( gg_trg_Untitled_Trigger_002, function Trig_Untitled_Trigger_002_Actions )
endfunction

Also, thank you DrazharLn. I'll look at yours in a minute.
 
What are you inputting, and what does it spit out? I thought I fixed all the bugs, but there may still be one hanging around.

Also, you should store the words[] array to a local so that it survives the wait properly (Muiltiinstanceable).

The tests I did:

SplitString("I have a long string with spaces"," ")

SplitString("Ilolhavelolalollonglolstringlolwithlol","lol")

As well as many variations on the above.
 
I put in "-match abc xyz" or "-match lulz srs" . Stuff like that.

I realized I messed up what I posted, but I tried to put this there:
JASS:
set udg_TeamName[1] = words[1]
set udg_TeamName[1] = words[1]

and then I would make a message display that said

Team 1 is named udg_TeamName[1]

but it left a blank.

What I need is for it to set a global variable to the first word after -match, and another variable to the second word. Also, for some reason as I was trying different things, I got a lot of fatal errors right as it started loading. I would change something, press test, then got a fatal, then tried again, and it worked. So maybe about half the time I got a fatal. Anyone have an idea on what might be causing that?
 
You're using NewGen, right? If so, you have to save before you test map.

As for your problem, I honestly can't say what's causing it, seeing as I can't recreate it myself. It works fine in my tests.

Try this:

JASS:
call SplitString("-match lulz srs"," ")
call BJDebugMsg(words[0])
call BJDebugMsg(words[1])
call BJDebugMsg(words[2])

And see what you get.
 
EDIT: winrar!

I got it now. I guess I had to use a local string and set it to GetEventPlayerChatString.

JASS:
function Trig_Untitled_Trigger_002_Actions takes nothing returns nothing
    local string input = GetEventPlayerChatString()
    call SplitString(input," ")
    call BJDebugMsg(words[0])
    call BJDebugMsg(words[1])
    call BJDebugMsg(words[2])
endfunction

Thank you very much. Now the next version of Gnome Sploders will have a match mode :)
 
Status
Not open for further replies.
Back
Top