Unexpected behaviour of globalState
Hi there,
it is probably my fault, but globalState does not seem to work as expected.
This is my Script:
var num = 0
while (globalState.synqrFriendActive){
log (num)
num += 1
sf.wait({
intervalMs: 1000,
executionMode: "Background",
})
}
It logs ascending numbers, one every second.
From my understanding it does that as long as globalState.synqrFriendActive is true.
But when I set globalState.synqrFriendActive to false from another script, the while loop continues. Is that expected behaviour? Can I work around it?
- Chad Wahlbrink @Chad2024-06-25 19:19:32.486Z
Hey @Benjamin_Horbe!
Thanks for the question.
The issue you are running into is thatexecutionMode: "Background"
does not mean the entire action will be run in the background. If you wanted a whole function to run in the background, you would use a callback function like this:globalState.synqrFriendActive = true; var num = 0 sf.engine.runInBackground( function () { while (globalState.synqrFriendActive) { log(num) num += 1 sf.wait({ intervalMs: 1000, executionMode: "Background", }) } })
In this scenario,
sf.engine.runInBackground()
will continue running the while loop in the background as expected. If you don't usesf.engine.runInBackground()
, then the SF engine will continue indefinitely and never release control for the action to be canceled (without calling "Stop All Running Commands" or restarting SoundFlow).
To stop this kind of loop, you would call something like this from another command:
This behavior is more commonly setup as a "runForever" script like this example:globalState.synqrFriendActive = null;
Benjamin Hörbe @Benjamin_Horbe
Thank you. I knew it was my fault!