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.

global variables don't work

Help for those learning Tcl or writing their own scripts.
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

global variables don't work

Post by darton »

Hello!
Maybe it is just a stupid mistake, but I have made a script with global variables which don't work. Here is the script:

Code: Select all

set bla [string equal -nocase bla $nick]
set blabla [string equal -nocase blabla $nick]

bind pubm - "*!afk*" afk
proc afk {nick host hand chan arg} {
global bla blabla
 if {$bla || $blabla} {
  putquick "PRIVMSG $chan :$nick is AFK."
 } 
}
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well, what confuses me, if why you've put those two string equal-checks outside the proc...
NML_375
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

because I have many procs in one tcl-file and I must use these variables in almost every proc.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

What I mean is, do you update them at any time?
Would'nt it be easier to just make $nick global, and run the test inside the proc (won't increase load that much, probably won't even notice it)
NML_375
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

Yes I update it at any time with "global bla blabla". But does that increase load? I think there is no difference if I write "if {$bla || $blabla}" or "if {[string equal -nocase $blabla $nick]}"
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

This is what I'd suggest... Assuming from your first post that you wish to check wether the bot is afk or not, and reply "<yournick> is AFK" if so...

Code: Select all

proc {unick host hand chan text} {
 global nick
 if {[string equal -nocase "bla" $nick] || [string equal -nocase "blabla" $nick]} {
  putquick "PRIVMSG $chan :$unick is AFK"
 }
}
The load won't increase much, unless you plan to call this 10 times a second or such...

Also, my first question in my last post should actually have been: do you update those variables anywhere _else_ in the script while the bot runs
NML_375
User avatar
krimson
Halfop
Posts: 86
Joined: Wed Apr 19, 2006 8:12 am

Post by krimson »

@darton: the reason why the code in your first post won't work is because (probably) you didn't set $nick before setting $bla and $blabla. this is the reason why you should use both vars inside your procs
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

As I remember, $nick is a reserved variable, "always" set to whatever nick your bot currently uses..
Hav'nt dug into the source in a while, but I'd guess it's not set during startup..
I also get this distict feeling darton expects bla and blabla to be updated as soon as nick is changed... which is most definetly not the case, all "global" does is to link the local (function-instance) variable to the global one (ie making bla and blabla readable from within the function even tho they exist outside).

It feels like he actually wants to do some variable tracing (man n trace), although coding event-driven tcl isn't that pretty in eggies as I recall...
NML_375
User avatar
KrzychuG
Master
Posts: 306
Joined: Sat Aug 16, 2003 2:51 pm
Location: Torun, Poland
Contact:

Post by KrzychuG »

Can't you convert those global variables to procedures? I don't think you can use variable like you defined.

Code: Select all

proc bla { nick } { return [string equal -nocase bla $nick] }
proc blabla { nick } { return [string equal -nocase blabla $nick] }

bind pubm - "*!afk*" afk 
proc afk {nick host hand chan arg} { 
 if {[bla] || [blabla]} { 
  putquick "PRIVMSG $chan :$nick is AFK." 
 } 
}
Que?
User avatar
krimson
Halfop
Posts: 86
Joined: Wed Apr 19, 2006 8:12 am

Post by krimson »

if you used an argument when declaring the procs, you must use it when calling them too

Code: Select all

proc bla { nick } { return [string equal -nocase bla $nick] }
proc blabla { nick } { return [string equal -nocase blabla $nick] }

bind pubm - "*!afk*" afk
proc afk {nick host hand chan arg} {
 if {[bla $nick] || [blabla $nick]} {
  putquick "PRIVMSG $chan :$nick is AFK."
 }
}
User avatar
KrzychuG
Master
Posts: 306
Joined: Sat Aug 16, 2003 2:51 pm
Location: Torun, Poland
Contact:

Post by KrzychuG »

krimson wrote:if you used an argument when declaring the procs, you must use it when calling them too
]
Of course you're right. It was late, i was tired :)
Que?
User avatar
krimson
Halfop
Posts: 86
Joined: Wed Apr 19, 2006 8:12 am

Post by krimson »

that's no prob :P just wanted to make sure the info is recieved the right way ;)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Guess that's why one should never code when dead-tired (or quite intoxicated) :)
(learned that the hard way myself...)

Tho while we're at it, might aswell throw in something like this

Code: Select all

proc botaway {
 global nick
 return [expr {[string equal -nocase bla $nick] || [string equal -nocase blabla $nick]}]
}

proc afk {n u ho ha c t} {
 global nick
 if {[botaway]} {
  puthelp "PRIVMSG $c :${nick} is AFK"
 }
}
Anyway, what still puzzles me is this: is it the user typing !afk, or the bot, that we are supposed to test against those afk-nicks?
I've never known an eggdrop to be afk sorta.. but then again, why would an user need a bot to tell him/her wether (s)he's afk.. (and quite limited to two possible nicks)

But as it seems darton is'nt putting much more info into this thread, I guess we'll never know *shrug*
NML_375
User avatar
krimson
Halfop
Posts: 86
Joined: Wed Apr 19, 2006 8:12 am

Post by krimson »

here are some tips:

1. don't use global nick, you'd be better off using an argument for proc botaway instead

2. you don't have to use ${nick} there, you use that when you have any non-space char right after the var, and don't want it to get mixed up with something else
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

1. Depends on the purpose of the proc.. in this case, botaway would indicate it's this bot, hence requiring a nick to be supplied externally would be confusing. If the objective were to check a random nick, you'd be correct tho.
Considdering the ambiguos nature of darton's original code, I'd guess both are equally correct given it's interpretation of that code...

2. True. Just keep a habit of enclosing variable names with {} whenever it is adjacent to any non-whitespace character (Rather do it excessively, rather than forgetting it when really needed)
NML_375
Post Reply