Cannot Access C++ Actor in HUD Blueprint

Greetings. The title says it all. I created the following c++ class:

.h:

UCLASS(BlueprintType)
class BLAH_API ATestActor : public AActor
{
	GENERATED_BODY()
	
public:	

	ATestActor();

	UFUNCTION(BlueprintCallable, Category="Test")
	void TestActorFunction()
};

.c

void ATestActor::TestActorFunction()
{
	UE_LOG(LogTemp, Error, TEXT("CLICK CLICK CLICK CLICK CLICK CLICK"));
}

I then edited the level blueprint and added a test GUI with a button. That works fine. I also placed an instance of my test actor in the scene.

The problem is that I have no idea how to reference the actor that was placed in the scene. If I am in the Blueprint Level editor, I can just drag a highlighted object into the graph and then create an instance of it. That does not work in the HUD blueprint. All I really want to do is call the function when the button is pressed. Simple stuff.

This is how I am starting:

51389-img3.png

Right, so I can see the function, but I can’t figure out get it to target the test actor. I can create an object reference variable for the test actor class, but that doesn’t work:

Can someone please help me understand how to do this? Nearly all the examples I have come across are blueprint-to-blueprint communication.

EDIT:

I was able to get the function to work by iterating through the class:

This works but … it seems rather inefficient and ineffective if I want to reference one specific actor.

One approach would be to specialise your hud class and put a u property of an actor pointer in there that is blueprint accessible.

Another approach i use is to use the level blueprint to push actor references into child actors - not sure how you get hold of the hud actor but can’t be that hard!

You will find yourself often in the situation where you need to get a reference somehow. There are multiple
methods to get them in specific classes. For example line trace, overlap or Blueprint Interfaces.

But for Widgets, you need to think clever. There are two ways i use. Either i create the Widget in the Class that uses the widget, so i can give it a reference directly. Or i get the reference by one of the above mentioned ways.

The Variable you created is just a container for such an actor. If you don’t fill it, it will remain empty and you will get Accessed None Errors (use IsValid nodes to make sure that you only use it when it is not NULL!).

If the Actor is already spawned and you create the Widget in your LevelBlueprint, you can fill the variable at this point.

Create a Reference of the Actor in the LevelBlueprint by selecting it in the World and rightclicking in the LevelBP Event Graph. You can selected something like “Add Reference to …” there. Now, when you create the Widget, you can get the create WidgetObject, and set the Variable that you created in your picture above (“Blah”). I bit prettier way is to click on the Variable you’ve create and select “Editable” and “Expose on Spawn”. Then the variable will appear in the “Create Widget” node as soon as you select the WidgetClass and you can just connect the value you want to fill it with (the reference).

Thank you for the response. Coming from a programming background, I am finding Blueprints to be nonsensical. Obviously I am doing something wrong.

I basically did what you suggested and it did not work. From what I have researched, there is no way to access a variable assigned in the Level Editor from other Blueprints; you have to create some convoluted system.

In the Level Blueprint:

From my understanding, I am filling the “TestActorInBluePrint” variable which I created in the level editor with a reference to the C++ TestActor object which has been placed in the scene. Note that “Expose on Spawn” and “Editable” are checked.

I then go “Create Widget” and get the following:

51815-screen2.png

You can’t see it, but under the “Class” dropdown, I am unable to select or even find the TestActor class. To be honest, I don’t know how a blueprint variable can be turned into a widget.

So I tried the following in the HUD:

The top button is the only way that I have found to actually reference an object. As I mentioned before, I have to loop through all exposed objects of the class “TestActor” in order to find an object. Here, I do two things. First, I trigger a function in TestActor that outputs to the log. That works fine. Second, I assign the TestActor object that is found to the “TestActorBluePrint” variable in the HUD.

Now, the second button (“OkayButton”) is supposed to change the text in the HUD to the stored value in the TestActor object that has been referenced / set by pressing the top button. This, of course, returns the “NONE” error so it is clear that the object is not being set.

Well this seems to work:

I am not sure if this is the correct way of doing it, but … it works. Thank you for your help.

To my understanding it doesn’t look like your node to “set test actor blueprint” is being executed (there is no connection into the left hand white triangle).

You can put breakpoints on nodes and see if they are being hit (and also see values of variables). Right click for options!

I checked out my suggestion below - you can easily get the HUD class from the player controller so a the level blueprint should be able to inject data from the level to the HUD.

Not sure why blueprints would be nonsensical but they certainly can get unwieldy pretty fast. Good thing you have that programming background! :slight_smile: