question

chao.g avatar image
1 Like"
chao.g asked Ben Wilson edited

Defining global variables

Is there a way to define a global variable using code, and specify its parameters?

FlexSim 16.0.1
flexscriptcodeglobal variables
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

Ben Wilson avatar image
2 Likes"
Ben Wilson answered Ben Wilson edited

As @anthony.johnson noted:

I'm always hesitant to suggest digging into tree internals because it 
can cause compatibility issues if/when we change those tree internals. 
So, use at your own risk.

If you have an existing Global Variable, you can view the structure in your model tree under MODEL:/Tools/GlobalVariables. There you can browse the structure and see how they are set up.

To create another global variable programmatically, the easiest way would be to create a copy of an existing GV, then modify it, and finally call refreshglobalvariables() to register your new GV since it was added dynamically. Keep in mind that this command does have the side effect of resetting all your GVs to their initial value, so if you are trying to do this during a model run, its not a good idea.

Here is some example code to get you started:

treenode GVs = node("MODEL:/Tools/GlobalVariables");
treenode newGV = createcopy(first(GVs), GVs);

//change the name of the new Global Variable
setname(newGV, "myNewGV");
sets(first(newGV), "myNewGV");

//give the newGV a new data type. Try creating them using
// the normal GUI interface to see what all the types are
set(rank(newGV, 2), 1);

//set new data as the initial value of newGV. This is
// ALWAYS saved as a string, even if it is numeric data
sets(rank(newGV, 3), "4321");

//finally, refresh the global variables. This registers
// newGV as a GV so it can be used, but also resets all
// GVs back to their initial value
refreshglobalvariables();

//you can now use myNewGV as a normal Global Variable
5 |100000

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