How can I declare a C++ function, that will be defined in a Blueprint Graph?

How do you declare a function in a C++ class that will be defined on a blueprint graph and not in the .cpp file, but callable from C++?

Hello Mike,

The simple answer is that you will want to add the following above your function:

UFUNCTION(BlueprintImplementableEvent) 

This allows you to define the function in Blueprint, but then call it in C++.

If you need an example, you may want to take a look at StrategyGame. StrategyResourceNode declares OnDepleted, and then the Gold_Test Blueprint defines it, and then it is also called in code.

I hope that helps.

ooooh very nice, thanks for clarifying that Stephen!

I added a mention of this to my c++ basics thread

http://forums.epicgames.com/threads/973803-Tutorials-Entry-Level-UE4-C-Overview-Q-amp-A-gt-Loop-over-Letters-A-Z-add-to-String?p=31715618#post31715618

will flesh it whenever I figure out how to make functions in BPs :slight_smile:

Thanks, this is what i was looking for

Since no one else is replying

My question to you:

If you go through alllll the work of creating the .h and the .cpp and declaring your function

why would you then turn around and write the rest of the code in blueprints?

#Solution

Just write out the whole code in the .cpp and make the function BlueprintCallable in the .h file.

then you can call it C++ and call it in blueprints

/** Screen Shot */
UFUNCTION(BlueprintCallable, Category=VictoryFunctions)
void TakeVictoryScreenShot();

:slight_smile:

#Global Accessibility?

Is the issue that you want to be able to use the function in any blueprint?

then just make a BP function library as I explain thoroughly here (with pics! )

http://forums.epicgames.com/threads/972861-TUTORIALS-C-for-UE4-gt-gt-New-Equivalent-of-PostInitComp-amp-Tick-for-Anim-Blueprints?p=31682322&viewfull=1#post31682322

Rama

I don’t want to do that. I want to make lots of different blueprints extending off this class, with each one overriding this particle function in their own way, and I want that done in a blueprint graph because this function will be working with game-play elements in the level and it’s easier to do that in the Blueprint.

I also want to be able tweak that function in the Blueprint.

But a C++ class will have a container full of these, and will need to call a common function on all of them, such as process or execute, whatever it will be called.

Basically, want them to be forced to Implement an Interface Function in a Blueprint Graph.

This is for AI, so the number of these classes may grow quite large but each will have a unique process inside it’s function which will need world data and be able to call on a AI Controller, that is why I was thinking of doing it partially in Blueprints.

In that case I still recommend using BlueprintCallable

but you can make the function virtual in your base class

and override it in each subclass

then you can even use Super:: if you have common code to run from the super classes!

//in the base class .h
//Platform specific simple set rot speed
virtual void SetRotateSpeed(const float& NewSpeed);

//cpp for base class
void AJoyPlatformBase::SetRotateSpeed(const float& NewSpeed)
{
	RotateSpeed = NewSpeed;
	QuatSpeed = RotateSpeed.Quaternion();
}

//in subclasses
//a specific use
virtual void SetRotateSpeed(const float& NewSpeed) OVERRIDE;

cpp for subclass
void AVictoryCirclePlatform::SetRotateSpeed(const float& NewSpeed)
{
        Super::SetRotateSpeed(NewSpeed);

        //additional code specific to this subclass
}

#C++ Efficiency with Override

this by far the most efficient way to do something as complex as AI where speed is VERY important

and blueprints are compiled as byte code similar to unrealscript

#result

  • it is blueprint callable

  • it varies with each subclass

  • there can be common code with super classes to save you time

#Const efficiency

I made it const & since you are passing it to the super, no need to recreate the var constantly while passing up the super classes

Thank you for your comment. But it isn’t answering the question I asked. I don’t want to call the Function from a BluePrint, I want to declare it in C++ and define it in a BluePrint Graph.

It’s not for optimizing. If there is a performance problem I can stick it into C++ later. It’s for ease of making new Actions, in the Blueprint Graph. That will be called from C++.

" It’s for ease of making new Actions, in the Blueprint Graph"

can you explain this part to me, and exactly what you are doing that you think is better handled by BP than by the c++ / is more convenient?

I cant help you with the BP side of things :slight_smile:

The question is about declaring a function in a C++ class that is Blueprintable, and defining it in the BluePrint, not in the .cpp.

It’s totally fine to not know, I don’t know. That’s why I am asking.

I think it’s easier because I know I’ll be wiring inputs from GameWorld objects and that’s easier on the BP graph. Plus, I just want to do it in Blueprints as I think they will be good for it. I know they are working on a Behavior Tree system that is doing part of if at least through Blueprints so this is similar, but it’s not a Behavior Tree, I’m using a different model.

I suppose my only remaining question, which might help the others who can actually answer your question:

why do you need the c++ declaration at all if you are doing everything else in BP ?

Oh nvm, I see, you want to call it from the c++ as well

I’m not sure if the compiler will like that

a UFUNCTION with no body?

Rama

Maybe I don’t, but I want to call the same function on all the BluePrints so it makes sense that it would be declared, but if an Interface can be made which Blueprints have to Implement so that they can be called from C++ then that should be fine actually.

Hi, I’m interested to read your c++ basics thread, however I get a denied message when trying to read it:

I subscribe to UE4 engine, but it’s not that account on forums.epicgames.com that is used.

Is by any chance this on the wiki the very same?

Conny