Python error in Advanced Bloxter (creating functions)

I’ve found that when I use “Advanced Bloxter” and create functions I hit an error when I try to execute the program. The error is:
local variable 'gpg' referenced before assignment

When I look at the Python code produced by Bloxter, the problem is quite clear. The gpg = easy.EasyGoPiGo3() line is written after my function is declared.

Is this a known problem? Is there a work-around I can employ?

The full python code is shown below:

import easygopigo3 as easy
import time

direction = None
x = None

my_servo_portSERVO1 = gpg.init_servo('SERVO1')

my_Distance_portI2C = gpg.init_distance_sensor('I2C')
time.sleep(0.1)

"""Describe this function...
"""
def look_and_drive(direction):
    global x
    my_servo_portSERVO1.rotate_servo(direction)
    if my_Distance_portI2C.read() > 15:
        gpg.led_off(1)
        gpg.led_off(0)
        gpg.open_eyes()
        if my_Distance_portI2C.read() > 30:
            gpg.set_speed(400)
        else:
            gpg.set_speed(200)
        gpg.forward()
    else:
        gpg.close_eyes()
        gpg.set_speed(200)
        if direction == 90:
            gpg.led_on(1)
            gpg.led_on(0)
            gpg.turn_degrees(1*180, blocking=True)
        else:
            if direction == 45:
                gpg.led_on(0)
                gpg.turn_degrees(-1*45, blocking=True)
            else:
                gpg.led_on(1)
                gpg.turn_degrees(1*45, blocking=True)
        gpg.led_off(1)
        gpg.led_off(0)

gpg = easy.EasyGoPiGo3()


# start
while True:
    direction = 45
    look_and_drive(direction)
    direction = 90
    look_and_drive(direction)
    direction = 135
    look_and_drive(direction)
    time.sleep(0.05) # slowdown

To answer my own query/problem:
I found that if the function block starts lower down the page than the “main” block (even a few pixels lower is fine) then the error is avoided (because the “main” block gets declared first in the Bloxter file and resulting python).

So something to watch out for if you’re using functions.

Remind me which version of DexterOS you’re using?
I thought I had this fixed already.
(and yes, it’s a limitation of the tools we use for Bloxter. Bit of a pain…)

Cleo