Bitwise operations and binary assignments

Godot Version

4.3

Question

I’ve two questions. Can I assign integers like this’

enum PropertyTruthTable {
        HAS_DEF_ATK      = 0b00000001,  # 1
        HAS_IMPLICIT     = 0b00000010,  # 2
        HAS_PREFIX1      = 0b00000100,  # 4
        HAS_PREFIX2      = 0b00001000,  # 8
        HAS_PREFIX3      = 0b00001100,  # 12
        HAS_SUFFIX1      = 0b00010000,  # 16
        HAS_SUFFIX2      = 0b00100000,  # 32
        HAS_SUFFIX3      = 0b00110000   # 48
}

# these are capitalized, I swear
        
const Plus_Accuracy           = 0b00000001 #1
const Dmg_Phys_Min            = 0b00000010 #2
const Dmg_Phys_Max            = 0b00000011 #3
const Dmg_Phys_Percent        = 0b00000100 #4
const Dmg_Ele_Fire            = 0b00000101 #5
etc...
const Plus_Block_Recovery     = 0b00100010 #34
const Plus_Stagger_Recovery   = 0b00100011 #35
const Plus_Magic_Find         = 0b00100100 #36

Second question

My server sends over bytes that contain item data.
For example:

C# (not Godot)

            List<byte> SomeFunc() {
            ...
            for( int i = 0; i < prefixCount; i++ )
                outgoing.Add( BuildAffix( rand, cLvlDiv20 ) );
            ...
            return outgoing; }

Then I try to read it in Godot like this:

    if (truth_table & Item.PropertyTruthTable.HAS_PREFIX1) != 0:
        var prefix_1 = _data.read_byte()
                var affix_id = (prefix_1 & 63)
                var pow = (prefix_1 >> 6)

Can I do bitwise operations like this in Godot? I remember trying a while back and running into trouble, but I can’t remember specifically what the issue was.

If no on both, does Godot have any way to make a binary truth table?

Yes

And yes

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.