Solved Do globals used in config.g have to be declared there?
-
In my config.g file I ended up with a growing number of global variables. Life was good.
I decided to place the global declarations into a macro, called towards the start of config.g. Life ceased to be good. An ~"unknown variable"~ error occurs.
It seems that config.g is parsed before it is run and if a global is used that is not explicitely defined within config.g then the error above occurs. Notet that the reference to the global occurs after the macro call in config.g, so its not a positional issue.
Is this expected behavior ? It would be nice to be able to place all globals into a single macro and not have to worry. A workaround (I have not tried yet) is to place any portion of config.g that uses global variables inside a separate macro, but that seems forced.
All globals are declared / initialized thus:
; DirectDrive set to false is using Bowden var DirectDrive = true if !exists(global.DirectDrive) || global.DirectDrive=null global DirectDrive = var.DirectDrive else set global.DirectDrive = var.DirectDrive
-
@stuartofmt
You can declare globals whenever you want to, but you can't haveconfig.g
check for or depend on a global unless you declare it earlier in 'config.g` since it simply haven't been declared yet (when starting the printer).I've solved this by calling out
init.g
at the end ofconfig.g
, insideinit.g
i do the "other part" of initial startup.The first thing inside
init.g
is to executeglobals.g
who contain all the globals thats supposed to exist in "daily use" . Then afterglobals.g
are called (insideinit.g
) i run the checks that depend on the outcomes from the misc. globals that just got declared. -
@Exerqtor
Thank, I tried using your structure but ended up having to make some modifications.It looks like you cannot access globals (within a macro) that are declared by calling another macro from within that macro. I.e. the behavior is not limited to config.g. At least that seems to be the case with 3.5.0-beta3.
I created a minimal
config.g
which callsinit.g
. At the startinit.g
callssetGlobals.g
and later on tries to use a global (created insetGlobals.g
). This failed wih the same message as before. This seems identical to your structure, so I'm not sure why I'm getting a different behaviorI then moved the call to
setGlobals.g
into 'config.g' (before the call toinit.g
). All is well.I'm not sure if there is any downside to this approach ... most all the printer setup is now performed in
init.g
(and other macros that it calls). Myconfig.g
is simply, this:;M929 P"anEventlog.txt" S3 ; start logging ; ;Minimal config.g ; Setup and enable Network first so that if errors, still have DWC available M550 P"srsender" ; set printer name ;M551 P"mypassword" ;M552 S1 P"IoT2" ; enable wifi with ssid (standalone only) ;M552 S1 ; enable ethernet (standalone only) M586 C"*" ; enable CORS M586 P0 S1 ; enable HTTP M586 P1 S0 ; disable FTP M586 P2 S0 ; disable Telnet ; ; Setup Global Variables M98 P"setGlobals.g" ; Initialize M98 P"init.g"
-
-