This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Can I use the same variable name for different scripts?

Help for those learning Tcl or writing their own scripts.
Post Reply
d
daigo
Voice
Posts: 37
Joined: Fri Jun 27, 2014 8:02 pm

Can I use the same variable name for different scripts?

Post by daigo »

I am running all scripts on one eggdrop bot. Is it okay for me to use the same variable name if they are in different scripts? For instance, if I have this line in one script:

Code: Select all

set txt [string tolower $txt]
Can I use the same line of code in another script? Or do I have to change

Code: Select all

txt
to something else?
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

An Eggdrop bot can have only One Global Variable named txt

Usually txt is used in a proc, and that would be a local variable.
In most cases, local variables do not interfere with global variables.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

d
daigo
Voice
Posts: 37
Joined: Fri Jun 27, 2014 8:02 pm

Post by daigo »

willyw wrote:You might like to read:

http://forum.egghelp.org/viewtopic.php?t=12900
The article says to use

Code: Select all

bind pub - "hi" MyScript::respond 
in their example. What is the difference between using this and

Code: Select all

bind pub - "hi" ::MyScript::respond 
?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

My guess is that the difference is that in the second the respond proc is inside the MyScript namespace.
Once the game is over, the king and the pawn go back in the same box.
d
daigo
Voice
Posts: 37
Joined: Fri Jun 27, 2014 8:02 pm

Post by daigo »

caesar wrote:My guess is that the difference is that in the second the respond proc is inside the MyScript namespace.
Both of them are inside the MyScript namespace (I'm looking at another script that uses the double colons before the MyScript bind). What's interesting is, this script I'm looking at uses global. Here is the script:

Code: Select all

namespace eval magic8ball {

variable 8ballCatch "!8ball"

proc 8ball {nick host handle channel arg} {
  global 8ballCatch
  set cmd [string tolower [lindex $arg 0]]

  if {[file exists /eggdrop/scripts/8ball.txt]} {
    set list [open /eggdrop/scripts/8ball.txt r]
    set reply ""
    while {![eof $list]} {
      set tmp [gets $list]
      set reply [lappend test [string trim $tmp]]
    }
    close $list
  }
  set number [rand [expr [llength $reply] + 1]]
  puthelp "PRIVMSG $channel :8-ball\: [lindex $reply $number]"
}
bind pub - $8ballCatch ::magic8ball::8ball
}
User avatar
CrazyCat
Revered One
Posts: 1303
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

I think that the utility of the lead :: is essentialy when you use the import/export.
And it might be a little bit faster for the tcl to use the full path (::namespace::procedure) than looking in the namespace for the existence of a sub-namespace and then looking in global.
Post Reply