• 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.

Substring obstacle

Status
Not open for further replies.
Level 18
Joined
May 11, 2012
Messages
2,103
How can I check does the
  • Player - Player 1 (Red) types a chat message containing <Empty String> as A substring
event contain certain string?

Example: I write any string in it. Now through conditions, how can I check does Entered Chat String contain let's say "ar" or "bg" or something like that?
 
If your event is for example:
  • Player - Player 1 (Red) types a chat message containing "ar" as A substring
Then there is need to check if entered chat string contains "ar", because it does. If not the trigger would not be fired. (captain obvious told me ^_^)

If you want to check if "ar" is written as 2nd and 3rd character for example you can use a string comparisson for it:
Substring(EnteredChatString, 2, 3)) Equals "ar" == true

Does this solve your problem?
 
Your question seems pretty vague.

Nevertheless.

  • TEST
    • Events
      • Player - Player 1 (Red) types a chat message containing - as A substring
    • Conditions
      • (Length of (Entered chat string)) Equal to 3
      • (Substring((Entered chat string), 1, 1)) Equal to -
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Substring((Entered chat string), 2, 3)) Equal to ar
        • Then - Actions
          • Do something
        • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Substring((Entered chat string), 2, 3)) Equal to bg
            • Then - Actions
              • Do something
            • Else - Actions
When the player enters "-ar" or "-bg" something happens.
 
Level 18
Joined
May 11, 2012
Messages
2,103
Not quite, I know that haha
But I need to check if the string contains "ar" even if I write "fsdnbgzoebouiar"
The string above contains "ar" inside it, but how can I check if it is in?

The thing is, idk which character is "ar"...

Solu9, that doesn't helps either... What if I write "-angoiuardbuigbg"?
How can I retrieve the "ar" and "bg" inside that string?
 
Level 18
Joined
May 11, 2012
Messages
2,103
Then
  • Player - Player 1 (Red) types a chat message containing bg as A substring
Should do the trick.

If you JUST want to detect if the player types bg as a part of a word or sentence.

But I can't put "bg" in event. It must be empty string because a player can and most likely will type more than just "bg". Might type "-arbg" or "-bgar" or "ar-bg" adn many other words...
 
But I can't put "bg" in event. It must be empty string because a player can and most likely will type more than just "bg". Might type "-arbg" or "-bgar" or "ar-bg" adn many other words...

That doesn't matter.
If you use that event it will check all of the text a player types in. If bg is appearing in whatever the player writes the trigger will detect it.

Fx.
If the player writes: iughb45123hb€@$£€nkjhnd2bg332kjkjopimj
The trigger will still detect - iughb45123hb€@$£€nkjhnd2bg332kjkjopimj
 
Level 18
Joined
May 11, 2012
Messages
2,103
That doesn't matter.
If you use that event it will check all of the text a player types in. If bg is appearing in whatever the player writes the trigger will detect it.

Fx.
If the player writes: iughb45123hb€@$£€nkjhnd2bg332kjkjopimj
The trigger will still detect - iughb45123hb€@$£€nkjhnd2bg332kjkjopimj

Then
  • (Substring((Entered chat string), 1, (Length of (Entered chat string)))) Equal to -
what should I put instead of 1 in this condition? Because nobody knows where that "ar" or "bg" or "-" might be...
 
Have you even read my post above?

And if you really want to find the exact position you can run a loop from 1-Length(EnteredChatString) ... example is to find "ar":

