HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Help with chat substrings

08-27-2009, 10:15 AM#1
Fluff
I'm making a trigger that makes two variables from a chat message containing "-tribute" in the form "-tribute XX YY" one variable (here s1) should be set to XX, and the other variable (s2) should be set to YY. I don't know what I'm doing so you'll see I use spacer1 and spacer2 to indicate the locations of the spaces in the chat message to separate XX and YY because those two things will be of varying lengths.

This doesn't work and I don't know why. It doesn't display anything because the s1 and s2 strings are, for some reason, not being set correctly.

Collapse JASS:
function Trig_Tributing_Actions takes nothing returns nothing
    //local string chatmsg = GetEventPlayerChatString()
    local integer chatmsglength = StringLength(GetEventPlayerChatString())
    local integer spacer1 = 9
    local integer spacer2
    local integer i
    local string s1
    local string s2
    set i = spacer1 + 1
    loop   
        if ( SubStringBJ(GetEventPlayerChatString(), i, i) == " " ) then
            set spacer2 = i
        endif
        set i = i + 1
        exitwhen spacer2 > 0
        exitwhen i > chatmsglength
    endloop
    set s1 = SubStringBJ(GetEventPlayerChatString(), (spacer1 + 1), (spacer2 - 1) )
    set s2 = SubStringBJ(GetEventPlayerChatString(), (spacer2 + 1), chatmsglength )
    call DisplayTimedTextToForce (GetPlayersAll(), 5, s1)
    call DisplayTimedTextToForce (GetPlayersAll(), 5, s2)
endfunction

If that makes no sense to anyone don't worry. I usually only get motivation to work on difficult things at night, which is of course when I can't think straight. If there is another way to do what I'm try to get then that would be helpful as well.
08-27-2009, 12:24 PM#2
Themerion
First of all, it _is_ okay to store the chat string in a variable....

Secondly, you could do these optimizations (not that it will matter, but hey, why do it the bad way?)

Collapse JASS:
call DisplayTimedTextToForce (GetPlayersAll(), 5, s1)
call DisplayTimedTextToForce (bj_FORCE_ALL_PLAYERS, 5, s1)
//---
call SubStringBJ(chatmsg, i, i)
call SubString(chatmsg, i-1, i) // Because that's how it works in real programming languages, for instance: Java.

Collapse JASS:
function Trig_Tributing_Actions takes nothing returns nothing
    //local string chatmsg = GetEventPlayerChatString()
    local integer chatmsglength = StringLength(GetEventPlayerChatString())
    local integer spacer1 = 9

//=========================================================================
// By setting this initial value, your script will work just fine...
    local integer spacer2 = 0
//=========================================================================

    local integer i
    local string s1
    local string s2
    set i = spacer1 + 1
    loop   
        if ( SubStringBJ(GetEventPlayerChatString(), i, i) == " " ) then
            set spacer2 = i
        endif
        set i = i + 1

//=========================================================================
   // Here you try to read spacer2 without having it set first.
   // Causes the thread to return immediately.
        exitwhen spacer2 > 0
//=========================================================================

        exitwhen i > chatmsglength
    endloop
    set s1 = SubStringBJ(GetEventPlayerChatString(), (spacer1 + 1), (spacer2 - 1) )
    set s2 = SubStringBJ(GetEventPlayerChatString(), (spacer2 + 1), chatmsglength )
    call DisplayTimedTextToForce (GetPlayersAll(), 5, s1)
    call DisplayTimedTextToForce (GetPlayersAll(), 5, s2)
endfunction
08-27-2009, 06:43 PM#3
Fluff
Thanks Themerion, I appreciate the help. I'm now able to get it to correctly display those two strings. I realize that those two bits aren't optimized, but they'll be okay I think for now. I'm not even going to display and text. I just needed to see if those work.

Problem solved