question

BenJoaquin Gouverneur avatar image
1 Like"
BenJoaquin Gouverneur asked BenJoaquin Gouverneur commented

How to return array type from User Command

Hi folks,

My colleague @Will Bishop and I are building out a collection of custom User Commands to support our work. We have several commands related to interacting with global tables and are working on commands to make working with arrays easier.

I'd like to build a command for removing a matched element from an array and a separate one for adding an element to a given index location. To do both of these I need the command to return the various array types. I'm able to pass arrays into commands but get a type error if I try to assign an array returned from a command. Is there a way to do this or is this an inherent limitation in FlexScript?

FlexSim 16.2.0
flexscriptarrayuser command
· 3
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

Matt Long avatar image
1 Like"
Matt Long answered Matt Long commented

Without seeing your code I can't tell you exactly what you're doing wrong, but here's a simple user command that removes a value from a couple different types of arrays. Keep in mind this command is not very robust as it doesn't do any kind of dummy checking, but you should get the idea.

  1. var theArray = param(1);
  2. var removeVal = param(2);
  3.  
  4.  
  5. int size = arraysize(theArray);
  6. int index = 1;
  7.  
  8.  
  9. switch (getvartype(theArray)) {
  10. case VAR_TYPE_TREENODEARRAY: {
  11. treenodearray oldArray = theArray;
  12. treenodearray newArray = makearray(size - 1);
  13. for (int i = 1; i <= size; i++) {
  14. if (oldArray[i] != removeVal) {
  15. newArray[index] = oldArray[i];
  16. index++;
  17. }
  18. }
  19. return newArray;
  20. }
  21. case VAR_TYPE_INTARRAY: {
  22. intarray oldArray = theArray;
  23. intarray newArray = makearray(size - 1);
  24. for (int i = 1; i <= size; i++) {
  25. if (oldArray[i] != removeVal) {
  26. newArray[index] = oldArray[i];
  27. index++;
  28. }
  29. }
  30. return newArray;
  31. }
  32. }

And some example code of using it:

  1. treenodearray testArray = makearray(3);
  2. fillarray(testArray, node("Source1", model()), node("Source2", model()), node("Source3", model()));
  3. return removeFromArray(testArray, node("Source2", model()));
  4.  
  5. intarray testArray = makearray(3);
  6. fillarray(testArray, 1, 2, 3);
  7. return removeFromArray(testArray, 2);

(As a side not, you can look forward to FlexSim 2017 where dealing with arrays will become quite a bit easier...)


removefromarray.png (32.1 KiB)
· 2
5 |100000

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