Controlling component visibility in Unreal Editor using c++

I am working on a weapon configuration system that allows users of the unreal editor to define uniform grid overlays on characters. These grids represent an internal position array that determines the local attachment of weapons to the character in game. The grids can be resized in width and height, cols and rows, in the editor to fit the desired area on the character for weapon attachment. Think of players dragging a weapon over the hardpoint attachment area, the weapon snaps to cols and rows as the player moves the weapon over the hardpoint surface, allowing the player to interactively determine the grid position of individual weapons.

I have implemented an editor module to implement the custom details panel to visualize the grid pattern in the details panel and I’m currently working to implement in-editor visuals that visually represent the grid in the character viewport. I flag individual grid entries through a call to SetVisibility(false) but the specific UStaticMeshComponent that visually represents the grid entry remains visible in the viewport. Only modifications to the grid meshes during construction affect the visual display.

Any ideas on what I need to be looking at to toggle the component meshes to visibility to false in the editor viewport and have the components not de displayed?

[Edit]
Below is the code responsible for hardpoint management.

WeaponHardpointComponent.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Components/SceneComponent.h"
#include "WeaponHardpointComponent.generated.h"

/*
	Class representing weapon attachment sockets on the mech character class
	Users specify an attachment socket, weapon restriction, and weapon attachment type (graphical attachment type)
*/

UENUM(BlueprintType)
enum class EHardpointRestriction : uint8
{
	VE_NONE UMETA(DisplayName = "No Restrictions"),
	VE_BALLISTIC_ONLY UMETA(DisplayName = "Ballistic"),
	VE_ENERGY_ONLY UMETA(DisplayName = "Energy"),
	VE_MISSLE_ONLY UMETA(DisplayName = "Missles"),
	VE_ARTILLERY_ONLY UMETA(DisplayName = "Artillery"),
	VE_BALLISTIC_ENERGY UMETA(DisplayName = "Ballistic/Energy"),
	VE_BALLISTIC_ENERGY_MISSLE UMETA(DisplayName = "Ballistic/Energy/Missle")
};

struct BLACKMARS_API UWeaponHardpointSlotEntry
{
public:
	UWeaponHardpointSlotEntry();

	bool _bEnabled;
};

#define MAX_SLOT_COLS 8
#define MAX_SLOT_ROWS 8

UCLASS(ClassGroup = (MechComponents), meta = (BlueprintSpawnableComponent))
class BLACKMARS_API UWeaponHardpointComponent : public USceneComponent
{
	GENERATED_BODY()

	//Cached static mesh for hardpoint vidual rep
	static UStaticMesh *_editorVisual;

	//Local array holding slot visual static mesh components - Show/Hide desired number of slots out of maximum slots
	TArray<UStaticMeshComponent *> _slots;

	//During construction - Build maximum size representable by individual hardpoint array, during editing show/hide desired slots
	void BuildEditorVisual();
	void AdjustEditorVisuals();

public:	
	/* Class methods and properties */

	// Sets default values for this component's properties
	UWeaponHardpointComponent();

	// Called when the game starts
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

	/* Unreal methods and properties */

	//Currently unused - defines restirction types of individual hardpoint slots
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Slot)
		EHardpointRestriction RestrictionType = EHardpointRestriction::VE_NONE;

	//Tracking variables for current desired dimensions of slot array 
	UPROPERTY()
		int32 MatrixWidth = 4;

	UPROPERTY()
		int32 MatrixHeight = 4;

	UFUNCTION(BlueprintCallable, Category = Weapons)
		FString GetRestrictionType();

	int32 GetCols() { return MatrixWidth; }
	void SetCols(int32 value) { MatrixWidth = value; AdjustEditorVisuals();}

	int32 GetRows() { return MatrixHeight; }
	void SetRows(int32 value) { MatrixHeight = value; AdjustEditorVisuals();}
};

WeaponHardpointComponent.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "BlackMars.h"
#include "WeaponHardpointComponent.h"

UStaticMesh *UWeaponHardpointComponent::_editorVisual = nullptr;


UWeaponHardpointSlotEntry::UWeaponHardpointSlotEntry()
{
	_bEnabled = false;
}

// Sets default values for this component's properties
UWeaponHardpointComponent::UWeaponHardpointComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	bWantsBeginPlay = true;
	PrimaryComponentTick.bCanEverTick = true;

	BuildEditorVisual();
}


