Looking for better way to play with Bitmask

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 :thinking:) 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
class BitmaskBase
{
public:
  void SetFlags(int32 Initializer);
  bool HasAllOf(int32 Model);

private:
  int32 Flags;

};
BitmaskBase.cpp
#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
// 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
#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

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

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


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
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.

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:

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.

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

to :

image

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 :thinking:

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)

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