| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (6 out of 25)

Sample Procedure

The following sample shows a complete procedure. This procedure, create-and-move-robot, creates a robot object, transfers it to a workspace, and slowly moves it to a particular place on the workspace. This place is represented by the destination argument, which is defined in the procedure header. The newly created robot will move until it reaches whatever is displayed at that point.

create-and-move-robot (destination: class item) 

myrobot: class robot;
new-x: quantity = 0;
new-y: quantity = 0;

begin
create a robot myrobot;
transfer myrobot to the workspace of destination;
repeat
exit if
(the icon-x-position of myrobot = the icon-x-position of destination and
the icon-y-position of myrobot = the icon-y-position of destination);
if the icon-x-position of destination > the icon-x-position of myrobot
then new-x = the icon-x-position of myrobot + 1
else
if the icon-x-position of destination < the icon-x-position of myrobot
then new-x = the icon-x-position of myrobot - 1;
if the icon-y-position of destination > the icon-y-position of myrobot
then new-y = the icon-y-position of myrobot + 1
else
if the icon-y-position of destination < the icon-y-position of myrobot
then new-y = the icon-y-position of myrobot - 1;
move myrobot to (new-x, new-y);
wait for 1 second;
end;
end
This procedure uses four local names: destination, myrobot, new-x, and new-y. Destination is declared in the procedure header, as described under Procedure Header Syntax. The other three (myrobot, new-x, and new-y) are used only within the procedure body and are declared in the local declarations part of the definition, as described under Local Declarations Syntax.

This procedure first uses a create action to create a new instance of the class robot called myrobot. It places myrobot on whatever workspace the destination object is displayed, using a transfer action.

Note that this procedure includes references to the icon-x-position of destination and the workspace of destination, where destination is an item that is passed to the procedure as an argument.

If you want to change the value of a variable from within a procedure, you must use a conclude action, as described under conclude. You can also use a set action to set the value of a variable in the G2 Simulator or an external data server, as described under set.

The G2 Simulator is a superseded capability. For more information, see Appendix F, Superseded Practices.

You can use implicitly declared local names in procedure statements just as you can in expressions generally. For details see Using Local Names in Expressions.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (6 out of 25)

Copyright © 1997 Gensym Corporation, Inc.