// Called when the game starts
void UWeaponHardpointComponent::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}


// Called every frame
void UWeaponHardpointComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
	Super::TickComponent( DeltaTime, TickType, ThisTickFunction );

	// ...
}

FString UWeaponHardpointComponent::GetRestrictionType()
{
	const UEnum *EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EHardpointRestriction"), true);
	if (!EnumPtr) return FString(TEXT("Invalid"));
	return EnumPtr->GetDisplayNameText((int32)RestrictionType).ToString();
}

void UWeaponHardpointComponent::BuildEditorVisual()
{
	static ConstructorHelpers::FObjectFinder<UStaticMesh> BoxVisualAsset(TEXT("/Game/EditorContent/HardpointCube.HardpointCube"));
	if (BoxVisualAsset.Succeeded())
	{
		//Store visual asset in static member
		_editorVisual = BoxVisualAsset.Object;

		float midPointX = (float)MAX_SLOT_COLS / 2.0f;
		float midPointY = (float)MAX_SLOT_ROWS / 2.0f;
		float startX = -midPointX + 0.5f;
		float startY = -midPointY + 0.5f;

		//Generate maximum dimensional array of slots allowed for hardpoint
		int index = 0;
		for (int col = 0; col < MAX_SLOT_COLS; col++)
		{
			for (int row = 0; row < MAX_SLOT_ROWS; row++)
			{
				//Calculate visual position
				float posX = (startX + (float)col)*8.0f;
				float posY = (startY + (float)row)*8.0f;

				FString name = FString::Printf(TEXT("SlotVisual_%d"), index++);
				UStaticMeshComponent *BoxVisual = CreateDefaultSubobject<UStaticMeshComponent>(*name);
				if (BoxVisual != nullptr)
				{
					//Add static mesh to slot array
					_slots.Add(BoxVisual);

					//Attach visual to weapon hardpoint
					BoxVisual->AttachTo(this);
					BoxVisual->SetStaticMesh(BoxVisualAsset.Object);
					BoxVisual->SetRelativeLocation(FVector(0.0f, posX, posY));
					BoxVisual->SetWorldScale3D(FVector(1.0f));
				}
			}
		}
	}
}

void  UWeaponHardpointComponent::AdjustEditorVisuals()
{
	float midPointX = (float)GetCols() / 2.0f;
	float midPointY = (float)GetRows() / 2.0f;
	float startX = -midPointX + 0.5f;
	float startY = -midPointY + 0.5f;

	for (int i = 0; i < _slots.Num(); i++) _slots[i]->bVisible = false;

	int index = 0;
	for (int col = 0; col < GetCols(); col++)
	{
		for (int row = 0; row < GetRows(); row++)
		{
			//Calculate visual position
			float posX = (startX + (float)col)*8.0f;
			float posY = (startY + (float)row)*8.0f;

			USceneComponent *slotVisual = _slots[index++];

			slotVisual->bVisible = true;
			slotVisual->SetRelativeLocation(FVector(0.0f, posX, posY));
			slotVisual->SetWorldScale3D(FVector(1.0f));
		}
	}
}

And an images showing the results in the viewport.

Hey -

Components derived from USceneComponent should have a uint32 bVisible variable. You can find this variable in the SceneComponent.h file. Setting this variable to false will cause the component to not be drawn and effectively hidden in the editor.

Cheers

This didn’t fix the problem. I have updated the original question with the most recent code for hardpoint management.

In the screenshot provided under the Rendering section there is the checkbox for “Visible”. In the editor you can uncheck this box to hide the component in the editor. The bVisible variable is the code representation of this checkbox, so changing this value should change the component visibility. Depending on when you’re calling the function that changes bVisible, this should update the component in the editor. For example, if bVisible is changed in the component’s constructor and then compiled, the default value of the Visible checkbox in the editor will update.

I’m still new to this, correct me if I am wrong. As of 5.1, bVisible doesn’t work or function anymore. You have to use these methods instead:

ComponentName->SetVisibleFlag(true); //sets visibiltiy to true
ComponentName->SetVisibleFlag(false); //sets visibility to false
ComponentName->ToggleVisiblity(); //toggles visibility
ComponentName->ToggleVisiblity(true); //toggles visibility for children
ComponentName->ToggleVisiblity(false); //toggles visibility for parent