다양한 기록

[ChronoSpace] TActorIterator를 이용한 LabyrinthKey 활성화 (Random Activation of Actor) (ClockworkLabyrinth) 본문

언리얼 엔진/ChronoSpace

[ChronoSpace] TActorIterator를 이용한 LabyrinthKey 활성화 (Random Activation of Actor) (ClockworkLabyrinth)

라구넹 2025. 2. 13. 05:16

일단 유니티 SetActive같은게 언리얼 액터에 없다는게 놀랍다

 

void ACSLabyrinthKeyActivator::SetActorActive(AActor* Actor, bool bActive)
{
	Actor->SetActorHiddenInGame(!bActive);
	Actor->SetActorEnableCollision(bActive);
	//SetActorTickEnabled(bActive);
}

Tick 쓸 거면 주석 해제하고 사용

저 대로 쓰면 액티베이트 조정 됨

 

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CSLabyrinthKeyActivator.generated.h"

UCLASS()
class CHRONOSPACE_API ACSLabyrinthKeyActivator : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ACSLabyrinthKeyActivator();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	void SetActorActive(AActor* Actor, bool bActive);

	void SetLabyrinthKey();

	UPROPERTY()
	TArray< TObjectPtr<class ACSLabyrinthKey> > Keys;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "MaxKeyCount")
	int32 MaxKeyCount;
};

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


#include "Actor/CSLabyrinthKeyActivator.h"
#include "EngineUtils.h"
#include "Actor/CSLabyrinthKey.h"
#include "ChronoSpace.h"

// Sets default values
ACSLabyrinthKeyActivator::ACSLabyrinthKeyActivator()
{
	bReplicates = true;
	MaxKeyCount = 3;
}

// Called when the game starts or when spawned
void ACSLabyrinthKeyActivator::BeginPlay()
{
	Super::BeginPlay();
	
	if ( HasAuthority() )
	{
		SetLabyrinthKey();
	}
}

void ACSLabyrinthKeyActivator::SetActorActive(AActor* Actor, bool bActive)
{
	Actor->SetActorHiddenInGame(!bActive);
	Actor->SetActorEnableCollision(bActive);
	//SetActorTickEnabled(bActive);
}

void ACSLabyrinthKeyActivator::SetLabyrinthKey()
{
	for (TActorIterator<ACSLabyrinthKey> It(GetWorld()); It; ++It)
	{
		ACSLabyrinthKey* LabyrinthKey = *It;

		if ( LabyrinthKey )
		{
			SetActorActive(LabyrinthKey, false);
			Keys.Emplace(LabyrinthKey);
		}
	}

	int8 length = Keys.Num();

	if ( length < MaxKeyCount )
	{
		return;
	}

	//UE_LOG(LogCS, Log, TEXT("SetLabyrinthKey : %d"), length);

	int32 ActivatedKeysCount = 0;
	while ( ActivatedKeysCount < MaxKeyCount )
	{
		int8 Idx = FMath::RandRange(0, length);

		if ( !Keys.IsValidIndex(Idx) || Keys[Idx]->GetActorEnableCollision() )
		{
			continue;
		}

		SetActorActive(Keys[Idx], true);

		++ActivatedKeysCount;
	}
}

활성화 시킬 총량 조절 시 MaxCount 조절하면 된다

단 애초에 개수가 부족하면 return

 

월드에 배치하고

 

랜덤으로 오브젝트가 활성화 되는 걸 알 수 있다