Using Variables

From EventScripts Community Encyclopedia

Contents

What is a "variable" and how do I make one?

The term for "making" a variable is called "declaring" a variable. A variable is either a word or number that is "set" to a certain value. For the most part, coders/scripters try to give variables unique names so that they can recognize the variable later in the coding process.

EXAMPLE VARIABLES

For a player's heath: myplayer_health For a player's money: myplayer_money For a player's name: myplayer_name

Take notice that the variables above do not have spaces in them. For the most part, NO variable should EVER have a space in it.

Syntax

You may ask: What is "syntax"? Syntax is "how" the code is supposed to be ordered to work properly. That being said, here is the proper syntax to use es_set and es_xset properly:

Syntax: es_set <variable> <value>
Note: Command parameters are described inside the < and > characters. Optional parameters are contained within [ and ] characters.
  1. Creates a custom server variable and sets its value to the value listed. EventScripts will expand any variables it finds.
  2. (Equivalent to running "es es_xset var val".)
  3. After creation, you can use the variable just like a server variable (e.g. via server_var()).
  4. Behaves just like Valve's "set" command.
Syntax: es_xset <variable> <value>
Note: Command parameters are described inside the < and > characters. Optional parameters are contained within [ and ] characters.
  1. Creates a custom server variable and sets its value to the value listed.
  2. No variables are expanded as it is assumed everything is already perfect. See es_x commands for more information. After creation, you can use the variable just like a server variable (e.g. via server_var()).
  3. Behaves just like Valve's "set" (and es_xset is even accessible as "set" on Linux since Valve wouldn't support set on Linux)

As an example of the syntax above, let's say that we wanted to create a variable that holds my age.

Let's call the variable that will hold my age: myage Let's say that I want the value of my age to be: 25

Here is how it would be coded:

es_set myage 25

or using es_xset:

es_xset myage 25

What is the difference between es_set and es_xset?

es_set

When you use "es_set", it will expand any variable that it finds. For example, if you want to set a variable for a player's health, then here is how it would be coded:

es_set player_health event_var(es_userhealth)

Another example: player name

es_set player_name event_var(es_username)

Once you have "declared" these variables, you can make use of them. They are now called "server variables". The shortened term for this is server var. For example, you can use the "es_msg" command to send this newly-coded information to the player chat area:

es_msg server_var(player_name) has a health of: server_var(player_health)

When the players see the message in the chat area, it will look like this (if the player name that you defined is "XE_ManUp"): Player Chat Message wrote:

XE_ManUp has a health of: 75

es_xset

When you use "es_xset", it WILL NOT EXPAND any variable that it finds. (See es_x commands for more information.) For example, if we were to follow the above example for attempting (I will explain in a moment) to use "es_xset" to set the player's health and player's name to a variable:

es_xset player_health event_var(es_userhealth)
es_xset player_name event_var(es_username)

Then, if we use the following code to display the above codes to chat:

es_msg server_var(player_name) has a health of: server_var(player_health)


The message displayed to chat will look like: Player Chat Message wrote:

server_var(es_username) has a health of: server_var(es_userhealth)

The above is bad code because with es_xset, no variables are expanded! If you want variables to be expanded, then use es_set.


Why use "es_xset" if it doesn't expand variables?

Like the example of "myage" above, not every variable needs to be set to a value that needs expanded. A few good examples of "when" to use "es_xset" instead of "es_set":

  • Setting the player's score.
  • Setting a round counter.
  • Setting a value from "0" to "1".

Advanced Note

You can (and should) use es_xset any time it is following an "if" statement. If es_xset is on the same line following the "if" statement, any variable found will be expanded.

Good Example Code: (player_say.cfg)

if (event_var(text) equalto "what's my name?") then es_xset player_name event_var(es_username)
if (event_var(text) equalto "what's my name?") then es_xmsg Your name is: server_var(player_name)

The above code will display this in player chat:

Your name is: XE_ManUp 


BAD Example Code: (player_say.cfg)

es_xset player_name event_var(es_username)
es_xmsg Your name is: server_var(player_name)

The above code will display this in player chat:

Your name is: server_var(player_name)

What is the difference between es_set and es_setinfo?

es_setinfo is depricated (older and no longer recommended). es_set performs the same function, but with the option to include a description of the variable.

What is the difference between server_var and event_var?

The underlying c++ code causes this requirement. When events are fired, they are passed as c++ objects. The variables germane to the event are retrieved from this object, unlike server variables. Thus, server_var retrieves a variable from the pool of all server variables, while event_var retrieves variables from the currently executing event. This helps keep excessive unnecessary-to-access event variables from cluttering the server variable namespace.

Final Notes

When you decide to declare a variable, make sure that if you are setting the variable with es_set, that the event you are using provides the information that you are trying to set.

To test, you can simply use es_msg to display the information you are trying to set to a variable...like "event_var(es_username)".

es_msg The username is: event_var(es_username)

If the message displayed in player chat is:

The username is: 0 

then, you know that the particular event that you are trying to use does not give information about the player.

As a quick "heads up", two events that will not give you ANY information about individual players are:

  • round_start.cfg
  • round_end.cfg
blog comments powered by Disqus