If you find character "a" you check if character before "a" is a space, if next character is a "r" and also if the character after the "r" is a space again (or if length of string is reachted then it's no space)
 
Level 18
Joined
May 11, 2012
Messages
2,103
Have you even read my post above?

And if you really want to find the exact position you can run a loop from 1-Length(EnteredChatString) ... example is to find "ar":

If you find character "a" you check if character before "a" is a space, if next character is a "r" and also if the character after the "r" is a space again (or if length of string is reachted then it's no space)

Oh, I didn't notice you posted, I will try this

I'm sorry. I'm lost on what you are trying achieve.

Because you either want to run a trigger if bg is apparent in whatever text is typed.
or
You want to detect if bg is apparent in a certain location in the typed text.

I think you need to explain exactly what, and why, you are trying to accomplish :)

A player types game mode regardless of the mode order "-arbg", "bg-ar" , "arbg-", "bgar-" etc..
And if "ar" is written, I save that in a string, same for all other things he writes.
And then I run the triggers if the string contains that mode

  • Mode Init
    • Events
    • Conditions
    • Actions
      • For each (Integer TempInt) from 1 to (Length of (Entered chat string)), do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Substring((Entered chat string), TempInt, TempInt)) Equal to -
            • Then - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Substring((Entered chat string), TempInt, (TempInt + 1))) Equal to ar
                • Then - Actions
                  • Set GameMode[1] = |c0040E0D0All Random|r
                  • Trigger - Run All Random <gen> (checking conditions)
                  • Trigger - Turn on AR Repick <gen>
                  • Trigger - Turn off Repick <gen>
                  • Trigger - Turn off Mode On Off <gen>
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Substring((Entered chat string), TempInt, (TempInt + 1))) Equal to bg
                • Then - Actions
                  • Set GameMode[2] = |c0040E0D0Bonus Gold|r
                  • Trigger - Turn on Bonus Gold <gen>
                • Else - Actions
              • Skip remaining actions
            • Else - Actions
      • Set GameMode[3] = (GameMode[1] + ( + GameMode[2]))
      • Game - Display to (All players) the text: (((PlayerColors[(Player number of MainPlayer)] + (Name of MainPlayer)) + |c00FDD017has selected ) + GameMode[3])
      • Trigger - Turn off (This trigger)
This is what I'm at now.
 
If character at X == "-" then you have to check if Substring(x+1, x+2) == "ar"

Also you dont check if "ar" comes later, and not immediatly after the "-".

You can use a single string for modes:

If blah blah = blah blah then
Set GameModes = (GameModes + " All Random ")

And in end display GameModes, and it will be addition of all modes together.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Jass does not provide many string manipulation functions natively. So you have to write them yourself. To search for the occurence of a string inside another, you can iterate over it and always compare the substring from curPos to curPos + length of searched term - 1 to the term. In my map, I split the input into words separated by whitespace because you would normally not want "-dasdsadar" count as "ar".
 
Level 18
Joined
May 11, 2012
Messages
2,103
Lol people, I did it hahaha :D
Feels so good now

  • Mode Init
    • Events
    • Conditions
      • (Substring((Entered chat string), TempInt, (TempInt + 1))) Equal to -
    • Actions
      • For each (Integer TempInt) from 2 to (Length of (Entered chat string)), do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Substring((Entered chat string), TempInt, (TempInt + 1))) Equal to ar
            • Then - Actions
              • Set GameMode = (GameMode + |c0040E0D0All Random|r)
              • Trigger - Run All Random <gen> (checking conditions)
              • Trigger - Turn on AR Repick <gen>
              • Trigger - Turn off Repick <gen>
              • Trigger - Turn off Mode On Off <gen>
            • Else - Actions
      • For each (Integer TempInt) from 2 to (Length of (Entered chat string)), do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Substring((Entered chat string), TempInt, (TempInt + 1))) Equal to bg
            • Then - Actions
              • Set GameMode = (GameMode + |c00FDD017\|r|c0040E0D0Bonus Gold|r)
              • Trigger - Turn on Bonus Gold <gen>
            • Else - Actions
      • Game - Display to (All players) the text: ((PlayerColors[(Player number of MainPlayer)] + (Name of MainPlayer)) + ( |c00FDD017has selected + GameMode))
      • Trigger - Turn off (This trigger)
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
I do not see the problem.

Player - Player 1 (Red) types a chat message containing ar as A substring
Will detect any chat message that contains ar. The key part here is the "substring" part as that means it will match up messages that contain the string as any part of them. This is different from "exact match" which will only run the trigger if there is an exact match.

The following will fire the event as they contain "ar" as a substring.
ar
ar-whatever
We are doing very well.
sfjdsfsdsarsdfjnsdfnsdnf
-ar
StarCraft II

The following will not fire the trigger as they do not contain "ar" as a substring.
dog
Diablo III
WTF
------
-load xxxx

Iteration through all possible sub strings of a certain length from a message is not a good option due to string leaks.

But wait, how can I detect which command was mentioned if I have multiple such events attached to the same trigger? Blizzard thought of this and gave us the following native.
JASS:
constant native GetEventPlayerChatStringMatched takes nothing returns string
This will return the string used for matching by the chat event. All you need to do is check if this is equal to ar or bg and do whatever.

This converts the mega complex mess needed for a manual search into practically 2 events (per player) and an if statement with 2 branches. No loops, no leaks and a heck of a lot faster.
 
Status
Not open for further replies.
Top