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.

Axel Kohonen avatar image Axel Kohonen commented ·

Hi @BenJoaquin Gouverneur

There is also a user command return type called numarray that you can use that accepts intarray and doublearray. However, using var as the return type is more flexible.

Axel

1 Like 1 ·
svmes.png (13.2 KiB)
BenJoaquin Gouverneur avatar image BenJoaquin Gouverneur Axel Kohonen commented ·

@Axel Kohonen and @Matt Long great info! Everything is working now. You guys are the best!

0 Likes 0 ·
Matt Long avatar image Matt Long commented ·

Prior to version 7.7 you had to return numbers from a user command and use the tonum() and tonode() commands in order to return a treenode. Starting in 7.7 you can return all types from a user command including arrays.

0 Likes 0 ·

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.

var theArray = param(1);
var removeVal = param(2);


int size = arraysize(theArray);
int index = 1;


switch (getvartype(theArray)) {
	case VAR_TYPE_TREENODEARRAY: {
		treenodearray oldArray = theArray;
		treenodearray newArray = makearray(size - 1);
		for (int i = 1; i <= size; i++) {
			if (oldArray[i] != removeVal) {
				newArray[index] = oldArray[i];
				index++;
			}
		}
		return newArray;
	}
	case VAR_TYPE_INTARRAY: {
		intarray oldArray = theArray;
		intarray newArray = makearray(size - 1);
		for (int i = 1; i <= size; i++) {
			if (oldArray[i] != removeVal) {
				newArray[index] = oldArray[i];
				index++;
			}
		}
		return newArray;
	}
}

And some example code of using it:

treenodearray testArray = makearray(3);
fillarray(testArray, node("Source1", model()), node("Source2", model()), node("Source3", model()));
return removeFromArray(testArray, node("Source2", model()));

intarray testArray = makearray(3);
fillarray(testArray, 1, 2, 3);
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.

BenJoaquin Gouverneur avatar image BenJoaquin Gouverneur commented ·

Thanks for the info @Matt Long. You're absolutely right, your code and mine both work. The two use cases we are planning for do return warnings, assigning the returned value to an array and using it in conjunction with the setlabel command. Both uses log the following to the Console:

"Warning Line 33 Invalid type for assignment operation on returnarray. Expecting type num. Type is none."

Can you elaborate on the how the type is "none"? As you point out both cases do work; is this a warning we should heed?

Thanks!

0 Likes 0 ·
Matt Long avatar image Matt Long BenJoaquin Gouverneur commented ·

If you copied my code exactly and pass in an array that isn't a treenodearray or intarray, it will get to the bottom of the code and receive no return value. I still don't get any warning if I do this, but you need to be sure to complete the code such that you always hit a return statement.

0 Likes 0 ·