Substring Help Please

Status
Not open for further replies.
Level 3
Joined
Apr 16, 2010
Messages
21
I looked around and couldn't really find a simple explanation of how substrings work in the WE. The example they give in the trigger editor for substrings is:

Example: Substring("Grunts stink", 2, 4) = "run".​

Unfortunately, I can't figure out at all what this means. What I'm trying to do is make a resource trading trigger that uses entered chat strings from players as the trigger event and values of the trigger. So if anyone could enlighten me as to how substrings work and point me in the right direction for making this trigger, I would greatly appreciate it!
 

ZIR

ZIR

Level 10
Joined
Jan 12, 2010
Messages
321
(String from which you want to get a substring, Position of the character from where you want to start reading a substring, Position of the last character you want to read into your substring)

This is the meaning of the parameters.
So the example that you want to get a Substring from the String "Grunts stink" and you start at the position 2 (In the world editor counting begins at 1 not at 0), so the first letter of the substring is a "r" and you will end at the 4th character so you read 2,3 and 4th character of the string into your substring.

Just another example:
You want to make a sell system via the chat.
The standard command is "-sell XXXX" the X's represents a random item name.
You can get the item name via Substring("-sell XXXX", 7,end of string)
I hope this enlighted you the usage of Substrings.
 
Level 3
Joined
Apr 16, 2010
Messages
21
So let me see if I get this.
If I had the triggering string be "-give 01 1000", to identify which player to give the 1000 gold to, would the condition be:
substring("-give" 7, 8) = 01
That would be the condition in the if/then/else trigger function to give Red the gold. But then I have another question. How could I make it so that whatever number they entered in the give gold would be sent? there will be variables because they could give X, XX, XXX, or XXXX as a value. How could I make the trigger identify the number? I couldn't find an end of string interger value for the numbers in the substring example.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
It is used to give a slice of a string. In programming the same consept can apply to other data types like arrays or lists.

The actual mechanics behind the GUI one are warped.
SubString(source, start-1, end)
This is the code that is run every time you reference it in GUI so might be why you are having problems understanding it in comparison to those you may have used in other programming langguages.

Basically, strings start with the left most character being 0 and each character to the right of that is incrimented by 1.
Grunts
012345
The start point marks the first character where the selection starts. In the example given it is 2 but cause of the way GUI works it passes to the native 2-1 which becomes 1 so the start character is r.
It then itterates until the end is reached, in this case 4.
So it has r and then moves up to character 2. As 2 is not equal to 4 it adds character 2 to the selection.
So it has ru and then moves up to character 3. As 3 is not equal to 4 it adds character 3 to the selection.
So it has run and then moves up to character 4. As 4 is equal to 4 returns the selection (run).

This is the logic how it works. Remember though in GUI the start position has 1 added to it.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Although esentially how each individual string is made. They are not handled like an array as they are immutable. They instead make new objects for each string you generate and use hashtables to recall previously generated strings. This is infact an uncurable leak in WC3 whereby not all copies of strings are subject to the garbage recycling process thus you will get slowdowns late game if you make a lot of unique strings.

There can be up to 4 copies of the same string in RAM at any time (all in different address ranges).
 
Level 4
Joined
May 23, 2010
Messages
83
you can recognize a string by converting it to integer and then give it to a player like this:

  • Events
    • Player - Player x types a text message cointaing -give as a substing
  • Conditions
  • Actions
    • set EnteredStringPlayerIndex = substring ((entered chat string), 7, 8)
    • set EnteredStringGold = substring ((entered chat string), 10, 99)
    • set ReceivingPlayer = Player(String(EnteredStringPlayerIndex))
    • set XGold = String(EnteredStringGold)
    • Player - Add XGold to ReceivingPlayer
    • Player - Subtract XGold from (Triggering Player)
that should work :). remenber if the string converted has a letter in the substring it will return 0, so the gold amount will be 0 and the player will be 0 (i dont know if a the player index 0 will return a player)

so you can add a condition to verify if the player is playing (to avoid sending gold to computer players) and you need to count the player gold (if he has the typed gold then give the gold, if not, block the trigger and send a message to player.) try this instead:

  • Events
    • Player - Player x types a text message cointaing -give as a substing
  • Conditions
  • Actions
    • set EnteredStringPlayerIndex = substring ((entered chat string), 7, 8)
    • set EnteredStringGold = substring ((entered chat string), 10, 99)
    • set ReceivingPlayer = Player(String(EnteredStringPlayerIndex))
    • set XGold = String(EnteredStringGold)
    • If, Then, Else
      • If (Conditions)
        • ((Triggering Player)´s current gold) greater than or equal to XGold
        • ReceivingPlayer status equal to Is playing
        • ReceivingPlayer controller equal to user
      • Then (Actions)
        • Player - Add XGold to ReceivingPlayer
        • Player - Subtract XGold from (Triggering Player)
      • Else (Actions)
        • Game - Display to (Triggering Player) the text: ´You do not have enought money or targeted player does not exists or is not controlled by a user.´
 
Level 12
Joined
Apr 4, 2010
Messages
862
you can recognize a string by converting it to integer and then give it to a player like this:

  • Events
    • Player - Player x types a text message cointaing -give as a substing
  • Conditions
  • Actions
    • set EnteredStringPlayerIndex = substring ((entered chat string), 7, 8)
    • set EnteredStringGold = substring ((entered chat string), 10, 99)
    • set ReceivingPlayer = Player(String(EnteredStringPlayerIndex))
    • set XGold = String(EnteredStringGold)
    • Player - Add XGold to ReceivingPlayer
    • Player - Subtract XGold from (Triggering Player)
that should work :). remenber if the string converted has a letter in the substring it will return 0, so the gold amount will be 0 and the player will be 0 (i dont know if a the player index 0 will return a player)

so you can add a condition to verify if the player is playing (to avoid sending gold to computer players) and you need to count the player gold (if he has the typed gold then give the gold, if not, block the trigger and send a message to player.) try this instead:

  • Events
    • Player - Player x types a text message cointaing -give as a substing
  • Conditions
  • Actions
    • set EnteredStringPlayerIndex = substring ((entered chat string), 7, 8)
    • set EnteredStringGold = substring ((entered chat string), 10, 99)
    • set ReceivingPlayer = Player(String(EnteredStringPlayerIndex))
    • set XGold = String(EnteredStringGold)
    • If, Then, Else
      • If (Conditions)
        • ((Triggering Player)´s current gold) greater than or equal to XGold
        • ReceivingPlayer status equal to Is playing
        • ReceivingPlayer controller equal to user
      • Then (Actions)
        • Player - Add XGold to ReceivingPlayer
        • Player - Subtract XGold from (Triggering Player)
      • Else (Actions)
        • Game - Display to (Triggering Player) the text: ´You do not have enought money or targeted player does not exists or is not controlled by a user.´

a PICTURE speaks a thousand word! after view so much typing, i cant understand. but after looking at ur triggered picture, i understood! + rep for u
 
Status
Not open for further replies.
Top