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.

Variables

Old posts that have not been replied to for several years.
Locked
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Variables

Post by Jag »

why when i closing the eggdrop, and reopen it the eggdrop clears all the variables? :(
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Because saving them would make little sense in most cases. If you want a value stored you'll have to do it yourself :)
Have you ever read "The Manual"?
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

:(
10x any way :P
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Using arrays will save them in memory ONLY if you restart the eggdrop, not kill it. To save an varialbe you must save it to disk as user said.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:Using arrays will save them in memory ONLY if you restart the eggdrop, not kill it.
A restart makes a fresh tcl interpreter (so everything is lost). I think you meant 'rehash' :)
Have you ever read "The Manual"?
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

to backup an array:

Code: Select all

proc write_array {name file} { 
  set fp [open $file w] 
  puts $fp [uplevel 1 [list array get $name]] 
  close $fp 
} 
to restore the backup:

Code: Select all

proc read_array {name file} { 
  set fp [open $file r] 
  uplevel 1 [list array set $name [read -nonewline $fp]] 
  close $fp 
} 
credits for this code go to ppslim, slenox and all the others in the "backup array" thread :P
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

GodOfSuicide wrote:credits for this code go to ppslim, slenox and all the others in the "backup array" thread :P
An alternative way would be to write it to the file as tcl code creating the array and just 'source the.file' (making the loading a bit faster I imagine) Where's that thread btw?

Edit: here's some generic code for variable storage:

Code: Select all

# usage: var2str <qualified name> ?output name? ?pretty array boolean?
# the pretty array option is to choose between 'array set' or multiple 'set's for arrays with one or more elements
proc var2str {var {name {}} {prettyArray 0}} {
	if {$name=={}} {set name $var}
	if {[set var [uplevel 1 [list namespace which -variable $var]]]==""} {
		error "No such variable "$name""
	}
	upvar 1 $var v
	if {[info exists v]} {
		if {[array exists v]} {
			if {$prettyArray&&[array size v]} {
				foreach {ele val} [array get v] {
					lappend out [list set ${name}($ele) $val]
				}
				join [lsort -dict $out] \n
			} {
				list array set $name [array get v]
			}
		} {
			list set $name $v
		}
	} {
		list variable $name
	}
}
# usage: storeVar <variable name> <file name> ?file access mode?
proc storeVar {var file {mode w}} {
	set f [open $file $mode]
	puts $f [var2str $var $var 1]
	close $f
}
Have you ever read "The Manual"?
J
Jag
Halfop
Posts: 90
Joined: Fri Sep 19, 2003 10:06 am

Post by Jag »

10x a lot!!!! :P
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

user wrote:Where's that thread btw?
http://forum.egghelp.org/viewtopic.php? ... writearray
Locked