我正在使用一个名为 ns-2 的离散事件模拟器,它是使用 Tcl 和 C++ 构建的。我试图在 TCL 中编写一些代码:
set ns [new Simulator]
set state 0
$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 2.0 "puts \"At 2.0 values of state is: $state\""
proc changeVal {} {
global state
global ns
$ns at-now "set state [expr $state+1]"
puts "Changed value of state to $state"
}
$ns run
这是输出:
At 0.0 value of state is: 0
Changed value of state to 0
At 2.0 values of state is: 0
state 的值似乎没有改变。我不确定我在使用 TCL 时是否做错了什么。任何人都知道这里可能出了什么问题?
编辑:感谢您的帮助。实际上,ns-2 是我无法控制的东西(除非我重新编译模拟器本身)。我尝试了这些建议,这是输出:
对于代码:
set ns [new Simulator]
set state 0
$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""
proc changeVal {} {
global ns
set ::state [expr {$::state+1}]
$ns at-now "puts \"At [$ns now] changed value of state to $::state\""
}
$ns run
输出是:
At 0.0 value of state is: 0
At 1 changed value of state to 1
At 2.0 values of state is: 0
对于代码:
set ns [new Simulator]
set state 0
$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""
proc changeVal {} {
global ns
set ::state [expr {$::state+1}]
$ns at 1.0 {puts "At 1.0 values of state is: $::state"}
}
$ns run
输出是:
At 0.0 value of state is: 0
At 1.0 values of state is: 1
At 2.0 values of state is: 0
似乎不起作用...不确定是ns2还是我的代码有问题...