How do I access functions and variables of a c++ class from a blueprint?

I have just recently installed U4 version 4.7.2 and I have been watching tutorials.
I am trying to make a c++ class and make a blueprint with that class and have access to some of it’s variables and functions. I did google search for the answer and found many yet none of them worked.

is there currently a problem with U4? not even the examples are working for me?

the only thing I have managed to do is make a variable appear in the details tab in the blueprint editor.

please help!

There are two things to do:

  1. Create a class with a function on it.

Here’s a simple ‘static’ function that has no state:

#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Foo.generated.h"

UCLASS()
class HELLOWORLD_API UFoo : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

	public:
          UFUNCTION(BlueprintCallable, meta=(
            HidePin="WorldContextObject",
            DefaultToSelf="WorldContextObject",
            Keywords="Hacks Debug"
          ), Category="DebugHacks")
          static int32 GetHappyMessage();  
};

Notice that the function is public, and static. This means it can be run without having to be on a component (ie. statefully).

Notice that is has a WorldContext the defines the ‘target’, otherwise the blueprint editor is spaz out.

#include "HelloWorld.h"
#include "Foo.h"

int32 UFoo::GetHappyMessage()
{
  return 100;
}

Now compile.

Now reload the editor. That’s right. Close and exit the editor, and restart it. The hot reload does not work for blue print functions.

Now open a blue print, right click and toggle off the ‘context specific’ tick box in the top right.

Search for ‘GetHappyMessage’ and you should be able to drop it in here.

Once again, the really important parts to remember here are:

  • Your function must be ‘static’ and ‘public’
  • You need to tag your function with UFunction(BlueprintCallable…)
  • You must provide meta data, including at least 'DefaultToSelf=“WorldContextObject”
  • Obviously at this point your code should compile
  • You must restart the editor to have it work in the blue print editor

Yes, writing blue print functions from C++ is not particularly great.

Be prepared to have the editor crash if you don’t restart it often enough.

Hot Reload should work with most of the UFUNCTION /UPROPETY Macros.

It was improved the last few versions. I only got problems with hotreloading contructors.

Also for a basic c++ class you don’t need to make the function static.

If he just extends his PlayerCharacter or something, he can use a normal function.

The most important thing is the "UFUNCTION(BlueprintClassable, Category=“Test”) part. (You need a category if you have a Function/Variable exposed to BPs)

This is the least thing he needs to create a function that is callable in BPs. There are alot more ways to create functions for BPs. Look up the UFUNCTION properties. You could, for example, create a function that you need to define in BP and that has no implementation in c++, etc.

Hello,

welcome to the Epic('s) Unreal Engine 4 :slight_smile:

For Functions use this f.e.:

UFUNCTION(BlueprintCallable, Category = MyCategory)
   void MyNewFoo(int abc);

For Properties/Variables use this:

UPROPERTY(BlueprintReadwrite, Category = MyCategory)
    int myVar;

These are the most basic forms of accessing c++ code from Blueprints though, you might want to take a look at shadowmint’s / eXi’s answer/comment for more in-depth ideas :slight_smile:

thanks to everyone who responded, you have no idea how much you have helped me!

I was not closing the editor to update changes, that could be the mayor source of my confusion. I suspected I needed to re compile but closing the editor seemed a little execive.

I also did not make my function static. and I did manage to make a variable appear in the detail tab, witch confused me as to why I was not able to modify it from the blueprint.

I will try out everything you have told me very soon.