Draw text and follow mouse

Is there a way to draw a number next to the cursor using gpu shaders?

yes and no- it’s not done with bgl, it’s done with blf.

I would have to include mouse event. But when I plug that into the bgl position, it doesn’t follow the mouse

again… it’s not done with bgl. it’s done with blf.

font_size = 16
scaled_font_size = int(font_size * bpy.context.preferences.system.ui_scale)

blf.size(0, scaled_font_size, 72)
# self.stored_mouse_pos is assuming you stored the mouse position in your modal function. You don't need the entire event, just the mouse position.
blf.position(0, self.stored_mouse_pos[0], self.stored_mouse_pos[1], 0)
blf.draw(0, f"This is text following the mouse {self.stored_mouse_pos}")

I believe i’d have to append it?

self.stored_mouse_pos.append( (event.mouse_region_x, event.mouse_region_y))

that’s up to you… how many mouse positions do you need? are you tracking historical mouse position? if not why would you need a list of mouse positions?

blf.position takes four inputs, the middle two are for x and y positions respectively- in my example, we’re taking the X position from the mouse_pos tuple (index 0), and the Y position from index 1 of the tuple.

Just 2d coordinates of the viewport region. X & Y are fine but when I plug that into the drawcallback it doesn’t update

there’s an error in your code somewhere, you’d need to post it- otherwise I have no idea what it could be.

As I mentioned, blf.position is expecting individual coordinates (see the docs), you’re passing it a tuple in the position it’s expecting ‘x’ (that has not been properly initialized and doesn’t exist in the scope of the draw handler anyway. I would assume you’re getting a TypeError exception on that line?