Hey there,
using GDscript for a good amount of time, I had used Boolean so often, but when I use logical Conditions/Statements on it sometimes i had to create a Second Boolean variable as a FLAG to check that the condition only should be active once and soo froth. Instead of create 2 Boolean if there were 3 data values for Boolean like some coding Language I haired “true” , “false” and “maybe“. it must be greate.
Best Regards…
You should use an enum for this, rather than a boolean:
5 Likes
Will this solve a problem(not a grate one) -
1. I have to recreate it again and again and use EnumName.Value
- from a Global script the recreation problem is solved but calling Global.EnumName.Value is one other thing
- One of them is that about memory allocating 4 byte instated of 1 byte is very rough
I’m surprised you’d recreate the same enum often, you should try to make the enum values more relevant to the actual representation, more than YES/NO/MAYBE (unless you are making a 20 questions game). Giving more information about your project and actual issue may help.
If using a class_name you can avoid the global.
Where did you get the 4 vs 1 byte figure? If you are worried about 3 bytes wasted you should check out GDExtensions otherwise you will not be able to optimize memory layout in such a way.
2 Likes
yeah that 1 and 4 byte figure was from c++. i searched how to get enum memory size but no code was working for me.. according to gemini for godot for a enum it’s 8 byte. now thats the problem.
i will check how can i use class_name.
enum newBool = {
True,
False,
Maybe
};
#code i don't want
var condition : newBool = newBool.Maybe;
#code i want
var condition2 : newBool = Maybe;
#what i am asking for
var condition3 : bool = maybe;
i am not making a quiz game, but sometimes i feel like i need something like progress boolean thing.
sure i can use a float and int :
var condition : float = 0.0 # false
var condition2 : float = 0.5 # maybe
var condition3 : float = 1 # true
is this ideal to do or use.
Creating third value for boolean will give us more combination and control over them isn’t it
oh i get it there will be some limitation as godot is built on language like c++ and not in assembly creating something like that might be impossible
Care to elaborate on why you consider this to be a problem?
Your alternatives using int/float, while perhaps workable, are strange and not to many people in their right mind would do this.
The solution you are looking for is, as has already been mentioned, enum.
Also, maybe look up the definition of boolean, the tri-state type you are suggesting is, by definition, not a boolean.
Edit: Actually, using an int here is pretty normal, plenty of people would to that; the float seems bizarre, but if you modelling ‘progress’ of something, a normalised float (0.0 → 1.0) would be perfectly sensible…. context is everything.
1 Like
enum is great for progression, an actual use case could be quest progression where you wouldn’t use “true/false/maybe” but the states of the quest “unknown/accepted/completed” and any other in between.
1 Like
There are no programming languages that have three values for a boolean. You may have heard of SQL (Structured Query Language) which technically allows an “unknown” null value as a third option. You can do this with a boolean value in GDScript. Test for null before testing for true/false in a boolean. If it isn’t set, it will be null. You have to do this separately though, because a boolean set to null evaluates to false if tested for true/false.
Yup. Or…
If you’re only wanting something to happen once, store the value on disk. Get the value from disk, and if it’s null, it hasn’t happened yet. If it’s not null, then it has happened, and you can test separately what actually happened.
In what universe? 1980? Modern processors are 32-bit and 64-bit processors (4 bytes and 8 bytes respectively). That means that something that is only 1-byte actually is accessed slower - so processors (and the languages that use them) pad the 1-byte value with extra bits. So if you are running on a modern computer with a 64-bit processor (i.e. you bought your computer in the last 12 years for sure, but potentially in the last 20 years), you are running a 64-bit processor. Which means a boolean value, which only takes up one bit, actually consumes 8-bytes of memory to be processed as quickly as possible.
But wait, it gets better! A bool value in Godot is actually a Variant under the hood, and takes up 20 bytes. Meanwhile, an enum is an int under the hood, and takes up 8 bytes. Which means using a bool in Godot is less memory efficient than an enum!
You are trying to optimize something that does not need to be optimized. Modern compilers and interpreters (Godot is the latter) are optimized under the hood to actually take whatever you write and make it more efficient than you can on your own using the language. There is not a game in the world you could make where enum values is going to be the memory or processing speed bottleneck over your graphics.
So, instead of trying to solve this optimization non-problem, focus on writing your code so that it is easy for you to read and debug.
P.S. This should not be a forum feedback post.
1 Like