Help with Knob animation

Discussion in 'KONTAKT: Sampling, Programming & Scripting' started by Claud9, Mar 11, 2019.

  1. Claud9

    Claud9 Active Member

    192
    7
    Jul 16, 2016
    Hello everyone,
    I'm trying to modify a script I found on KSP manual.
    It's a script that animates a single knob back an forth at a fixed speed.
    I'm trying to add a second knob and animate it at a different speed so that at the same time they are both moving but at different speed rates.
    It's the first time I'm using the "on listener" call back.
    I tried this solution but is not working and only the first knob is moving. I did also some experiment with "change_listener_par" but is not working. Is something possible to move both at the same time with different speed? Thanks in advance for any help!
    Code:
    on init
    declare ui_knob $test1 (0,99,1)
    declare $direction_test1
    declare $tick_counter_test1
    
    declare ui_knob $test2 (0,99,1)
    declare $direction_test2
    declare $tick_counter_test2
    
    set_listener($NI_SIGNAL_TIMER_MS,7000)
    end on
    
    on listener
     
    if($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)
    if ($direction_test1 = 0)
    inc($tick_counter_test1)
    else
    dec($tick_counter_test1)
    end if
    $test1 := $tick_counter_test1
    if ($tick_counter_test1 = 99)
    $direction_test1 := 1
    end if
    if ($tick_counter_test1 = 0)
    $direction_test1 := 0
    end if
    end if
    
    while ($NI_SIGNAL_TIMER_MS=2000)
    if ($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)
    if ($direction_test2 = 0)
    inc($tick_counter_test2)
    else
    dec($tick_counter_test2)
    end if
    $test1 := $tick_counter_test2
    if ($tick_counter_test2 = 99)
    $direction_test2 := 1
    end if
    if ($tick_counter_test2 = 0)
    $direction_test2 := 0
    end if
    end if
    end while
    
    end on
     
  2. OP
    OP
    Claud9

    Claud9 Active Member

    192
    7
    Jul 16, 2016
    This is an updated version where I'm trying to change the speed of Knob "$test2" using "change_listener_par" but now both Knobs are spinning at the same faster speed. Is there a way to have Knob "$test1' spinning at a different speed than Knob "$test2'? thanks for any help!
    Code:
    on init
    declare ui_knob $test1 (0,99,1)
    declare $direction_test1
    declare $tick_counter_test1
    
    declare ui_knob $test2 (0,99,1)
    declare $direction_test2
    declare $tick_counter_test2
    
    set_listener($NI_SIGNAL_TIMER_MS,7000)
    end on
    
    on listener
     
    if($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)
    if ($direction_test1 = 0)
    inc($tick_counter_test1)
    else
    dec($tick_counter_test1)
    end if
    $test1 := $tick_counter_test1
    if ($tick_counter_test1 = 99)
    $direction_test1 := 1
    end if
    if ($tick_counter_test1 = 0)
    $direction_test1 := 0
    end if
    end if
    
     
    if ($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)
    if ($direction_test2 = 0)
      change_listener_par($NI_SIGNAL_TIMER_MS,2000)
     
    inc($tick_counter_test2)
    else
    dec($tick_counter_test2)
    end if
    $test2 := $tick_counter_test2
    if ($tick_counter_test2 = 99)
    $direction_test2 := 1
    end if
    if ($tick_counter_test2 = 0)
    $direction_test2 := 0
    end if
    end if
     
    
    end on
    
     
  3. EvilDragon

    EvilDragon KSP Wizard

    5,787
    3,634
    May 25, 2010
    Croatia
    Listener is a global thing, so if you change the listener par that means it changes the number of ticks executed. You should probably update the other knob at only certain ticks (use %, modulo operator, to skip, for example, every 2nd or 3rd tick by doing % 2 or % 3 on the tick counter).
     
  4. OP
    OP
    Claud9

    Claud9 Active Member

    192
    7
    Jul 16, 2016
    I don't honestly have any idea how to apply your suggestions, I'm completely new to "on listener" and "mod operator"... I have tried to find an example on KSP manual but I can't find anything similar.
     
  5. EvilDragon

    EvilDragon KSP Wizard

    5,787
    3,634
    May 25, 2010
    Croatia
    You use modulo like any other math operator. a % b. You use that in an if clause. You don't need two queries for $NI_SIGNAL_TYPE.
     
  6. Lindon

    Lindon KSP Developer

    673
    224
    Jul 27, 2010
    Republic of Yorkshire
    so in you init declare a counter

    declare listener_counter

    ..and each time your listener call-back executes increment the counter.

    Now lets say I want to do something (lets call it X) every time the listener executes - then all you do is add your code for X.

    But lets say I ALSO want to do something else (lets call it Y) , BUT... I only want this to happen every 3rd time the listener call back executes...

    in this case put your code inside this if statement (after incrementing the listener_counter)

    if listener_counter % 3 = 0
    {do the Y think here}
    listener_counter == 0;
    end if
     
  7. OP
    OP
    Claud9

    Claud9 Active Member

    192
    7
    Jul 16, 2016
    Thanks for the help. I tried to follow your directions with this code. Now knob 2 seems to do something different from knob 1 but they are still moving at the same speed...
    Thanks in advance for any new help!
    Code:
    on init
    declare ui_knob $test1 (0,99,1)
    declare $direction_test1
    declare $tick_counter_test1
    
    declare ui_knob $test2 (0,99,1)
    declare $direction_test2
    declare $tick_counter_test2
    
    set_listener($NI_SIGNAL_TIMER_MS,7000)
    end on
    
    on listener
     
    if($NI_SIGNAL_TYPE = $NI_SIGNAL_TIMER_MS)
    if ($direction_test1 = 0)
    inc($tick_counter_test1)
    else
    dec($tick_counter_test1)
    end if
    $test1 := $tick_counter_test1
    if ($tick_counter_test1 = 99)
    $direction_test1 := 1
    end if
    if ($tick_counter_test1 = 0)
    $direction_test1 := 0
    end if
    end if
    
    if ($direction_test2   = 0)
    inc($tick_counter_test2)
    else
    dec($tick_counter_test2)
    end if
    if ($tick_counter_test2 mod 3 = 0)
    change_listener_par($NI_SIGNAL_TIMER_MS,5000)
    $test2 := $tick_counter_test2
    if ($tick_counter_test2 = 99)
    $direction_test2 := 1
    $tick_counter_test2:=0
    end if
    end if
    if ($tick_counter_test2 = 0)
    $direction_test2 := 0
    end if
    end on
    
     
  8. EvilDragon

    EvilDragon KSP Wizard

    5,787
    3,634
    May 25, 2010
    Croatia
    I don't see you using modulo anywhere, also you don't need to change_listener_par at all, that will just screw things up. Also you need just a single tick counter.
     
  9. OP
    OP
    Claud9

    Claud9 Active Member

    192
    7
    Jul 16, 2016
    The point is that I have zero ideas what a "modulo" is and how it works. The only thing I have found on KSP manual containing the word "modulo" is this:
    x mod y
    modulo; returns the remainder of a division e.g. 13 mod 8 returns the value 5

    Without an example of code that uses "modulo' for me is not even possible to try to understand how it works...
    And also if I can't change the "SIGNAL_TIMER" to have the second knob spinning at a different speed what I have to change?
     
  10. Lindon

    Lindon KSP Developer

    673
    224
    Jul 27, 2010
    Republic of Yorkshire
    Code:
    if listener_counter mod 3 = 0
    The listener call back can and should only "run" at a single speed - its when you are in there that you decide to make a thing happen....

    modulo will give you the remainder of a divide operation so:
    1 mod 2 will give you 1
    2 mod 2 will give you 0
    3 mod 2 will give you 1
    4 mod 2 will give you 0

    ..see how if we test for a result of 1 then we can do something at half the call back speed?

    1 mod 3 gives you 1
    2 mod 3 gives you 2
    3 mod 3 gives you 0
    4 mod 3 gives you 1
    5 mod 3 gives you 2
    6 mod 3 gives you 0

    .. now we test for a result of 1 and we get to do something every third time the call back happens...
    Code:
    on listener
          inc(listener_counter)
          if listener_counter mod 3 = 1
                {do something every three times the listener execute}
          end if
    
          if listener_counter  mod 2 = 1
                {do something every second time the listener executes}
          end if
    
    
     
    Last edited: Mar 15, 2019 at 8:59 AM
  11. EvilDragon

    EvilDragon KSP Wizard

    5,787
    3,634
    May 25, 2010
    Croatia
    You could've done some example code yourself to see how the math operation works...
     

Share This Page