Home AMX User Forum NetLinx Studio

Use GET_LAST outside button events?

I'm using arrays of button numbers in BUTTON_EVENTs with GET_LAST(Button_Array) to pass indexes off to calls and functions. But there are a couple of places where I'd really like to use that same functionality inside a call or a function (i.e. I end up with the button number in a call but I need an index for it).

Am I stuck using a FOR loop to get this info when I can't rely on GET_LAST, or is there another function like "GET_INDEX" that I can't figure out? Can I do something inside a call to access the array like BUTTON_EVENT does, so that I can immediately use GET_LAST to retrieve the index?

Thanks,

Comments

  • NMarkRobertsNMarkRoberts Posts: 455
    While I try to avoid using button array indexes, because it's a trap for the next person to look at your code, here are two solutions.

    First, pass the index as well as the button number to your function.

    Second, put the index value into a global variable inside the event code.
  • From the NetLinx Keyword help:
    GET_LAST works with BUTTON_EVENTS and CHANNEL_EVENTS, but not with LEVEL_EVENTS

    GET_LAST() can not be used outside of Events
  • DHawthorneDHawthorne Posts: 4,584
    It will retain the last channel event that applies ... but you cannot trust it to be accurate outside of the button event handler. Once the running code passes outside of the handler, anything can change it that overlaps the device and/or channel you are using it on.

    My solution is to simply create a variable, and use GET_LAST within the event to assign a value to that variable. You can then pass this variable to any outside function you desire; since it's your own variable, you have full control over what can modify it and insure it's OK when you are actually using it.
  • From the NetLinx Keyword help:
    GET_LAST works with BUTTON_EVENTS and CHANNEL_EVENTS, but not with LEVEL_EVENTS

    GET_LAST() can not be used outside of Events

    altough my experience is contradictory to this.
    LEVEL_EVENT[dvTP,nLevels]
    	{
    	STACK_VAR INTEGER nColor
    	nColor=GET_LAST(nLevels)
    	nLevelValue[nColor]=LEVEL.VALUE
    	}
    

    works perfectly and I have been using for more than a year.

    but: make sure to use Levels 1-10 or somethin on each port, and if not try SET_VIRTUAL_LEVEL_COUNT to something
  • LEVEL_EVENT[dvTP,nLevels]...works perfectly and I have been using for more than a year.

    Very interesting! You're either lucky or I'll be able to replicate.
  • alexanboalexanbo Posts: 282
    Get_last just retreives an internal variable stored by the array referenced, kind of like the length. The variable gets set any time an event involving that array occurs so you have to be careful about what's accessing the arrays. I think, since you can't gurantee what get_last will return outside an event, that they tell you to not use it outside of events.

    I ran a test with this function:
    define_function getlasttest()
    {
    	send_string 0,"'TP:',itoa(get_last(dvTP))"
    }
    

    And it worked when called from a button event.
  • viningvining Posts: 4,368
    I think if you are using a Get_Last in a function that is only called by a Button or Channel Event then the function is in itself a continuation of the event and using a Get_Last there should not be a problem. But for the sake of proper coding ettiquatte it would be be a better technique to just to pass it the value.

    If that doesn't do it for you then just set a global var in the event and then call it where ever it's needed as previuously suggested.
Sign In or Register to comment.