[C++] Getting a Reference to a Component in the World

I’m getting started with C++ in UE4, with a strong background of blueprinting. I’m trying to make a simple sky sphere actor with a function that allows it to change the colour of the exponential height fog component in the world. Doing this in blueprints was really easy. I just made an exponential height fog component object reference and checked ‘Instance Editable’. This, with the eyedropper, is what I want to achieve in C++.

So far, this is the closest I can get right now. It shows up in the details panel but only seems to be able to get a reference to the class type, I think? Whatever it is, it doesn’t let me get an instance of the component. No eyedropper.

I am declaring the variable in the sky sphere header file like this:

UPROPERTY(EditAnywhere)
	UExponentialHeightFogComponent* WorldFogReference;

Though I have also used `TSoftObjectPtr, to no avail. I feel like I’m close, but I’m clearly missing something. Any insight would be appreciated.

Many thanks.

All actors you place if they are not blueprint and not some level editor construct they are C++ classes which you can normally refrence in C++ like anything else. The name of the class is same as actors, just remove spaces and A at beginning, you can google class name to verify UE4 API reference results come up quite easily, so in you case it is AExponentialHeightFog

Of corse you need to use TSoftObjectPtr if you want to reference anything level from blueprint class defaults. But there a lot more other ways to do this so you don’t need to hard code something on single level in your project:

  • More automatic way, using TActorIterator to search AExponentialHeightFog on BeginPlay and save reference to variable, code will find it it self so need to setting anything A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums
  • More manual way by creating your own AWorldSettings with AExponentialHeightFog* property and set it as world settings of you project (just place normal UPROPERTY with EditAnywhere and thats it), then you can see that property in World Settings and you can set anything that is the level in it. World Settings same as LEvel Blueprint is saved with the level so you would set that property on every level, you might use World Settings for other things, not much people know about custom World Settings ;] Then you can get world settings from () UWorld::GetWorldSettings | Unreal Engine Documentation
  • More hackish way, use the fact that AGameMode or APlayerController is actually actor to and you can put components on it like UExponentialHeightFogComponent, the AExponentialHeightFog is just dummy actor to contain that component for blueprint only users and level editors, but you can put it in any other actor and it will work the same.
  • Or don’t do anything of that and just spawn it in C++ so you got reference right away, you can do that in blueprint too ;p

Two last to methods of corse require you to remove that actor from the level

The AExponentialHeightFog is exactly what I needed! I’m still getting to grips with prefixes but I knew A meant actor. It did cross my mind that an AExponentialHeightFog would better suit my needs, but for some reason I concluded that there surely wouldn’t be one if a UExponentialHeightFogComponent existed. Shame on me for making that assumption. Thank you very much.

You’re right - I could have used an iterator to find the fog and not have to store the value, but I quite like keeping a reference to a specific one. If I were to ever have a second fog actor, I’d want to know which one I’m referring to at any time. The iterator is a bit of a black box in that regard and I don’t like giving up that kind of control. Less of an issue in this case, I grant you, but it’s a little convention of mine.

Your other solutions are perfectly suitable as well and in fact a couple of them had already crossed my mind, but as I learn new things, I like to solve an issue I encounter rather than find an entirely new approach and leave my confusion unaddressed. In this case, simply knowing which class I could be referencing was all I needed.

Many thanks! :smiley: