Hi-
In the simple code below I'm working on to simulate an elevator I am having a little trouble with the constructor initializing. I get an error that says "cant read currentFloor...no such variable." I thought I had set up the constructor right. I tried following this format from the manual ---
Room instproc setCeilingColor color { my set ceilingColor $color }
Room instproc init args { my setCeilingColor white next }
Here is the code. It bombs out in the body of the method named request when it tries to read the first $currentFloor variable. I hope I am getting the idea and it is something kind of simple I am missing. Please offer a suggestion if what I am doing wrong is apparent to you.
thank you, marvin
package require XOTcl; namespace import -force xotcl::*
Class Elevator
Elevator instproc setInitFloor floor { my set currentFloor $floor ; }
Elevator instproc init args { my set setInitFloor 1; next; }
Elevator instproc getFloor args { puts $currentFloor;
}
Elevator instproc request requestFloor {
my set request $requestFloor;
while {$requestFloor > 0} {
if {$currentFloor < $requestFloor} { set currentFloor [[Ride getFloor] + 1]; puts "going up...current floor is $currentFloor"; } if {$currentFloor > $requestFloor} { set currentFloor [[Ride getFloor] - 1]; }
if {$currentFloor == $requestFloor} { set requestFloor 0;
} }
puts "current floor is $currentFloor"; }
set requestFloor 4;
Elevator Ride;
Ride request $requestFloor;
Ride destroy ;
Hi marvin, in order to get an instance variable into the scope of an instproc, you have to import it via the method instvar
Below is a simplified version of you script.
best regards -gustaf ============================================== package require XOTcl; namespace import -force xotcl::*
Class Elevator -parameter {{currentFloor 1}}
Elevator instproc request requestFloor { my instvar currentFloor
while {1} { if {$currentFloor < $requestFloor} { incr currentFloor puts "going up...current floor is $currentFloor"; } if {$currentFloor > $requestFloor} { incr currentFloor -1 puts "going down...current floor is $currentFloor"; } if {$currentFloor == $requestFloor} { break } } puts "current floor is $currentFloor"; }
set requestFloor 4;
Elevator Ride; Ride request $requestFloor; Ride destroy ; ==============================================