Skip to content

Receiving Variable Updates

Unlike normal gcode_macros, Dynamic Macros supports receiving variable updates within the same macro. For example, the following macro will show the same output in both M117s:

1
2
3
4
5
6
7
8
9
[gcode_macro STATIC_MOVE]
gcode:
  G28
  M117 Before: {printer.toolhead.position.z}
  # Above displays position before macro run
  G90
  G1 Z20
  M117 After: {printer.toolhead.position.z}
  # Above displays same position

However, this macro will show different outputs based on the current Z position of the toolhead:

[gcode_macro DYNAMIC_MOVE]
gcode:
  G28


  M117 Before: {printer.toolhead.position.z}
  # Above displays position after G28
  G90
  G1 Z20


  M117 After: {printer.toolhead.position.z}
  # Above displays position after G1

Notice the large whitespaces. Three newline characters (two blank lines) between code segments denotes a variable update. However, some variables won't be preserevd across the whitespace.

1
2
3
4
5
6
7
8
[gcode_macro VARIABLES]
gcode:
    {% set num = 10 %}
    M117 {num}


    M117 {num}
    # Above line outputs nothing

You can use the update() function to preserve certain variables across the whitespaces:

1
2
3
4
5
6
7
8
[gcode_macro VARIABLES]
gcode:
    {% set num = update("num", 10) %}
    M117 {num}


    M117 {num}
    # Above line outputs 10

See Examples for examples.

Comments