# Looking for better way to play with Bitmask

**URL:** <https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, Blueprint\
**Created:** [May 17, 2023, 12:41pm UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679 "2023-05-17T12:41:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PnyTrr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pnytrr/32/701859_2.png) [@PnyTrr](https://forums.unrealengine.com/u/PnyTrr)\
**Post date:** [May 17, 2023, 12:41pm UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679/1 "2023-05-17T12:41:38Z")

</div>

Hi, my goal is to add functions to the Native Integer type with Bitmask meta option.

After trying some “things” based on my pure c++ knowledge like Inherite from Integer ( look impossible 🤔) and making a templated UObject (impossible for sure) I decided to make this things that’s work :

> Consider following code as sample with more Base function and more Enum to treat with

> **BitmaskBase.h**
>
> ```auto
> class BitmaskBase
> {
> public:
> void SetFlags(int32 Initializer);
> bool HasAllOf(int32 Model);
> 
> private:
> int32 Flags;
> 
> };
> 
> ```

> **BitmaskBase.cpp**
>
> ```auto
> #include "Bitmasks/BitmaskBase.h"
> 
> void BitmaskBase::BitmaskBase::SetFlags(int32 Initializer)
> {
> Flags = Initializer;
> return;
> }
> 
> bool BitmaskBase::BitmaskBase::HasAllOf(int32 Model)
> {
> return (Flags & Model) == Model;
> }
> 
> ```

> **BM\_Actions.h**
>
> ```auto
> // Fill out your copyright notice in the Description page of Project Settings.
> 
> #pragma once
> 
> #include "BitmaskBase.h"
> 
> UENUM(BlueprintType)
> enum class EActions : uint8 {
> None = 0x00,
> Running = 0x01,
> Jumping = 0x02,
> Falling = 0x03,
> Attacking = 0x04
> };
> ENUM_CLASS_FLAGS(EActions)
> 
> /**
> * 
> */
> UCLASS(Blueprintable, BlueprintType)
> class UBM_Actions : public UObject, public BitmaskBase // There is the trick ...
> {
> GENERATED_BODY()
> 
> public:
> UFUNCTION(BlueprintCallable)
> void SetFlags(
> UPARAM(meta=(Bitmask, BitmaskEnum=EActions)) int32 Initializer
> );
> 
> /** Check present flags */
> UFUNCTION(BlueprintCallable)
> bool HasAllOf(
> UPARAM(meta=(Bitmask, BitmaskEnum=EActions)) int32 Model
> );
> };
> 
> ```

> **BM\_Actions.cpp**
>
> ```auto
> #include "Bitmasks/BM_Actions.h"
> 
> void UBM_Actions::UBM_Actions::SetFlags(int32 Initializer)
> {
> return BitmaskBase::SetFlags(Initializer);
> }
> 
> bool UBM_Actions::UBM_Actions::HasAllOf(int32 Model)
> {
> return BitmaskBase::HasAllOf(Model);
> }
> 
> ```

> **Usage**
>
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/7/8/678a29defed198f5dc055111aa84605256448e34.png)

> The question: Is this a good Unreal logic ? There is another way to achieve this ?

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [June 10, 2023, 6:17am UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679/2 "2023-06-10T06:17:48Z")

</div>

in blueprints themselves you create a int type variable either byte(uint8), or Integer(int32) then in the details with the variable selected

 ![bitmaskInt](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/a/a/4aa96a1a0ff9c7f989c40292a7516f240c812453.png)  
you still need to do your bitwise operations on the variable as expected

for C++ doesn’t require any inheritance it is a subset of UENUM  
you can look here  
[https://forums.unrealengine.com/t/bitmask-warning-spam/1194637](https://forums.unrealengine.com/t/bitmask-warning-spam/1194637)  
for a method that works to declare them, but I am getting a bunch of warning spam even though I looked through quite a few guides, and it works as it should just throws up numerous Warnings.

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [June 15, 2023, 5:19pm UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679/3 "2023-06-15T17:19:37Z")

</div>

Was able to find a C++ method to use enums with more then 8 bits (so up to int32 maybe int64 but that would not be Net Replicatable)  
detailed here:

> [@Problems with making large bitmasks](https://forums.unrealengine.com/t/problems-with-making-large-bitmasks/485436):
>
> Hi, I have been struggeling a lot for a long time today trying to set up a proper working enum to use as a bitmask. I have looked through countless articles and here are my findings… The prefferable way to set up an enum seems to be to add in the class keyword. Though this has to inherit from uint8 which does not support more than a maximum of 8 bits worth of values. Example: So I had to find some alternative. And I shortly after figured out that I could also just drop …

Note: you can only have 1 non `enum class` per file; so if you only really need 1 of these relative to a UCLASS then you can declare it in the same header as the UCLASS, but if you need another you will need to have it declared and included from another file for each of them.

---

<div class="post-metadata">

**Author:** ![PnyTrr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pnytrr/32/701859_2.png) [@PnyTrr](https://forums.unrealengine.com/u/PnyTrr)\
**Post date:** [June 15, 2023, 5:53pm UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679/4 "2023-06-15T17:53:17Z")

</div>

Yeah I maybe I’m not enough explicit. My goal is to transform :

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/3/f/83f51c0db024228e2447e39f0e820c74d5ec79b3.png)

to :

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/1/f/e1f5f37460df458ad8c9ca0ad3b62a4ca949c49e.png)

And this is exactly what my code do, but I have to repeat BM\_\* codes litteraly copy paste that I found ugly and I think is another way.

Saying that, maybe I don’t understand your suggestion 🤔

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [June 21, 2023, 1:53am UTC](https://forums.unrealengine.com/t/looking-for-better-way-to-play-with-bitmask/1168679/5 "2023-06-21T01:53:51Z")

</div>

from your question I thought you were having trouble making bitmasks in general, but I will try for “how to make a function to work with general bitmasks” as I doubt you will only ever have 1 bitmask flag enum.  
you might be over thinking it a bit. A bitmask is just how the integers are treated (we can manually set the bits to be effectively booleans) the data type is just an integral integer. The function just needs to take in 2 integers of the desired type (uint8, or int32) then spit out a bool.

in pure blueprints you could give this function to some reachable object (Game Instance, the player Character, or something else that is relatively static):  
-created 2 Integers (both where set to bitmask)  
-on the Game Mode gave a function that takes in 2 integers, and spits out a bool  
then in the calling exec string need to `Get Game mode` then `Cast to [BP_GameModeName]`

in C++ you could make this a member function to a BlueprintFunctionLibrary (a static non-instance collection of functions)

```auto
// split up as desired
static bool HasAllOf(int32 inTarget, int32 inModel){
    return (inTarget & inModel) == inModel;
}

```
