question

Steven Chen avatar image
0 Likes"
Steven Chen asked Jordan Johnson answered

Incorrect Value of Token in Module SDK

Hello,

Token passed as parameter on calling dll function. But the token in dll seems not correct.

visible void dllprinttokenid(FLEXSIMINTERFACE)
{
    ProcessFlow::Token token = param(1);
    print("tokenid=" + numtostring(token.tokenID));// print tokenid=0
}
FlexSim 22.0.1
processflowmodule sdk
5 |100000

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

1 Answer

Jordan Johnson avatar image
1 Like"
Jordan Johnson answered

The param(n) macros return a Variant value. A variant holds a treenode. When you pass a Token in to a command, it gets cast as a Variant, which means the Variant is set to the node that has the token data on it.

So to get a token from a parameter, you'd need code like this:

TreeNode* tokenNode = param(1);
if (tokenNode && isclasstype(tokenNode, "ProcessFlow::Token")) {
    ProcessFlow::Token* token = tokenNode->object<ProcessFlow::Token>();
    print("tokenid=" + numtostring(token->tokenID));
}

You can see that there are a few steps:

  1. Cast the Variant as a TreeNode* (or treenode, which is a typedef for a TreeNode*)
  2. Check that the node exists, and check that it actually has Token data on it. You could add an else statement to throw an exception if something other than a token node is passed in. Or you could not check at all, and that the user always passed in a node, and that the node always had token data.
  3. Get the data the node holds, and cast that data as a Token* (not a Token).
  4. Use the token how you would like

It might be a little confusing, because FlexScript hides the fact that treenode and Token are both pointer types. FlexScript also lets you cast from a node to a Token. This is valid in FlexScript:

Token t = someNode;

But as you can see, FlexScript is hiding lots of details for you. This is nice for most users, but when it comes time to write C++ code, it can be confusing.

5 |100000

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