Dynamic variable names

From Scriptwiki
Revision as of 22:29, 20 April 2016 by Cail (talk | contribs) (A notification about hash tables. Bindi is a dickbutt.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Dynamic variables allow you to store lots of information in several variables, all of which have different names that relate to the data stored in them. Dynamic variable names are useful if you need to be able to reference data-specific information. For example, you may have a join message script which displays a join message set for a particular user based on his nick name. You would need a way to store one join message for every user, and to get it out. You could not store all of this information to one variable, obviously, so we use several variables with dynamic (or changing) names.

Evaluation

Evaluation Brackets

Many people who try to create changing variables names on their own fail because they do not realize the need for evaluation brackets. These are the [ ] rectangular brackets located on English keyboards above the return/enter key and to the left.

Suppose we set a variable in a command such as this:

on *:text:!setmyjoin *:#help.script: {
 set %joinmessage. $+ $nick $2-
}

If aca20031 types "!setmyjoin I pwn you" in #help.script, the variable %joinmessage.aca20031 will be successfully set to "I pwn you". No evaluation brackets are required here during the set and var commands. Where issues arise commonly is the reading of these variables. Take a look at the following script:

on *:JOIN:#help.script: {
  if (%joinmessage. $+ $nick != $null) {
     msg $chan $nick $+ : %joinmessage. $+ $nick
  }
}

Many would expect this script to work fine, but it does not. What is wrong with it? Missing evaluation brackets. mIRC will evaluate %joinmessage. before adding $nick to the end. Therefore it will see:

if ($nick != $null)

Which is not what we wanted. This condition can never be false. Furthermore, it will try to message the channel with $null $+ $nick as the join message! The following is a correctly revised version of this script, using evaluation brackets.

on *:JOIN:#help.script: {
  if (%joinmessage. [ $+ [ $nick ] ] != $null) {
     msg $chan $nick $+ : %joinmessage. [ $+ [ $nick ] ]
  }
}

Eval and $+

Another way of correcting evaluation errors is by using a combination of $eval() (or $()) and $+. This is generally considered a much more clean and readable way of evaluation. It should be used exclusively when you have a variable name in which more than one part may change. For example, consider the following revision of the join message script, which allows for every channel to have their own unique join message for every user.

on *:text:!setmyjoin *:#: {
 set %joinmessage. $+ $chan $+ . $+ $nick $2-
}
on *:JOIN:#: {
  if ($($+(%,joinmessage.,$chan,.,$nick),2) != $null) {
     msg $chan $nick $+ : $($+(%,joinmessage.,$chan,.,$nick),2)
  }
}

Lets take a look at the evaluation:

  • $(X,2) indicates that we want to evaluate X twice.
    • 1: $+(%,joinmessage.,$chan,.,$nick) is evaluated to %joinmessage.#help.script.aca20031
    • 2: %joinmessage.#help.script.aca20031 is evaluated to "I pwn you"

See Also