Home AMX User Forum AMX General Discussion

Getting my head around multi-dimensional arrays

All:

So I'm working with two-dimensional arrays and I think I've got a good handle on the correct syntax. Just checking with the forum to make sure.

Ex.
Camera1Position [2][4] =  //two rows, four columns
{
  {4500,250,49,1523},  //row 1 - focus, pan, tilt, zoom
  {4500,358,66,1523} 
}

To implement I think my code should look like:
SEND_COMMAND camera1, "'Focus = ',Camera1Position[1][1],'Pan=',Camera1Position[1][2],'Tilt=',Camera1Position[1][3],'Zoom=',Camera1Position[1][4]"

Does that look close?

Thanks,
Norm

Comments

  • ericmedleyericmedley Posts: 4,177
    Norm,
    Yep, you're getting it. You could rewrite the command like this into a funciton.
    define_function fn_Send_My_Cam_Command(integer cam_id){
    	send_command camera1, 	" 'Focus = ',Camera1Position[cam_id][1],
                                                      'Pan= ',Camera1Position[cam_id][2],
                                                      'Tilt= ',Camera1Position[cam_id][3],
                                                    'Zoom= ',Camera1Position[cam_id][4] "
    	} // define_function
    

    On a side note: If your intention is to have the text look like this:
    "Focus=4500Pan=250 etc..."
    You will need to put the variables into an ITOA() function "Integer To ASCII"

    otherwise you're going to get some goofy resuls since it will send a literal 4500 decimal.

    So it will look like this:
    define_function fn_Send_My_Cam_Command(integer cam_id){
      send_command camera1, 	" 'Focus = ',itoa(Camera1Position[cam_id][1]),
                                            'Pan = ',itoa(Camera1Position[cam_id][2]),
                                            'Tilt = ',itoa(Camera1Position[cam_id][3]),
                                             'Zoom = ',itoa(Camera1Position[cam_id][4] )"
    	} // define_function
    

    and for more fun. to prevent the function from accidntally prducing an error you could put a couple traps to prevent bad indexes. In your example you have 2 cameras. So if you accidentally sent the cam_id as 3, it would produce an error (index too large) since you don't have a 3rd row in your array. Also if you accidntally sent cam_id as zero you'd get a zero index error. So put in these traps.
    define_function fn_Send_My_Cam_Command(integer cam_id){
      if(cam_id){ // cam_id must be greater than zero
        If(cam_id<3){ // cam_id must be less than 3
          send_command camera1, 	" 'Focus = ',itoa(Camera1Position[cam_id][1]),
                                            'Pan = ',itoa(Camera1Position[cam_id][2]),
                                            'Tilt = ',itoa(Camera1Position[cam_id][3]),
                                             'Zoom = ',itoa(Camera1Position[cam_id][4] )"
          } // if <3
        } // if not zero
      } // define_function
    

    okay - enough fun for now.
    e
Sign In or Register to comment.