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.

1st tcl script - text event trigger - help needed

Help for those learning Tcl or writing their own scripts.
Post Reply
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

1st tcl script - text event trigger - help needed

Post by jeffo »

hi all,

as u've seen in the topic it's my first tcl one (did mirc scripts already).

here's my code so far:

Code: Select all

bind pubm  o|* "!game" game

proc game {nick uhost hand chan rest}


{


 if {[string equal -nocase "!game" $text]} {random_int} 

proc random_int { } {
            global myrand
            set myrand [expr int(rand() * 7)]
            return $myrand
            }

{

#if     {$myrand == 0} {set $myrand i+1} //<-needed??????????

if     {$myrand == 1} {putserv "PRIVMSG #channel:$nick plays 1!"}
elseif {$myrand == 2} {putserv "PRIVMSG #channel:$nick plays 2!"}
elseif {$myrand == 3} {putserv "PRIVMSG #channel:$nick plays 3!"}
elseif {$myrand == 4} {putserv "PRIVMSG #channel:$nick plays 4!"}
elseif {$myrand == 5} {putserv "PRIVMSG #channel:$nick plays 5!"}
elseif {$myrand == 6} {putserv "PRIVMSG #channel:$nick plays 6!"}
elseif {$myrand == 7} {putserv "PRIVMSG #channel:$nick plays 7!"}


}


}

as u can see, it shall trigger an "!game" from ops and randomnize the output in a public message.

as it is my first tcl, i'm not sure if i got the syntax right, probably/most likely not. so help a newbie here pls.

thx in advance
t
thommey
Halfop
Posts: 76
Joined: Tue Apr 01, 2008 2:59 pm

Post by thommey »

  1. bind pubm doesn't work that way, reread the documentation about it.. the mask is matched against "#chan text", not against "text"
  2. $text isn't set in your proc, you called the last variable $rest
  3. defining a proc in a proc is almost always wrong, you wouldn't do that in mircscript either, are you sure you know mircscript?
  4. no need to "global myrand", a local variable is sufficient and better style if you store the return value of random_int
  5. your if control structure seems to be weird, it generates the id only if !game is equal to $text but the rest is evaluated in any case
  6. why not just .. "PRIVMSG #channel :$nick plays $myrand" instead of that spaghetticode?
  7. you violate fundamental Tcl syntax with your elseif statements and your bracing style (brace = {})
In the end, my advice is to read a tcl tutorial, here's a list:

Tcl tutorials: Eggdrop specific: I took the liberty of posting your "Tcl script" on a pastebin that has a static Tcl syntax checker and here you can see the result of that (the errors above the code): http://paste.tclhelp.net/?id=8p2
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

yea, will do. ty, was trying to get my mirc script into tcl and as i've said i've never worked with tcl scripts, so u are right. but calling it "spaghetticode" is veeeery nice of u :P
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

so...

Code: Select all

bind pubm  o|* "!game" game

proc game {nick host hand chan text}


{
 if {[string equal -nocase "#channel:!game" $text]} {random_int} 
#or add set var text here and return $text or can i leave it at all??
}

proc random_int { } 


{
 set myrand [expr int(rand() * 7)]
 return $myrand
}

{
#if     {$myrand == 0} {set $myrand i+1} //<-needed??????????

if     {$myrand == 1} {putserv "PRIVMSG #channel:$nick plays TXT1!"}
elseif {$myrand == 2} {putserv "PRIVMSG #channel:$nick plays TXT2!"}
elseif {$myrand == 3} {putserv "PRIVMSG #channel:$nick plays TXT3!"}
elseif {$myrand == 4} {putserv "PRIVMSG #channel:$nick plays TXT4!"}
elseif {$myrand == 5} {putserv "PRIVMSG #channel:$nick plays TXT5!"}
elseif {$myrand == 6} {putserv "PRIVMSG #channel:$nick plays TXT6!"}
elseif {$myrand == 7} {putserv "PRIVMSG #channel:$nick plays TXT7!"}
#endif ???

} 
...better?

and the if one is due to the output, it has to be a txt-msg instead of the no. out of the variable

and regarding the first error, i've read:

BIND [type] [Flags] [Event] [NameofProc]

edit:
thommey wrote:I took the liberty of posting your "Tcl script" on a pastebin that has a static Tcl syntax checker and here you can see the result of that (the errors above the code): http://paste.tclhelp.net/?id=8p2
i don't get this static tcl checker -> leaving most of the braces, which i thought were needed from what i've read, removes some errors. guess i have to think over the syntax once again
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

