Is it possible to have a variable that is a single byte?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Merlin1846

Long story short I am creating a marching cubes algorithm and I had an idea to instead of using an array of bools to use instead a single byte and access the individual bits in the byte like an array. This seemed like the right idea since you can’t really get much faster than a single byte. But I haven’t found anything on how I could do this. Here’s an example in case you didn’t understand.

print(byte[0])
byte[3] = true
print(byte)

Am I just overcomplicating this? Should I just do this?

var data:Array = [false,false,false,false,false,false,false,false]

:bust_in_silhouette: Reply From: spaceyjase

The smallest datatype in gdscript is int. Use bitwise operators to access the individual bits in the byte range (although you’re wasting the excess storage with a single byte stored as an int, a 64 bit value internally).

If speed is a concern you’ll have to measure and compare in your game. This seems like premature optimisation to me.

:bust_in_silhouette: Reply From: gnumaru

In gdscript it’s impossible to have a single byte variable. All variables are of the same underlying C++ type, Variant, which occupies several bytes in memory (probably 16 bytes or more). BUT, if you use C++ instead of gdscript, you may use whatever type you want, like uint8_t and int8_t.

I agree with spaceyjase that this seem like premature optimization. First you should write a function that contains your implementation (or a whole class if you need several functions) and then you should test performance preferably with real usage scenarios. If your most performance intensive scenario, on your worst intended device, does not fall bellow 30 fps, you should not bother at all. If it does, write other functions or classes following the same interface and compare them.