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 Slider Update on Random

Discussion in 'Scripting Workshop' started by foundation77, Sep 20, 2018.

  1. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Happy Thursday Folks!

    Code:
    on ui_control($randomize)
      $randomize := 0
      $group := 0
      while ($group < $NUM_GROUPS)
        set_engine_par($ENGINE_PAR_PAN, random(0, 1000000), $group, -1, -1)
        inc($group)
      end while
    end on
    So we can randomise (english spelling ;))
    but how do we update a control to display the change?
    Code:
    $PanSlider
     
  2. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You need to execute everything that's in the "on ui_control" callback for $PanSlider. Makes sense to have a function for each UICB if you have a lot of controls.
     
  3. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Well I'm simply learning for the sake of learning at the moment so no need for a vast number of controls.
    I see the point though, I'm going to give that a try.
    I would assume that I could still operate the button using the same principle as mentioned above without needing to change that?
     
  4. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Yeah you would just call the function from the button's UICB, and also from your randomizer button, after you set the value of the control.
     
  5. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    I'm just working on it now, I may have it!
    Am I right to create a function for this?
     
  6. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    That's what I said straight away. :D
     
  7. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    So I started making a function that I could call using the get_engine_par
    I'm just trying to get my head around the logic...

    Code:
    on init
      declare ui_button $randomize
      declare $group
      declare ui_slider $pan (0,1000000)
    end on
    
    
    function update_pan_slider
        $pan := get_engine_par($ENGINE_PAR_PAN, random(0, 1000000), $group, -1, -1)
    end function
    
    on ui_control($randomize)
        set_engine_par($ENGINE_PAR_PAN, random(0, 1000000), $group, -1, -1)
    end on
    
    on ui_control($pan)
    set_engine_par($ENGINE_PAR_PAN, $pan, $group, -1, -1)
    call update_pan_slider
    end on
    
    So I'm struggling (learning) how functions work - I get a parse error on this, I know "The function has to be declared before it is called."
    however I may be getting it wrong with the way I'm writing this.
    Where do you think I can improve here?
     
  8. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Your function should contain set_engine_par, not get_engine_par...

    The idea is: you hig randomize, your parameter gets randomized (you set its value), then you update the engine parameter using set_engine_par().

    You got a parse error because get_engine_par() which you used in the function has one extra argument that shouldn't be there (using the random() function).
     
  9. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Code:
    on init
      declare ui_button $randomize
      declare $group
      declare ui_slider $pan (0,1000000)
    end on
    
    
    function update_pan_slider
        set_engine_par($ENGINE_PAR_PAN, $pan,$group,-1,-1)
    end function
    
    on ui_control($randomize)
    $pan := random(0,1000000)
    call update_pan_slider
    end on
    
    on ui_control($pan)
    $randomize := random(0,1000000)
    set_engine_par($ENGINE_PAR_PAN, $pan,$group,-1,-1)
    end on
    Just got back, I think I was tired when I was doing this before - Does the above seem correct to you?
    It works this end however just wanted to make sure I'm not leaving in anything here that doesn't need to be there.

    Thanks again for your help Mario, I have little time to dedicate to learning KSP but your help really.... helps! :)
     
  10. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    I'm not sure why would you set $randomize in $Pan UICB at all, that's totally not necessary. You might as well call the function from $Pan UICB, too.
     
  11. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    I see your point now

    Code:
    on init
      declare ui_button $randomize
      declare $group
      declare ui_slider $pan (0,1000000)
    end on
    
    
    function update_pan_slider
        set_engine_par($ENGINE_PAR_PAN, $pan,$group,-1,-1)
        $pan := random(0,1000000)
    end function
    
    on ui_control($randomize)
        call update_pan_slider
    end on
    
    on ui_control($pan)
        set_engine_par($ENGINE_PAR_PAN, $pan,$group,-1,-1)
    end on
    Would you say that is better, would you say I'm 'getting it' ....maybe?
     
  12. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Errr, not quite. :)

    Why would you always randomize in the function? Makes no sense.

    Code:
    function update_pan_slider
         set_engine_par($ENGINE_PAR_PAN, $pan,$group,-1,-1)
    end function
    
    on ui_control($randomize)
        $pan := random(0,1000000)
        call update_pan_slider()
    end on
    
    on ui_control($pan)
      call update_pan_slider()
    end on
     
  13. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Now I feel stupid haha!
    So first determine the parameter in the function
    then tell it to randomize using the button whilst calling the function and call the function in the slider.
    the function itself (want for a better phrase) links the two as long as they are both calling the function.