If you want to trigger a command upon !game then you will need 'bind pub' not 'bind pubm'. By using 'pub' instead the:

Code: Select all

if {[string equal -nocase "#channel:!game" $text]} {random_int} 
can be removed. Instead of a bunch of 'if' and 'elseif' you should use switch. In order to get a random number from 1 to 7 (both included) not from 0 to 6
you need to use [expr [rand 7] +1] to have. Also, the correct definition of a proc is:

Code: Select all

proc myproc {variables} {
# my stuff
}
Notice the { and }.
Anyway, here's a 'pub' code:

Code: Select all

bind pub  o|* !game pub:game

proc pub:game {nick uhost hand chan text} {
  switch -- [expr [rand 7] +1] {
    1 {
      putserv "PRIVMSG #channel :$nick plays TXT1!"
    }
    2 {
      putserv "PRIVMSG #channel :$nick plays TXT2!"
    }
    3 {
      putserv "PRIVMSG #channel :$nick plays TXT3!"
    }
    4 {
      putserv "PRIVMSG #channel :$nick plays TXT4!"
    }
    5 {
      putserv "PRIVMSG #channel :$nick plays TXT5!"
    }
    6 {
      putserv "PRIVMSG #channel :$nick plays TXT6!"
    }
    7 {
      putserv "PRIVMSG #channel :$nick plays TXT7!"
    }
  }
}
Once the game is over, the king and the pawn go back in the same box.
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

ty, will try it this way then. i thought the "rand()" is calling the function, but [rand 7] looks much more handy aswell as the "switch" cmd..

kudos
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

While in other coding languages it's rand() in TCL it's [rand]. Also, notice the [ and ] that translate in to calling the function of whatever you got inside.
Once the game is over, the king and the pawn go back in the same box.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

In core Tcl it is the mathamatical expression rand(), used together with the command expr to return a random floating point value between 0 and 1.

The command [rand <limit>] only exists in Eggdrop Tcl to return a random integer between 0 and <limit>-1

Hence if you are scripting for Eggdrop, either is allowed.
I must have had nothing to do
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

just another question for you pro's, would be cool if you could help me there. is it possible to set the trigger so that it will function (in this case) to not only "!game", but also to look in whole sentences like "why dont you type !game while we're at it"

i've tried:

bind pub -|- * !game game
bind pub -|- "* !game" game
bind pub -|- *!game* game
bind pub -|- "*!game*" game
bind pubm -|- "* !game" game
bind pubm -|- "% !game" game

and so on, but nothing works...

i.e. "*!game*" will trigger on *!game* only but not on "is this a !game"
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Wildcards such as * and % only works with the matching version of the binding (such as pubm for public channel chat and msgm for private messages). The proper binding would in this case be as follows:

Code: Select all

bind pubm -|- "*!game*" game
Edit: Fixed minor typo
NML_375
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

yea, ty, but this pubm is messing the script up somehow, talking about the http://www.egghelp.org/cgi-bin/tcl_arch ... ad&id=1689 by oldsoldier here, dunno what is wrong there
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I can't see why the binding I proposed would interfere with bender_quotes, as they don't overlap.

If you were to apply the same kind of binding to the bender_quotes script however, you would have to rewrite the pub_bender proc as "arg" would contain the whole line of text (including the !game trigger), rather than the text following the first word.

With the simple scripts proposed in this thread, that wouldn't be an issue, since they don't care of the content of "text", though.
NML_375
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

if i do pubm on it, it'll do

<..> <trigger>
-...- enter a number pitiful human! (notice)

the whole time, and only that

yea, trying to learn on the trigger thing, different script tho
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

So you were indeed applying the same kind of binding to the pub_bender proc. Thus, you'll either have to re-write pub_bender, or write a wrapper, to dig out the number from the whole line of text.
The simplest would probably be to write a wrapper, that actually don't bother with digging for the number, but use the random-line operation:

Code: Select all

bind pubm -|- "*!trigger" pubm_bender
proc pubm_bender {nick host handle channel text} {
  pub_bender $nick $host $handle $channel ""
}
NML_375
j
jeffo
Voice
Posts: 9
Joined: Wed Mar 02, 2011 9:17 pm

Post by jeffo »

ty, gonna try that
Post Reply