# ConstructorHelpers::FClassFinder outside Constructor

**URL:** <https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [August 23, 2015, 9:53pm UTC](https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509 "2015-08-23T21:53:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_c2afdbeb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c2afdbeb](https://forums.unrealengine.com/u/anonymous_user_c2afdbeb)\
**Post date:** [August 23, 2015, 9:53pm UTC](https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509/1 "2015-08-23T21:53:15Z")

</div>

I need to find UClass and use it in BeginPlay.  
Now I write constructor and save UClass in field:

```auto

AMyHUD::APuzzleHUD()
{
	static ConstructorHelpers::FClassFinder<UObject> widgetClass(TEXT("/Game/UI/GameUI.GameUI_C")); // find class by url
	MyUIWidgetClass = *widgetClass.Class; // save to private field
}

void AMyHUD::BeginPlay()
{
	MyUIWidget = CreateWidget<UUserWidget>(GetWorld(), MyUIWidgetClass);
	MyUIWidget->AddToViewport();
	MyUIWidget->SetVisibility(ESlateVisibility::Visible);
}

```

But I would be more convenient to use FClassFinder in BeginPlay or use as field of class.

1. Why FClassFinder can’t be use in other methods?
2. Why if I write it as field of class:

```auto

private:
	ConstructorHelpers::FClassFinder<UObject> widgetClass(TEXT("/Game/UI/GameUI.GameUI_C"));

```

I get error: _error C2059: syntax error : ‘string’_ ?

---

<div class="post-metadata">

**Author:** ![Druha](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Druha](https://forums.unrealengine.com/u/Druha)\
**Post date:** [August 24, 2015, 11:43am UTC](https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509/2 "2015-08-24T11:43:01Z")

</div>

1. I am not sure why exactly FClassFinder is only useable in constructor, but think it’s for performance.  
If you look inside ConstructorHelpers class, you’ll see, that internally it uses StaticLoadClass function.  
You can use it throughr LoadClass:

```auto

LoadClass<UObject>(nullptr, TEXT("/Game/UI/GameUI.GameUI_C"));

```

1. This is not valid C++ code, because it is not allowed to initialize variables in header. The line you wrote is similar to this:

```auto

ConstructorHelpers::FClassFinder<UObject> widgetClass = ConstructorHelpers::FClassFinder<UObject>(TEXT("/Game/UI/GameUI.GameUI_C"));

```

---

<div class="post-metadata">

**Author:** ![Greg\_G\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Greg\_G\_1](https://forums.unrealengine.com/u/Greg_G_1)\
**Post date:** [August 23, 2019, 4:11pm UTC](https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509/3 "2019-08-23T16:11:44Z")

</div>

If you need to load a UClass\* latently (outside constructor), I found this to work:

 ![TSoftClassPtrExample.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/8/a/8a041555b80ff096822000585ff2a4369f2fb60b.png)

 ![TSoftClassPtrStrings.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/f/3/f3a704edc6ccdd3fb44138a4eb57863f8b0374fe.png)

 ![TSoftClassPtrLoad.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/e/b/ebacb80a42aa2e693107c9c3a363a0945775e911.png)

---

<div class="post-metadata">

**Author:** ![Shmoopy1701](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shmoopy1701/32/1194536_2.png) [@Shmoopy1701](https://forums.unrealengine.com/u/Shmoopy1701)\
**Post date:** [August 23, 2019, 4:35pm UTC](https://forums.unrealengine.com/t/constructorhelpers-fclassfinder-outside-constructor/39509/4 "2019-08-23T16:35:07Z")

</div>

Using the following to load and play a line of dialog on demand. This function is in the PlayerController.

```auto

  FString soundCuePath; // you'll need the path to the sound cue

  USoundCue* chatSound = Cast<USoundCue>(StaticLoadObject(USoundCue::StaticClass(), NULL, *soundCuePath));
  if (chatSound)
  {
     ClientPlaySound(chatSound);
  }

```
