From the "Environment Controller" block, I extracted the code
switch_mode = rtwenvironmentmode(bdroot(gcbh)) || ...
(exist('sldvisactive','file')~=0 && sldvisactive(bdroot(gcbh)));
which is exactly the flag I needed. It separates between the code generation (ctrl + b) and the simulation in Simulink (green play button). I use it in the initialization method of the system by replacing droot(gcbh) with gcs.