Circuity Python Stage Text Item Buffer Issue
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Circuity Python Stage Text Item Buffer Issue

by randomhacksdrc on Mon Jul 08, 2019 8:40 pm

According to the stage.py module from

https://github.com/python-ugame/circuit ... r/stage.py

The __init__ routine sets the width and height of the text box. In addition it creates a variable called self.buffer

self.buffer = buffer or bytearray(width * height)

This is slick because it declares memory on the fly but the issue I'm having is displaying text that changes length every iteration. I have multiple levels like pacman that increase in difficulty and I want text to display how long it took you to complete the level using the time.time() module (after converting to a string of course).

The problem is that

3.2 seconds (11 characters)
19.5 seconds (12 characters)

etc are different lengths. I guess theoretically every level I could create a new Text() module but after like 50 levels that seems kind of ridiculous to constantly create a new Text object. Seems more reasonable to create a new method like Text.Reset(str new_text) where the old buffer is deleted from memory and then new memory is allocated to accomodate the new_text with the length of the new_text.

I tried initially creating a text box that had a large enough buffer to accommodate everything in combination with Text.clear() but it didn't work as nicely as I'd like it to.

Apologies if this is not the venue for these types of discussions. I guess I could post this as a bug/issue request on github if needed.


Posts: 24
Joined: Thu Feb 11, 2016 11:51 am

Re: Circuity Python Stage Text Item Buffer Issue

by Dumfing on Wed Jul 10, 2019 1:45 am

I'd recommend against modifying libraries for your own use, assuming your memory usage isn't a hypercritical parameter you should be able to just initialize a new Grid (I think that's what you're using) object where the old one was and Python should be able to use its garbage cleaner to free the memory the old one took up

If I were to make a custom Text module, the reset method would just remake the Grid every time rather than poking at the Grid's variables

Posts: 4
Joined: Mon Jun 05, 2017 9:08 am

Please be positive and constructive with your questions and comments.