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

Load error with NKA Script

Discussion in 'Scripting Workshop' started by Reylon, Dec 26, 2021.

  1. Reylon

    Reylon NI Product Owner

    Messages:
    124
    Hi Guys,

    When you save your NKA preset and then try to load it, the a,b,c knobs stay at the same position and doesn't change, but as soon as you hit again on load the values gets loaded correctly.

    Anyone has an eye of an eagle? I can't seem to find the problem here.
    Code:
    on init
    make_perfview
    
        declare const $data := 2021
        declare ui_knob $a(0,127,1)
        declare ui_knob $b(0,127,1)
        declare ui_knob $c(0,127,1)
    
    
        make_persistent ($a)
        make_persistent ($b)
        make_persistent ($c)
    
    
        declare $load_arr_id
                $load_arr_id := -1
        declare $save_arr_id
                $save_arr_id := -1
                  
        declare ui_button $Load
        set_text($Load,"Load")
        set_control_par_str(get_ui_id($Load), $CONTROL_PAR_PICTURE, "")
        set_control_par(get_ui_id($Load), $CONTROL_PAR_HEIGHT, 15)
        set_control_par(get_ui_id($Load), $CONTROL_PAR_WIDTH, 60)
        set_control_par(get_ui_id($Load),$CONTROL_PAR_POS_X, 380)
           set_control_par(get_ui_id($Load),$CONTROL_PAR_POS_Y, 10)
    
        declare ui_button $Save
        set_text($Save,"Save")
        set_control_par_str(get_ui_id($Save), $CONTROL_PAR_PICTURE, "")
        set_control_par(get_ui_id($Save), $CONTROL_PAR_HEIGHT, 15)
        set_control_par(get_ui_id($Save), $CONTROL_PAR_WIDTH, 60)
        set_control_par(get_ui_id($Save),$CONTROL_PAR_POS_X, 380)
           set_control_par(get_ui_id($Save),$CONTROL_PAR_POS_Y, 30)
          declare %save_data [$data]
    end on
    
     
    
    on ui_control($Load)
            $a:=%save_data[2]
            $b:=%save_data[3]
            $c:=%save_data[4]
            load_array (%save_data,0)
            $Load :=0
            message("File Loaded")
    end on
    
    on ui_control($Save)
            %save_data[2]:=    $a         
            %save_data[3]:=    $b                 
            %save_data[4]:=    $c         
            %save_data[$data]:=$data
            save_array (%save_data,0)
            $Save :=0
            message("File Saved")
    end on
    
    on async_complete
        if ($NI_ASYNC_ID = $load_arr_id)
            $load_arr_id := -1
            $Load := 0
        end if
        if ($NI_ASYNC_ID = $save_arr_id)
            $save_arr_id := -1
            $Save := 0
        end if
    end on
    
     
  2. medusa

    medusa NI Product Owner

    Messages:
    239
    You have to finish loading the array (wait async) before you can assign the values in it to the knobs.
     
  3. Reylon

    Reylon NI Product Owner

    Messages:
    124
    Hi Medusa,thank you for the reply.

    sorry to say I didn’t quiet understand what you mean. Should I move the wait async up somewhere?
     
  4. medusa

    medusa NI Product Owner

    Messages:
    239
    At the moment, in your Load code, you're assigning the preset values to the knobs BEFORE you've even loaded the preset. You need to load the preset, wait until it's finished loading (wait async) and only then place those values in the knobs.
     
  5. Reylon

    Reylon NI Product Owner

    Messages:
    124
    Hmm im not that good with async stuff so this will take me a while to get right, I'm trying to figure it out. Thanks anyway :)
     
  6. medusa

    medusa NI Product Owner

    Messages:
    239
    async is basically a process that we don't know will finish, or when. One such process is loading a file, we can't use the file UNTIL it's been sucessfully loaded. Since loading a file takes time, we use the async complete to know when it's done (we can also find out if it was successful or not). If you just want to wait you can also use the wait_async command inline...

    So a simple version of your load could be this


    Code:
    on ui_control($Load)
        { first load the array,  but wait until that process is finished}
        wait_async(load_array (%save_data,0))
        {  THEN put the values in the knobs}
        $a:=%save_data[2]      
        $b:=%save_data[3]
        $c:=%save_data[4]
        $Load :=0
    end on
     
    Last edited: Dec 26, 2021
  7. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Using wait_async() unfortunately doesn't return exit state (if load NKA file dialog was cancelled, say), so it's still better to use the async_complete callback in such scenarios.

    wait_async() is better used for setting user zone parameters, loading FX etc.
     
  8. medusa

    medusa NI Product Owner

    Messages:
    239
    Of course, I was just trying to make the logic clear and simple to understand.
     
  9. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    For sure! :)
     
  10. Reylon

    Reylon NI Product Owner

    Messages:
    124
    Thank you Medusa! You explained it very well. Also ED thanks for pointing out that async complete. Really appreciate the help here! :)