What's new

UI elements not affecting vars on release

KrisY

Noob in frenzy loop
Hello. I am trying to get a UI-element on my ui to affect a variable in the RCB. It is a ui_value_edit element and let´s users enter a value of their choice between 1-19. The value is then used in on release as a minimum velocity under/over which to play or not to play certain things. It all works fine on init, I set the ui-element to make_persistent and I can retrieve the values fine, also upon pressing the "Apply" button in the script´s slot it always responds to the latest value.

My issue is what happens when I enter a new value on the ui. It does not respond to it correctly.

These things happening every time:

1. open instrument, variable is read via the persistence and is correct. No notes play below the set velocity value in the ui_value_edit.
2. I change the value in the ui_value_edit element, now it is a lower value, below the current velocity being played. The tone plays fine. As it should, it is above the minimum velocity value.
3. I again change the value in the ui_value_edit element, but now to a higher value than the previous one. I now play a velocity below the new value. It should not play, but it does anyway. The latest value was not used, whereas just a second ago, that was the case when the value was lowered.

I did not change or apply the script since the last 2 value changes, they both had the same chance to affect the RCB.

Is there something I am missing?

(my variable in the RCB that reports the current minimum velocity, is the same as the one on the ui.)
 

EvilDragon

KSP Wizard
What's the actual code?

This works just fine here:

Code:
on init
   declare ui_value_edit $Vel (1,127,1)
   $Vel := 127
   make_persistent($Vel)
end on

on release
   if ($EVENT_VELOCITY > $Vel)
       ignore_event($EVENT_ID)
   end if
end on
 
OP
K

KrisY

Noob in frenzy loop
What's the actual code?

This works just fine here:
... ...
Yes indeed that works good, in the test below:

Code:
on init

   declare ui_value_edit $Vel (1,127,1)
   $Vel := 1
   make_persistent($Vel)

end on
on note

    disallow_group($ALL_GROUPS)
    allow_group(0)

end on
on release

    message("velocity is: " & $EVENT_VELOCITY)
    if ($EVENT_VELOCITY < $Vel)
         ignore_event($EVENT_ID)
    else
         disallow_group($ALL_GROUPS)
         allow_group(2)
         play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, 0)
    end if

end on
The script i´m on is a long and tedious one, will try to narrow this feature down more accurately than I have so far. Posting it all here will include multiple errors, most likely and a waste of your time. But this above works and my issue is most likely the if-else regarding velocity, there may be contradictions I´m not seing. In my original there is also no pre-defined "$Vel := 1" on init. Which I thought was redundant since it´s an input value. But will add that. Many thanks for the help. If I cannot solve it I´ll add my condensed issue in detail.
 
OP
K

KrisY

Noob in frenzy loop
What's the actual code? ...
Solved. The issue above was not the issue at all. My script is utilising a variable that stores the relevant group number throughout the script. Lots of UI elements and scenarios demanded that, or there would be too many repetitions. So the same var "$allowGroupX1 := 0/whatever" handles the changing and storing of the relevant group, followed by a last play_note with allow_group($allowGroupX1) ahead of it. That way I can keep the script concise.

My issue was that I had not reset the variable in the end of the on release. So the next time the release was asked for a number, the previous number applied, even in the event that the minimum velocity was not met. A quick workaround would have been to say if (EVENT_VELOCITY < min_vel) -> ignore event, as you did. But I didn´t like that I got a number that was wrong in the end of the script. Felt eerie.

Code:
...
    $allowGroupX1 := 99   {99 for "off", as instrument has max 85 grups.}
    $allowGroupY1 := 99
    $allowGroupZ1 := 99
end on  {release}
... that solved it. Thanks for the code.
 
Top Bottom