Home AMX User Forum NetLinx Studio

inline (non-pre-defined) array?

I'm kind of a newbie with only Programming 1 under my belt...

Can an array be passed as a parameter without first defining the array? I call this in-line. The way I am trying to do it does not work.

Example 1:

DEFINE_CALL 'MakeTies' (integer indexSource, integer indexDisplays[])
{
  stack_var integer i
  stack_var char cmd[64]
  cmd = ''
  for (i=1;i<=length_array(indexDisplays);i++) {
    // add indexDisplays[i] to the list of outputs on the switch command
    cmd = "cmd,' ',itoa(indexDisplays[i])"
  }
  cmd = "'CI',itoa(indexSource),'O',cmd,'T'"  // command format is CI<input>O<outputs>T
  // add cmd to the switches outgoing queue to be processed by a timeline
}

To put source 1 on displays 1, 2, 3 and 6, and put source 2 on displays 4 and 5, have the button code something like:

PUSH: {
  CALL 'MakeTies' ( 1 , { 1, 2, 3, 6 } )
  CALL 'MakeTies' ( 2 , { 4, 5 } )
}

Example 2:

TIMELINE_CREATE (TL_POLL, { 1000 }, 1, TIMELINE_RELATIVE, TIMELINE_REPEAT)

vs

DEFINE_VARIABLE
long TL_POLL_Times[1] = { 1000 }

DEFINE_START
TIMELINE_CREATE (TL_POLL, TL_POLL_Times, 1, TIMELINE_RELATIVE, TIMELINE_REPEAT)


My use case is the first example. I have room preset / macro buttons, and some presets have a source going to as many as 8 displays.
I'd really like to take advantage of the serial-controlled AMX switcher's ability to switch an input to multiple outputs with a single command (CI1O1 2 3 6T) rather than taking a few seconds processing a queue of multiple single switches.

I've tried to create a stack_var integer Displays[4] { 1, 2, 3, 6 } before the call but initializing an array is not allowed outside of DEFINE_VARIABLE
I could create an array and populate it one by one, but I'd rather not. example:

PUSH: {
  stack_var integer displays[4]
  displays[1] = 1
  displays[2] = 2
  displays[3] = 3
  displays[4] = 6
  CALL 'MakeTies' ( 1 , displays )
  displays[1] = 4
  displays[2] = 5
  SET_LENGTH_ARRAY(displays, 2) // since I'm not using the full capacity of the array
  CALL 'MakeTies' ( 2 , displays )
}

Is passing an array in-line possible in Netlinx?

Answers

  • sentry07sentry07 Posts: 77

    The simple answer is no, you can't pass raw integer arrays to functions.

    The complicated answer is yes, but not in the form you're thinking. One way of handling it could be to pass the array in as a delimited string (flat array) like CALL 'MakeTies' (2, '1,2,3,6') and then parse the string inside MakeTies.

    For the second example I have a function you can use that handles that:

    // ID;  Timeline ID - Constant
    // lTime; time in 1/1000th seconds
    // lRepeat; TRUE = repeat, FALSE = run once
    DEFINE_FUNCTION INTEGER CreateTimeline (LONG ID, LONG lTime, LONG lRepeat)
    {
        LONG arrDelay[1], lDelayTime
        lDelayTime  = TYPE_CAST( lTime )
        arrDelay[1] = lDelayTime
        IF(lRepeat)
        TIMELINE_CREATE(ID,arrDelay,1,TIMELINE_ABSOLUTE,TIMELINE_REPEAT)
        ELSE
        TIMELINE_CREATE(ID,arrDelay,1,TIMELINE_ABSOLUTE,TIMELINE_ONCE)
    }
    
  • mushmush Posts: 287
    edited July 2019

    Firstly, you should just use DEFINE_FUNCTION. Functions can be used inline and return values.
    How's this?
    DEFINE_FUNCTION char[24] fnMakeTies (integer indexSource, char indexDisplays[])
    {
    stack_var char cmd[64]
    cmd = "'CI',itoa(indexSource),'O',indexDisplays,'T'" // command format is CIOT
    // add cmd to the switches outgoing queue to be processed by a timeline
    }

    PUSH: {
    fnSwitchQueue(fnMakeTies( 1 , '1 2 3 6' ))
    fnSwitchQueue(fnMakeTies( 2 , '4 5' ))
    }

  • ericmedleyericmedley Posts: 4,177
    edited August 2019

    Interesting. I don't even use the Call anymore. For me Call is just a function with no passthrough. And, as far as passing in variables to a function - you can pass nearly usable variable type into a function. And this last bit of advice is technically lazy programming - but it is true as well... Since Netlinx is technically a single-threaded runtime environment; you can use global variables to pass stuff into a function and can be reasonably sure you won't crash the values as long as you remember to set the globals immediately prior to calling the function. Here again - this is not recommended in any multi-threaded environment. but, it does work in Netlinx. (I don't do this myself)

Sign In or Register to comment.