Home AMX User Forum NetLinx Studio

Expression as argument... why not?

Hello

Maybe obsolete, but still makes me ask WHY?
I have the sat-tv favourite channels stored as an bi dimensional array of integers:
DEFINE_CONSTANT
INTEGER DSTVCH [][3] = { {HundredsCH1, TensCH1, UnitsCH1}, ......, {HundredsCH10, TensCH10, UnitsCH10},...}

Please somebody take off the blind spot in my brain, and explain why something like this will NOT work:
PULSE [dvDSTV, DSTVCH[BUTTON.INPUT.CHANNEL][1]+10]
WAIT 3 PULSE [dvDSTV, DSTVCH[BUTTON.INPUT.CHANNEL][2]+10]
WAIT 6 PULSE [dvDSTV, DSTVCH[BUTTON.INPUT.CHANNEL][3]+10]
and it will ONLY pulse correctly the Hundreds value (the others will be 0)



while this will always work. The only difference is storing the expresions as variable, and not using them as arguments for the PULSE
HUN = DSTVCH[BUTTON.INPUT.CHANNEL][1]+10
TEN = DSTVCH[BUTTON.INPUT.CHANNEL][2]+10
UNI = DSTVCH[BUTTON.INPUT.CHANNEL][3]+10

PULSE [dvDSTV, HUN]
WAIT 3 PULSE [dvDSTV, TEN]
WAIT 6 PULSE [dvDSTV, UNI]

PS: The above is based on button events from buttons with the channel number equal with the preset I am selecting, and the +10 comes from the IR channel numbering (1=[dvDSTV,11], 2=[dvDSTV,12],etc)
Many thanks!

Comments

  • BUTTON.INPUT.CHANNEL is the problem

    BUTTON.INPUT.CHANNEL is a BUTTON_EVENT object value that is not available to the code after your WAIT. As you have the discovered, the value will be zero when trying to define the element of your array.

    This how the run time will interpret those lines.
    WAIT 3 PULSE [dvDSTV, DSTVCH[0][2]+10]
    WAIT 6 PULSE [dvDSTV, DSTVCH[0][3]+10]
    
  • ericmedleyericmedley Posts: 4,177
    As B_CLEMENTS says, Button.Input,Channel goes away after the button_event fires. However, if you store the value in a variable you can use it later.

    So,
    DEFINE_VARIABLE
    
    volatile integer Button_pushed
    
    DEFINE_EVENT
    
    BUTTON_EVENT[]
    {
    PUSH:
      {
      Button_pushed=Button.Input,Channel
      // etc...
      }
    }
    

    now replace all the Button.Input,channels in your other statments with Button_pushed and it'll work just fine.
  • Thanks gents. Looks like I have ignored valuable basics throughout my whole coding so far...
    There is nothing to argue with here, since the facts are happening exactly how you describe them, it just seems that the AMX documentation has misled me when saying, under "Button Events" that

    "The BUTTON object is available to the button event handler as a local variable",
    which, in my understanding, would have had to be kept stored for the whole life span of that BUTTON_EVENT, and not reset after once used...

    [INNER MONOLOGUE]
    And, just to confuse me more, even in the example above I am referencing three times to BUTTON.INPUT.CHANNEL when storing it's value in the HUN, TEN, UNI variables. Right... not using any WAIT this time.

    Does it have to do something with, maybe, the fact that during the WAIT time another button event might have occurred? (The release...) But even then, we are talking about the same button, just not stored because there is no RELEASE: statement in my button event?
    [/INNER MONOLOGUE]
  • viningvining Posts: 4,368
    felixmoldovan wrote:
    "The BUTTON object is available to the button event handler as a local variable",
    It's local in scope but like a stack var is only holds a value between the opening and closing brace of code where it is used. Waits are always considered outside the braces which is why stack vars don't work either.

    felixmoldovan wrote:
    Does it have to do something with, maybe, the fact that during the WAIT time another button event might have occurred?
    No. An event object will always have a value of zero outside the event because it doesn't exist outside the event.
    I guess they could make it so it gives you a complier warning when used in a wait since it is then technically outside the event like a stack var.
  • vining wrote: »
    Waits are always considered outside the braces

    This goes right on top of my "Netlinx for dummies" list!
    Thanks. If, axiomatically, I refer to the above, then suddenly the TV is showing the right bloody channel :-)
  • DHawthorneDHawthorne Posts: 4,584
    Waits are pushed completely off the event queue until next pass of mainline. I think of it like this: your code inside the wait is put in a holding area, and every pass of mainline just checks if the timer ran out, and if it has, runs the code. At that point, all context is lost; it only knows about the code inside the wait, not anything about the context it came from.

    Global variables that track events reset each pass of mainline, as I understand it. If the code runs right away, it will remain good until the next event overrides it, but the wait pushes it past that reset point.
  • AMXJeffAMXJeff Posts: 450
    Hello

    Maybe obsolete, but still makes me ask WHY?
    I have the sat-tv favourite channels stored as an bi dimensional array of integers:

    I have a question for you, why not uses the XCH command? Seems to be alot of work around something a command has been created to do automatically...

    // PULSES THE CORRECT CHANNELS FOR CHANNEL 123
    SEND_COMMAND dvTV,'XCH 123'
  • You're absolutely right. I eventually dropped the idea, but, initially I wanted the channels as integers (arrays) as opposed to strings so I can use + and - on the consolidated value when CH+, CH- will be pressed, and eventually let the user store his own presets as I pick it up from some variable

    Nevertheless, I abandoned that as soon as I realized there will be NO WAY to keep track of the actual channel the decoder is on, because of many factors, including the concurrent use of the supplied remote...

    And yes, even with integers, it could have worked very well and easier your way, even with leading 0...
Sign In or Register to comment.