# ¶ G-Code Guide

## ¶ Obtaining values from variable names

The variable "layer_z" can be accessed directly by using its name.

To obtain its value, use  `{layer_z}`.

## ¶ Accessing the value of an array or vector placeholder using variable name `[index]`

"`cool_plate_temp_initial_layer`" accesses the first element of "cool_plate_temp_initial_layer".

To obtain its value, use `{cool_plate_temp_initial_layer}`.

## ¶ Conditional statements

`{if scan_first_layer}`

`;=========register first layer scan=====`

`M977 S1 P60`

`{endif}`

## ¶ The ternary operator/Conditional operator

`(<condition> ? <cond_true>:<cond_false>)`

## ¶ Combining G-code instructions

`S[next_extruder]`

`S{cool_plate_temp_initial_layer}`

## ¶ Representing the String data type using "string"

Strings:

`"Bambu PLA Basic @BBL X1C"`

## ¶ Regular expression

`/regex/`

## ¶ Compare <, >, ==, !=, <>, <=, >=

`toolchange_count > 1`

## ¶ Logical Operations &&, ||, !

`{if old_filament_temp > 142 && next_extruder < 255};dosomething{endif}`

## ¶ Arithmetic operations +，-， *， /

Arithmetic Operators：

`{layer_num+1}`

Float operations return float type, while integer operations return integer type.

If you want the result to be a decimal value, you need to include a float type argument in the operation.

For example, 3/2 will return 1 (an integer), while 3.0/2 will return 1.5 (a float).

## ¶ Matching

`=~` matching and `!~` not matching:

`=~` and `!~` are comparison operators used in programming to check if a string matches a pattern or not. The `=~` operator checks if a string matches a pattern, while the `!~` operator checks if a string does not match a pattern.

## ¶ Function operations

Functions are a set of instructions that are executed when called upon to do so. In programming, a function may accept arguments and return a result. Here are some common function operations in programming:

• `min(a, b)`: returns the minimum value between `a` and `b`.
• `max(a, b)`: returns the maximum value between `a` and `b`.
• `int(a)`: converts `a` to an integer type.
• `round(a)`: rounds `a` to the nearest integer.
• `digits(a, num_digits, num_decimals=0)`: rounds the decimal portion of `a` to an integer and displays `num_digits` digits using space padding, where `num_decimals` defaults to 0 and can be left empty.
• `zdigits(a, num_digits, num_decimals=0)`: same as above, except it uses 0 padding instead of space padding.