Home AMX User Forum NetLinx Studio

ON/OFF with Variables

What do ON or OFF do to a variable if the variable is an array? I need to set all elements of an array to zero in response to a button event. Would something like ARRAY ="0,0,0,0" be the best way to do this in a single line?

Comments

  • ericmedleyericmedley Posts: 4,177
    bgrebner wrote:
    What do ON or OFF do to a variable if the variable is an array? I need to set all elements of an array to zero in response to a button event. Would something like ARRAY ="0,0,0,0" be the best way to do this in a single line?

    I must not know or remember a simple way to do this I tried ARRAY[]={0,0,0,0,0} and a multitude of other things. Perhaps someone else knows the stupid-simple way. Failing that...

    you could do several things.

    grunt-n-groan method...
    BUTTON_EVENT[tp_1,1]
    {
    push:
      {  
      ARRAY[1]=0
      ARRAY[2]=0
      ARRAY[3]=0
      ETC...
      }
    }
    

    or you could put the thing in a for loop if it's a big array.
    FOR(LOOP=1;LOOP<101;LOOP++)
      {
      ARRAY[LOOP]=0
      }
    

    There's several ways to skin that cat. Hopefully someone will chime in with the easy ARRAY= whatever it is to get it done...
  • NMarkRobertsNMarkRoberts Posts: 455
    Standard routine

    Every little problem like this is an opportunity to create a standard routine:
    (******************************************************************************)
    (* Fill an integer array with a value                                         *)
    define_function FillIntegerArray       (
      integer nArgArray[]                  , (* Array to fill                     *)
      integer nArgValue                    , (* Value to fill it with             *)
    	integer nArgLength                   ) (* If zero, whole array              *)
    (******************************************************************************)
    {
    stack_var long    nMyLen
    stack_var integer nMyPos
    
    if (nArgLength <> 0)
      {
    	nMyLen                                         = nArglength
    	}
    else
      {
      nMyLen                                         = max_length_array(nArgArray)
    	}
    
    for (nMyPos = 1; nMyPos <= nMyLen; nMyPos++)
      {
      nArgArray[nMyPos]                              = nArgValue
      }
    } (* FillIntegerArray *)
    
  • mpullinmpullin Posts: 949
    Once I had a program with a bunch of arrays of size 10 (there were 10 rooms). I made a variable called Ten_Ones[10] = {1,1,1,1,1,1,1,1,1,1} and Ten_Zeroes[10] = {0,0,0,0,0,0,0,0,0,0}. Then I could just do this_array = Ten_Zeroes; to "turn off" the array.

    This solution feels a little clumsy and is a waste of memory but it works well.
  • NMarkRobertsNMarkRoberts Posts: 455
    mpullin wrote:
    Once I had a program with a bunch of arrays of size 10 (there were 10 rooms). I made a variable called Ten_Ones[10] = {1,1,1,1,1,1,1,1,1,1} and Ten_Zeroes[10] = {0,0,0,0,0,0,0,0,0,0}. Then I could just do this_array = Ten_Zeroes; to "turn off" the array.

    This solution feels a little clumsy and is a waste of memory but it works well.

    Your method uses less memory, less code and less processor than mine, so it's not "wasteful" at all. Sometimes solutions like that are the best approach.

    But of course your solution isn't generalised in application so "clumsy" is OK by me 8^)
Sign In or Register to comment.