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.

if no variable is set

Old posts that have not been replied to for several years.
Locked
c
collett9
Voice
Posts: 11
Joined: Mon Dec 15, 2003 11:38 pm

if no variable is set

Post by collett9 »

I have a script that sets a variable on command

i.e.

-set var1 hello

so that when someone says

!greet

the bot says "hello"

the reason I don't set one in my seperate .tcl file that holds all of the variables is because everytime it restarts it resets it to the default variable, so I am hoping someone can help me out on one of two solutions

1- if the variable is blank "" on startup, it returns "not set up"

2- how to save the variable once it is set so that it stays that way through rehashing and restarting

thanks in advanced
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

Code: Select all

# if myvar doesnt exist -> do something
if {![info exists myvar]} { .... }

# if myvar is empty -> do something
if {$myvar == ""} { ... }
to safe it permanently you have to write the var into a file and parse it every time you rehash/restart
c
collett9
Voice
Posts: 11
Joined: Mon Dec 15, 2003 11:38 pm

Post by collett9 »

GodOfSuicide wrote:to safe it permanently you have to write the var into a file and parse it every time you rehash/restart
how would I do that?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

proc saveVars {file args} {
	set f [open $file w]
	set i 0
	foreach var $args {
		upvar 1 $var [incr i]
		if {[array exists $i]} {
			puts $f [list array set $var [array get $i]]
		} elseif {[info exists $i]} {
			puts $f [list set $var [set $i]]
		}
	}
	close $f
}
proc loadVars file {uplevel 1 [list source $file]}
usage:
saveVars <your.file> <variable name(s)>
loadVars <your.file>
Have you ever read "The Manual"?
c
collett9
Voice
Posts: 11
Joined: Mon Dec 15, 2003 11:38 pm

Post by collett9 »

k, I kind of get where you are coming from, but let me go into greater detail to help YOU help ME even easier. :)

- my variables are in a file called vars.tcl (in the scripts folder)


Here is what the code looks like in my vars.tcl

Code: Select all

# set variable
set variwannaset "default set"

Here is how I have it laid out:

Code: Select all

proc saveVars {vars.tcl nick} { 
   set f [open $file w] 
   set i 0 
   foreach var $args { 
      upvar 1 $var [incr i] 
      if {[array exists $i]} { 
         puts $f [list array set $var [array get $i]] 
      } elseif {[info exists $i]} { 
         puts $f [list set $var [set $i]] 
      } 
   } 
   close $f 
}

proc loadVars file {uplevel 1 [list source scripts/$file]}
and here is the actual command:

Code: Select all

proc cmd:setvar {nick host handle chan text} { 
global privchan variwannaset
 foreach channel $privchan {
   if {$chan == "$privchan"} {
   loadVars vars.tcl
   set variwannaset "$text"
   putquick "NOTICE $nick :Variable has been set to: \002$variwannaset\002"
   saveVars vars.tcl variwannaset 
   }
 }
}
The above happens when someone types !setvar < newvariable >


Why is it not saving? :P
Locked