Is there any way to "kill" a taskfunc instance?

vagfilm1 Member Posts: 5 Newcomer
edited February 24 in Scripting Workshop

Hi. Amateur ksp programmer here using SublimeKSP, and I have hit a roadblock in a piano script I am doing...

In order to script re-pedal events (ie, rapid pedal press after release that "catches" a sustaining note and maintains it instead of fast decaying), I have scripted the normal release decay as a succession of 4 fast tcm.wait() and fade_out commands that create an exponential decay of the sample (instead of the non-piano like linear decay of the regular fade_out...). Those tcm.wait and fade_outs are set inside a taskfunc that is triggered for each note release. So far so good and the release decays sound as good as release samples.

However, for scripting the re-pedalling I need to stop that taskfunc whenever there is a change in the #CC value of the pedal, so that a new faster or slower fade_out() command overrides the ongoing fade_out. However, I cannot find a way to "kill" a particular taskfunc... I am storing the taskfunc CALLBACK_ID in a array and I use that information to stop_wait(CALLBACK_ID, 1) the remaining tcm.wait() commands in that instance, but that does not block the remaining fade_outs from being executed. Is there something like exit(CALLBACK_ID)? Is there a work-around I am not thinking about? Any help is appreciated...


  • vagfilm1
    vagfilm1 Member Posts: 5 Newcomer
    edited February 21

    Replying to my own discussion... I will try to abort the instance by having it check a global variable (an array that acts as flag for each active instance of the taskfunc). So, before executing the next tcm.wait() within the taskfunc, it will query the flag variable and if true it will execute a exit() command. Not elegant, but it should work, although it is a lot of checking and doublechecking... An assignable exit(callback_id) would be far better.

    All additional suggestions and criticisms are welcome...

  • Gee_Flat
    Gee_Flat Member Posts: 903 Guru

    Should either ask for this to be moved to Scripting Workshop, or repost it there so it gets better attention.

  • vagfilm1
    vagfilm1 Member Posts: 5 Newcomer

    Thanks... Problems of first time poster...

  • vagfilm1
    vagfilm1 Member Posts: 5 Newcomer

    In case someone stumbles upon this discussion, my solution worked as intended. In fact, I ended up using the callback_id as a "flag"... Everytime a note calls the decay taskfunc() the callback_id is stored in a array[note]. Inside the taskfunc() there are 4 tcm.wait() commands that interrupt 4 fade-outs of increasing length. All it was needed was to check, before each tcm.wait(), if the callback_id of that note remained the same as the callback itself. If not, then it means that a newer instance was triggered and in that case the callback should be aborted using the exit command...

  • EvilDragon
    EvilDragon Moderator Posts: 947 mod

    Do note that using exit in taskfuncs is not entirely clean and has unforeseen outcomes.

  • vagfilm1
    vagfilm1 Member Posts: 5 Newcomer
    edited February 25

    ED: Thanks for the heads-up, but I am afraid that the concept of TCM memory pointers is beyond my skills... Should I put a call to the "TCM epilogue" (whatever that is...) before the taskfunc exit? My taskfunc is rather simple and contains only a few lines of code: a fade_out command followed by a conditional wait() or exit, then another fade_out and another check for wait or exit, and then a third similar block. Should I worry? What should I be looking for? Execution error or abnormal notes?

Back To Top