Reset ServerBoot

Hello, im following the wonderful SC tutorials series from Eli Fieldsteel

I have noticed that in the architecture part it is mentioned how to use ServerBoot/Tree/etc. It seems very practical, but in the startup routine it is said to run ServerBoot.remoevAll; to reset it, i found out tho that this leads to deletion of important internal functions as well.

So to my first question,

1. what is the best approach here? I though of storing the functions in a “bag” and on startup iterating on it and removing them one by one.

Something like the following:

(
s = Server.local;

~cleanup = {
	if(currentEnvironment.includesKey(~serverBootBag),
		{
			~serverBootBag.do { arg func;
				ServerBoot.remove(func);
			};
		}
	);
	~serverBootBag = [];
};
~clenaup.value;

~testfunc1 = {arg server; "test".postf(server);};
~testfunc2 = {~aBus = Dictionary.new;};

ServerBoot.add(~testfunc1);
~serverBootBag.add(~testfunc1);
ServerBoot.add(~testfunc2);
~serverBootBag.add(~testfunc2);

ServerBoot.objects.postln;

ServerQuit.add(~cleanup);

s.waitForBoot({
	// ...
	"READY".postln;
});
)

2. It seems tho i am doing something wrong since the cleanup function is not being called (the ~cleanup variable appear to be always nil, even if present in the currentEnvironment). Have i not understood something of the global variables behaviour?


PS: if it may be useful i am creating kind of a summary transcript of most videos here: GitHub - jalone87/supercollider-resources

What are you actually trying to achieve with this?

You need to check topEnvironment if you want to access ‘global’ variables from anywhere, as the current environment can be replaced. Generally, there’s a better way of doing this, hence why knowing your intentions might help :smiling_face:

Ok, let me try to clarify, i am trying to create a block that i can call to reset the whole architecture or plain run the whole program. As detailed in this video.

This should be responsible for configuring the server, and instantiating and configure all the needed synths, groups, midi events, etc, in such a way that if i re-call it (to reboot the server) i would bring back everything to the initial state. It’s very useful when i am experimenting and trying things out.

I could just close and reopen the editor and the server, and just have the routines that create all of the architecture, but as mentioned would be nice to just CMD+Enter the block and have it clean it’s state and re-create everything, or i would have duplicates in the language part of the code, for example in the ServerBoot callbacks.

In the specific case of ServerBoot, this object contains references to functions to be executed on server boot. I add some custom functions to it, and on reboot i would like to re-add them (i may have updated the code), but it’s also prefilled with internal supercollider/editor functions, so i can’t just wipe it before adding them. I need to keep track of the functions i add and remove them myself.

It’s entirely possible this is not a good approach, or it’s outdated, I am new here sorry.

Thanks a lot for the hint about the (top)Environment, i see that i am missing a lot of concepts there.

There is another shortcut (CTL+alt+L I think…) that recompiles the class library. Then to run stuff you can either add stuff to the startup file, or in an initClass method. That’s how I’d do it. There are some drawback, but since you want to reset everything including sound, it seems perfect for this!

Oh wow i did not know, thanks so much i have a lot of info to go now!

I think the ~cleanup variable is fine – but when evaluating, you’re asking for ~clenaup which is nil. (Programming languages are not forgiving of typos.)

Also, there’s a more subtle typo here:

currentEnvironment.includesKey(~serverBootBag)

In a dictionary, keys are typically Symbols, which are written as a \symbol or 'symbol'. The method name includesKey tells you that it expects a key, which would be written \serverBootBag – not with the ~.

~serverBootBag is shortcut syntax for \serverBootBag.envirGet, returning the value associated with the key.

currentEnvironment.includesKey(\serverBootBag)

hjh

1 Like

Thanks a lot for the clarifications!
(Ouch that ~clenaup typo was pretty silly)