String Manipulation

From EventScripts Community Encyclopedia

This is just a brief treatment of the many ways you can manipulate strings in EventScripts.

  • es_format <variable> <format-string> [token1] [token2] [token3] [...] [token9]

Formats a string according to the format-string and stores it into the variable command. EventScripts will expand any variables it finds. (Equivalent to running "es es_xformat".)

This is somewhat complicated if you haven't seen this sort of thing before. Once you get used to it, though, this is a very handy way to build up strings the way you may want. Basically, you specify a variable you want to store the data into as the first parameter. The format-string is simply some text with %1 in the place you want to place "token1", a %2 for token2, %3 for token3, and so on up to %9. Here's an example for player_death.cfg:

// EventScripts v0.6.0
es_setinfo mystring 0   // init
es_format mystring "%1 just killed %2 with a %3!" event_var(es_attackername) event_var(es_username) event_var(weapon)
es_msg server_var(mystring)

This code would send a message to everyone that looked like this:

Cr3V3TT3 just killed Mattie! with a deagle!

In addition, v0.7.0 of EventScripts introduced the ability to get a particular part of a string and store it into a variable:

  • es_token <variable> <string> <token#> [seperator-character]

Sets a variable to the part of the string corresponding with "token#". It splits up the string according to the seperator character which defaults to space. EventScripts will expand any variables it finds. (Equivalent to running "es es_xtoken".)

This is pretty simple once you look at it. Basically, you provide a variable (a server variable, or maybe one created via es_setinfo), a string, and which 'token' or part of the string you want. Here's an example of how it works:

setinfo myvar 0
es_token myvar "This is a long string, and I'm interested in the 4th word." 4
es_msg The string I found was: server_var(myvar)

This code would send the following text to all of the users on the server:

The string I found was: long

You can also tell it what kind of character you'd like to split the string up on. For example:

setinfo myvar 0
es_token myvar "This-is-a long-string, and-I'm-interested-in-the 4th: word!" 4 "-"
es_msg The string I found was: server_var(myvar)

This would split the string up using the dash (-) character. So the new output would actually be:

The string I found was: string, and

See Also

blog comments powered by Disqus