1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Solved Call user-defined function from 'on init'

Discussion in 'Scripting Workshop' started by Reid115, Oct 21, 2021.

  1. Reid115

    Reid115 NI Product Owner

    Messages:
    40
    EDIT: Okay I guess this is what on persistence_changed is for...

    Is it not possible to call a user-defined function from on init? Basically, I have a knob that needs to scale exponentially. In on init, I have the knob's declaration:
    Code:
    declare ui_knob $size_knob1 (100, 1000, 10)
    move_control($size_knob1, 2, 3)
    set_text($size_knob1, "Size")
    set_knob_unit($size_knob1, $KNOB_UNIT_MS)
    set_knob_defval($size_knob1, 200)
    $size_knob1 := 200
    declare $size1 := 0
    {call scale_size1}
    And then I have this to scale the values how I want:
    Code:
    function scale_size1
        $size1 := real_to_int(pow(int_to_real($size_knob1), 3.0) * 15.0 / 1000000.0)
        set_knob_label($size_knob1, $size1)
    end function
    on ui_control ($size_knob1)
        call scale_size1
    end on
    This works fine except for when the script starts/resets. The function needs to be called on startup to scale the default value of 200 to the proper number and set $size1. I tried "call scale_size1" but this gave me an error, and I get why -- if the function is declared after on init then on init can't see the function, and if it's declared before on init then the function can't see the declared variables. I can easily just copy and paste the function code to on init, but this seems like bad practice given that I will be doing this for many different knobs, and the function's code could hypothetically be huge.

    Thanks.
     
    Last edited: Oct 21, 2021
  2. corbo-billy

    corbo-billy NI Product Owner

    Messages:
    652
    Maybe, just before on ui_control ($size_knob1) _
    Code:
    on persistence_changed
        call scale_size1
    end on
     
  3. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Indeed, just use persistence_changed callback.
     
  4. Reid115

    Reid115 NI Product Owner

    Messages:
    40
    That was it, thanks.