0 votes

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.

var byte:Byte = 00000000 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]

Godot version 3.4.2
in Engine by (659 points)

2 Answers

+1 vote
Best answer

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.

by (272 points)
selected by
+1 vote

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.

by (840 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.