| View previous topic :: View next topic |
| Author |
Message |
Jag Halfop
Joined: 19 Sep 2003 Posts: 90
|
Posted: Wed Oct 08, 2003 1:43 am Post subject: Variables |
|
|
why when i closing the eggdrop, and reopen it the eggdrop clears all the variables?  |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Wed Oct 08, 2003 1:30 pm Post subject: |
|
|
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"? |
|
| Back to top |
|
 |
Jag Halfop
Joined: 19 Sep 2003 Posts: 90
|
Posted: Wed Oct 08, 2003 1:50 pm Post subject: |
|
|
10x any way  |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Wed Oct 08, 2003 2:04 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Wed Oct 08, 2003 2:22 pm Post subject: |
|
|
| 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"? |
|
| Back to top |
|
 |
GodOfSuicide Master

Joined: 17 Jun 2002 Posts: 463 Location: Austria
|
Posted: Wed Oct 08, 2003 4:39 pm Post subject: |
|
|
to backup an array:
| Code: |
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: |
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  |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Wed Oct 08, 2003 7:33 pm Post subject: |
|
|
| GodOfSuicide wrote: | credits for this code go to ppslim, slenox and all the others in the "backup array" thread  |
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: | # 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"? |
|
| Back to top |
|
 |
Jag Halfop
Joined: 19 Sep 2003 Posts: 90
|
Posted: Thu Oct 09, 2003 1:40 am Post subject: |
|
|
10x a lot!!!!  |
|
| Back to top |
|
 |
GodOfSuicide Master

Joined: 17 Jun 2002 Posts: 463 Location: Austria
|
|
| Back to top |
|
 |
|