mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
I would like to propose a 3rd type of variable scope. There are currently GLOBAL and LOCAL, I would like to add CONSTANT. They would actually be Script-level variables defined at the beginning of the script or anywhere outside of an event or alias; free floating. They would be writable and re-writable, but I can't think of a better name besides "constants" for a designation.

Their scope would be akin to the scope of "Alias -l". They would allow for initialization of default script settings, or as an easy method of personalizing a shared script.

For Instance:

A bot script designed to only operate in specified channels, while connected with the specified nickname(s), such that only events from this connection are interpreted by the script.

Example:

Code:
; My Simple Bot Script

SET %botnicks  Games,Gamez
SET %botchans  #games,#cards

Alias -l IsBot return $istok(%botnicks,$me,44)

On *:TEXT:*:%botchans: {
  if (!$IsBot) return

  xyzzy stuff only the bot would do

}

On me:*:JOIN:*: {
  if (!$IsBot) return
  set %botchans $addtok(%botchans,$chan,44)
}

On me:*:PART:%botchans: {
  if (!$IsBot) return
  set %botchans $remtok(%botchans,$chan,44)
}

...


As you can see, because %botnicks and %botchans were specified outside of any Event or Alias, they are both declared when the script starts, and they have a captive scope to this script alone (like Alias -l does) so there will be no conflict with other scripts or other global variables.

If a local variable is specified inside an Event or Alias, it would supersede these 'constant' variables, the same way they do with global variables. If a global variable is assigned inside an Event or Alias with a name matching a defined constant, that constant will be written to instead of a true global variable.

The primary gain here is simplicity in variable naming, without having to create cleverly-named global variables that might conflict with other scripts, and easy editing and personalizing of a script that somebody downloads. All the configurable features are right up at the top of the script (or just outside any events or aliases).

Thank you for the consideration, Khaled, as always!

Edit:

Maybe they can be called "Captive Variables" instead of "Constants"?

Last edited by Raccoon; 04/03/15 02:44 PM.

Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Jul 2006
Posts: 4,149
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,149
https://forums.mirc.com/ubbthreads.php/topics/247814/Script_specific_variables#Post247814

Edit: not a bad idea, but in my opinion, these new variable should not float around in the script file, because mIRC doesn't do that and it would require change...
Rather, I think we should have a new switch for var, -l, which would set a variable local to the script and if you want to initialize it to a default value, use the on start/on load events.



Code:
on *:start:{
  if ($starting) {
    var -l %botnicks Games,Gamez,%botchans #games,#cards
  }
}
on *:load:{
  var -l %botnicks Games,Gamez,%botchans #games,#cards
}
Alias -l IsBot return $istok(%botnicks,$me,44)

On *:TEXT:*:%botchans: {
  if (!$IsBot) return

  xyzzy stuff only the bot would do

}

On me:*:JOIN:*: {
  if (!$IsBot) return
  var -l %botchans $addtok(%botchans,$chan,44)
}

On me:*:PART:%botchans: {
  if (!$IsBot) return
  var -l %botchans $remtok(%botchans,$chan,44)
}


Remember that /set doesn't care if the variable is local or global, it just sets the variable, /set will keep a variable local if it was previously defined as local, it should keep doing that with those local script variables, meaning that on join/part here could use /set, it would keep them local to the script, however, for backward comp, it should set a global variable if you have a script-local variable and a global variable defined with the same name

Last edited by Wims; 04/03/15 03:14 PM.

#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard