+1

Bitwise Operators

JeBuS 3 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 4

Is there any way to do bitwise operators in Bolt? I came across a bit of code I wanted to move over to Bolt, but I'm having a hard time finding the correct nodes for it.

m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;

It seems that I can't do a multiselect on the RigidBodyConstraints enum.



So how do I recreate this code in graph form?

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
+3

Dont think any OOTB units do this, so for example you could create a static method to use?

static public class BitwiseOperators
{
    static public object BitwiseOr(IEnumerable list,Type type)
    {
        if (!type.IsEnum)
            throw new Exception("Type is not an enum");         int intVal = 0;         foreach(var val in list)
        {
            if (val.GetType()!= type)
                throw new Exception("List item type is not of type "+type.Name);             intVal = intVal | (int)val;         }         return Enum.ToObject(type,intVal);
    }
}

Then:

Go to types and add:


Then in your graph:


This is not efficient by any means, nor necessarily safe , if used for initialisation then all good, but probably not per frame (uses reflection)!  Feel free to improve anyone!

Also:  

m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;

can be just

m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

if you dont want to do all of the above!

+1
Pending Review

Annoyingly, Unity hasn't properly marked the RigidbodyConstraints enum with the [Flags] attribute, which means Bolt can't recognize that you should be able to combine items in it directly from the literal inspector.

Paul's solution is a good workaround for now. I'll switch this to an Idea thread for bitwise operators.

I would find bitwise operators very useful. Especially when checking to see if an object's layer is in a LayerMask.

A "Enum Has Flag" node would be very useful for that specific case indeed