Download

C++ Function Call from Blueprint Widget

I can’t get this to work for the life of me… I have a function in an actor class I created, I want to directly call it from a button. I exposed it with UFUNCTION() and tried calling it from OnClick event, but it’s not being called.

Can someone show how to do this? Thank you very much!

Did you specify that it’s callable from the blueprint?



public:
    // For a function that does not manipulate the class itself.
    UFUNCTION(BlueprintPure, Category = "My Functions")
    int GetSomeNumber() const;

    // For a function that does manipulate the object in some way.
    UFUNCTION(BlueprintCallable, Category = "My Functions")
    void SetSomeNumber(int NewNumber);


I tried that, unfortuantely doesn’t work. Here’s what I have in the SomeActor.h


	
UFUNCTION(BlueprintCallable)
void SomePrintFunction();


Looks good…

Now the SomeActor.cpp



void ASomeActor::SomePrintFunction()
{
	GEngine->AddOnScreenDebugMessage(-1, 1000, FColor::Blue, FString::Printf(TEXT("It Works!")));
}


This is what I want to be called.

I created a Blueprint out of my C++ Class. Here’s what I have in the widget blueprint:
a69d4f3120df839c78b27074e7462deec0ee4d49.png

Inside my Blueprint of SomeActor I made a reference (to the blueprint actor) to be passed along to the Widget Blueprint:
5c1ed860a5a527755715af97d0d4b9a4cd40e937.png

However, OnClicked doesn’t call the SomePrintFunction. Perhaps someone can chime in the proper way to do this? I’m stuck and this is an important feature :frowning: My guess it’s not getting the reference. What should I do? Thanks!

You’re calling a “Some Actor” actor to call ANOTHER “Some Actor” just to call the print function. There’s an anomaly in the way you setup your blueprints. Why not just call directly from the reference itself? Delete the “PrintMe” from the first “Some Actor”, and call the print function from there.

Also, you need to set the reference to something in the Widget. It does not have a reference set and you’re essentially calling from a NULL reference.

Is this a world actor or in the player character class?