question

Joerg Vogel avatar image
1 Like"
Joerg Vogel asked Mischa Spelt answered

Is the scope of current from a user command the calling object?

@Michael asked the question:

When calling a user command from an object's trigger's custom code, does the "current" in the user command scope still reference the original object that was triggered? Or should we pass in a reference to the object as an argument to the user command?

FlexSim 16.1.2
itemtypeitemuser commandcurrent
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Mischa Spelt avatar image
2 Likes"
Mischa Spelt answered

And here is the technical answer, in case anyone wants the details :-)

If you open up the code of any trigger, you will see that current is defined as

Object current = ownerobject(c);

The c variable is like a "magic" variable that always* points at the currently executing code. For the entry trigger of a queue for example, this is the entrytrigger node in the Queue's variables section, e.g. /Queue1>variables/entrytrigger. The ownerobject function basically strips of the 'go into the variables part' after the > and gives you the queue object back.

In a user command, you can also access the c variable, and it will point at the place where the User Command code is stored, e.g. /Tools/UserCommands/TestCommand/code. It doesn't matter whether you call the user command from a Script Window, User Event or other trigger, c will always be the user command's code node. So even if you copied the line that determines current literally to your user command, you would not get the expected result:

// c is /Tools/UserCommands/TestCommand/code
Object current = ownerobject(c);
// current is now... MAIN:/project/model !

As @steven.hamoen already answered, the way around this is by passing the current from the object trigger as an argument into your usercommand,

// In the on entry trigger
Object item = param(1);
Object current = ownerobject(c);
int port = param(2);
TestCommand(current);

and then getting it out in the user command by

// In the user command
Object current = param(1);

* Actually there are some subtleties, such as when executing event functions.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Steven Hamoen avatar image
2 Likes"
Steven Hamoen answered

@Michael, @Jörg Vogel current is not in scope in a usercommand. current points to the object the code is written on but a usercommand is not defined on an object and so current doesn't exist. Is also easy to check by creating a usercommand and do anything with current and use the debugger to see the value if you step through the code.

If you want to use current you have to pass it in as an argument and get it out with param() in the usercommand.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.