# KSP - invoking functions from the init callback



## kotori (Jun 5, 2010)

Hi everyone,

If one has UI initialization code that is invoked both from the init callback and UI callbacks it's a bit annoying to get duplicated code in the compiled output. Here's a little trick to work around the limitation of not being able to use the new "call" keyword in the init callback:

*on init*
``message('')
``_{ trigger pgs callback }_
``pgs_create_key(post_init_done, 1)
``pgs_set_key_val(post_init_done, 0, 0)``
*end on*

*on _pgs_changed*
``_{ if post initialization not already done, do it }_
``*if* pgs_get_key_val(post_init_done, 0) = 0
````pgs_set_key_val(post_init_done, 0, 1)
````*call* post_init
``*end if*````
*end on*

*function* post_init
``message('post_init function executed!')
``_{ ... do post initialization here ... }_
*end function*

This technique is of course not only useful in this case, but also generally when one wants asynchronous function calls.

Cheers,
Nils


----------



## gregjazz (Jun 5, 2010)

Thanks Nils!

Actually I've been using the technique for a while, and it's a cool trick to bypass the limitations of the on_init callback, but still run events on the initialization of the instrument.


----------



## Dynamitec (Jun 5, 2010)

Make sure you don't run any while loop with wait on init if called via pgs. On some machines this causes trouble.

Cheers,
Benjamin


----------



## kotori (Jun 5, 2010)

Dynamitec @ Sat Jun 05 said:


> Make sure you don't run any while loop with wait on init if called via pgs. On some machines this causes trouble.



Ok. That's good to know.

Btw. speaking of problems I ran into a really strange bug that it might be good for more people to be aware of. The following script will never get further than to presenting the message "Stage 2". However, if you modify the function call in the ui callback to helper_func or invoke _func_ using inlining instead then it presents "Stage 3":

*on init*````
``*declare* ui_switch test``
*end on*

*function* helper_func
``*call* func
*end function*

*function* func
``message("Stage 1")
``message(get_ui_id(test))
``message("Stage 2")
*end function*

*on ui_control*(test)````
``*call* func
``message("Stage 3")
*end on*

It seems that invoking get_ui_id inside functions invoked using "call" in some circumstances causes the execution of parts of the code to be skipped. Have you seen this problem before?


----------



## Dynamitec (Jun 6, 2010)

Hi Nils,

this is a bad one! Checked and reported! Haven't seen this one yet.

Cheers,
Benjamin


----------



## Big Bob (May 17, 2011)

Hi Nils,

Re:



> on init
> ``message('')
> ``{ trigger pgs callback }
> ``pgs_create_key(post_init_done, 1)
> ...



I think there may be a problem with doing it exactly as you've shown it when you have scripts in more than one slot trying to do a post init. For example, if the script in the first slot and the second slot both use this code, the 2nd script's pgs will trigger with post_init_done set to one and the conditional test will fail. 

Perhaps it might be better to also set an ordinary variable in the ICB (along with setting the pgs var). Then, in the pgs callback, test the ordinary variable to do a conditional init routine once and then clear the ordinary variable. In other words, use the pgs var only to trigger the callback but don't read or alter it in the pgs callback. :roll: 

Rejoice,

Bob


----------



## kotori (May 23, 2011)

Hi Bob,

I normally use different pgs variables in each script, but I guess your suggested alternative should work too and it is perhaps even cleaner.

Cheers,
Nils


----------



## Big Bob (May 23, 2011)

Hi Nils,

Yes, indeed if each script slot uses a different pgs var there is no problem. I guess I'm always thinking 'Library' these days since I'm working on the Math Library. :lol: 

If the library provides a utility to do this pgs thing (which the Math Library now does) and the library is imported into more than one script slot, then the problem will occur. I know, because I got bit by it :lol: 

Rejoice,

Bob


----------

