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 

Rand and if's

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
wikked
Voice


Joined: 24 May 2014
Posts: 1

PostPosted: Sat May 24, 2014 4:06 pm    Post subject: Rand and if's Reply with quote

I'm new to TCL, and I need a lil help with rand and if's.
Code:
proc fight_kick { nick host hand chan text } {
     set $rfu rand(1,6)
     if { $rfu == 1 } { putmsg $chan "!fu" }
     if { $rfu == 2 } { putmsg $chan "!fd" }
     if { $rfu == 3 } { putmsg $chan "!fg" }
     if { $rfu == 4 } { putmsg $chan "!fk" }
     if { $rfu == 5 } { putmsg $chan "!fp" }
     if { $rfu == 6 } { putmsg $chan "!fj" }
}
Back to top
View user's profile Send private message
Madalin
Master


Joined: 24 Jun 2005
Posts: 310
Location: Constanta, Romania

PostPosted: Sat May 24, 2014 4:46 pm    Post subject: Re: I'm new to TCL, and I need a lil help with rand and if's Reply with quote

Try this

Code:
proc fight_kick { nick host hand chan text } {
     set rfu [rand 6]
     if { $rfu == 1 } { putmsg $chan "!fu" }
     if { $rfu == 2 } { putmsg $chan "!fd" }
     if { $rfu == 3 } { putmsg $chan "!fg" }
     if { $rfu == 4 } { putmsg $chan "!fk" }
     if { $rfu == 5 } { putmsg $chan "!fp" }
     if { $rfu == 6 } { putmsg $chan "!fj" }
}

_________________
https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Mon May 26, 2014 1:10 am    Post subject: Reply with quote

Since [rand 6] will return results from 0 to 5 you need to either add 1 to it like [expr [rand 6] +1] to have both included or change the if lines to start with 0.

Anyway, I would honestly go with a switch than a bunch of if statements cos the interpreter will pass through them all, while the switch will go directly to the corresponding number and exit.
Code:

proc fight_kick {nick host hand chan text} {
   set rfu [expr [rand 6] +1]
   switch -- $rfu {
      1 {
         set msg "!fu"
      }
      2 {
         set msg "!fd"
      }
      3 {
         set msg "!fg"
      }
      4 {
         set msg "!fk"
      }
      5 {
         set msg "!fp"
      }
      6 {
         set msg "!fj"
      }
   }
   putmsg $chan $msg
}

_________________
Once the game is over, the king and the pawn go back in the same box.
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
Page 1 of 1

 
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