ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
|
Downloads |
Q&A Using Flexsim and building models |
#1
|
||||
|
||||
Is it possible to create a #define in the object (not model) scope
Ladies and Gentlemen,
I am trying to bring some order to a massive and ugly piece of code in the OnMessage trigger which presently looks like this: Code:
#define MSGCODE1 1 ... #define MSGCODEn <n> int msgCode = msgparam(1); if (msgCode == MSGCODE1) { ... insertproxytask(..., TASKTYPE_CALLSUBTASK, current, NULL, MSGCODE2, ...); ... dispatchcoordinatedtasksequence(...); } else if (msgCode == MSGCODE2) { ... insertproxytask(..., TASKTYPE_CALLSUBTASK, current, NULL, MSGCODE3, ...); ... return cts; } else if (...) ... I intend to refactor this code by removing every case into a separate nodefunction and only keep the skeleton in the trigger itself. The problem, however, is that I want to keep the #defined values only within the scope of the object handling the messages. Under different circumstances I would have defined these values globally, under "global macros", but the goal of this project is to create a completely self-contained reusable object, so it has to encapsulate any constants it happen to require. I would also prefer to avoid #defining the message codes twice, once in the nodefunction and once in the OnMessage trigger. Herein lies my question, is there a way to work around this problem, something that would mimic the behavior of the header file in C++ which can be implemented once and included in several places? Thank you! Vadim |
#2
|
||||
|
||||
At the first glance this looks impossible to do using flexscript and #defines
:-(, not elegantly, anyway. I guess it is possible to create a new node at runtime, copy the #defines from a text node, copy the code from another text node, compile flexscript and call a nodefunction from a nodefunction, but it sounds a bit Rube-Goldberg-esque. Without knowing how buildnodeflexscript() is implemented, I cannot say if my musings below make any sense or not, but I think it should not be difficult implement something like this: NOTE THAT THE FUNCTIONALITY DESCRIBED BELOW DOES NOT EXIST! I am just hypothesising what it may look like. includenodeflexscript(node codenode) that during execution of the command buildnodeflexscript() will check that it is not called recursively and copy the content of the codenode into the input of the buildnodeflexscript(). The trick is in ensuring that the node calling this function gets rebuilt if the included node changes, but does not get rebuilt every time the function is called, but if that can be addressed, the rest seems simple. For example, let's say in my object, I create a text node "/MyObject>includes/constants" containing the following: Code:
#define ONE 1 #define TWO 2 Code:
includenodeflexscript(node(">includes/constants", current); ... treenode cts = createcoordinatedtasksequence(...); ... insertproxytask(cts,..., TASKTYPE_CALLSUBTASK, current, NULL, TWO,...); ... dispatchcoordinatedtasksequence(cts); Code:
includenodeflexscript(node(">includes/constants", current); int msgCode = msgparam(1); if (msgCode == ONE) { nodefunction(node(">methods/HandleMsgOne", current),...); } else if (msgCode == TWO) { ... } IMHO, it would allow to make the code so much tidier, more object-oriented, and easier to reuse. Last edited by Vadim Fooks; 09-26-2012 at 08:16 PM. |
#3
|
||||
|
||||
Have you taken a look at the modellibraries node option in the helpfile topic Creating Custom Libraries?
That way you could per object create a link to the #defines you need. How to create these nodes depends on how you want to distribute your objects. If you create a userlibrary to load your objects it's quite easy to to use the loadinstall node and newmodel node to install these nodes. Other option is of course to call a dll function from your message trigger and that way write all the complex code nicely in visual studio. |
The Following 2 Users Say Thank You to Steven Hamoen For This Useful Post: | ||
Vadim Fooks (09-27-2012) |
Thread | Thread Starter | Forum | Replies | Last Post |
Object GUI from Model GUI | shashanktrivedi | Q&A | 1 | 06-26-2011 04:28 AM |
create an object from UserLibrary1 | Nischith Kashyap | Q&A | 1 | 04-07-2011 01:03 AM |
Simulati report for model with Fluid object | David Chan | Q&A | 1 | 07-26-2008 03:42 AM |
is there a command opposite to "destroyobject" which create a object? | KelvinHo | Q&A | 3 | 07-03-2008 08:09 AM |
Controlling the camera view to be focused on a moving object in your model. | Regan Blackett | Tips and Tricks | 0 | 08-03-2007 10:12 AM |