egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

global variables don't work
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Mon Aug 07, 2006 5:15 pm    Post subject: global variables don't work Reply with quote

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:
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."
 }
}
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Aug 07, 2006 5:26 pm    Post subject: Reply with quote

Well, what confuses me, if why you've put those two string equal-checks outside the proc...
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Mon Aug 07, 2006 5:27 pm    Post subject: Reply with quote

because I have many procs in one tcl-file and I must use these variables in almost every proc.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Aug 07, 2006 5:30 pm    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Mon Aug 07, 2006 5:38 pm    Post subject: Reply with quote

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]}"
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Aug 07, 2006 5:43 pm    Post subject: Reply with quote

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:

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
krimson
Halfop


Joined: 19 Apr 2006
Posts: 86

PostPosted: Tue Aug 08, 2006 3:23 am    Post subject: Reply with quote

@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
Back to top
View user's profile Send private message Send e-mail
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Aug 08, 2006 8:54 am    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
KrzychuG
Master


Joined: 16 Aug 2003
Posts: 306
Location: Torun, Poland

PostPosted: Tue Aug 08, 2006 2:59 pm    Post subject: Reply with quote

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

Code:

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?
Back to top
View user's profile Send private message Visit poster's website
krimson
Halfop


Joined: 19 Apr 2006
Posts: 86

PostPosted: Wed Aug 09, 2006 4:21 am    Post subject: Reply with quote

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

Code:
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."
 }
}
Back to top
View user's profile Send private message Send e-mail
KrzychuG
Master


Joined: 16 Aug 2003
Posts: 306
Location: Torun, Poland

PostPosted: Wed Aug 09, 2006 3:11 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website
krimson
Halfop


Joined: 19 Apr 2006
Posts: 86

PostPosted: Wed Aug 09, 2006 3:38 pm    Post subject: Reply with quote

that's no prob :P just wanted to make sure the info is recieved the right way ;)
Back to top
View user's profile Send private message Send e-mail
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Aug 09, 2006 3:40 pm    Post subject: Reply with quote

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

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

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
krimson
Halfop


Joined: 19 Apr 2006
Posts: 86

PostPosted: Wed Aug 09, 2006 3:55 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Aug 09, 2006 4:09 pm    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber