Blueprintcallable helloworld

Hello

Newbie at this, only had UE4 for a few days and trying to learn how to get it to interact with some C classes I already have. (not much of a C programmer either :frowning: )

With blueprintcallable I have read the manual, and numerous posts but still just don;t get it. I think I must be missing something fundamental in my understanding.

Have completed the Helloworld tutorial and have now added a method I want to call from a blueprint, say when the user presses a key,

My latest version (there have been many versions) of helloworld.cpp looks like this

#include "MyProjecttst.h"
#include "helloworld.h"

UFUNCTION(BlueprintPure, Category = "Game", FriendlyName = "getnum2", keywords = "getnum")  void getnum2();
		

Ahelloworld::Ahelloworld(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	MyNumber = 12;
}



void Ahelloworld::BeginPlay()
{
	Super::BeginPlay();
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World tester!"));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(MyNumber));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::FromInt(nummer.getnum()));
	}
}

void Ahelloworld::getnum2()

{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::FromInt(999));
	return;
}

I just want getnum2 to appear in the blueprint editor, under category GAME, or anywhere really. Not sure if its conceptual, programming or blueprints so driving me nuts!

Many thanks for any advice.

Hey,

i don’t have the time to create it exactly like you did, but normaly it looks like this:

Header File of your Class:

#pragma once

#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"

/**
 * 
 */
UCLASS()
class BLUEPRINTPLAYGROUND_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, category = "Game")
	void TestFunction();
	
};

The .cpp file of your Class

#include "BlueprintPlayground.h"
#include "MyCharacter.h"

AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{

}

void AMyCharacter::TestFunction()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::FromInt(999));
}

What you need to do is, create a function with UFUNCTION() header in your .h file:

UFUNCTION(BlueprintCallable, category = "Game")
void HelloWorld();

And then you can define it inside your .cpp file.

Your issue seems to be that you’re putting the UFUNCTION declaration outside the class header. CPP files are not processed by UnrealHeaderTool, and the compiler itself ignores things like UFUNCTION, so the only way for it to have an effect is for it to be placed in a .H file which is processed by the header tool (that is, in a Classes or Public directory). Put that UFUNCTION declaration in your header and everything should be fine.

While I have you, I’ll also point out that for classes which start with a U, A, F, or T prefix, it’s customary to also capitalize the following letter: AHelloworld. The header file should be similarly capitalized, and so should method names. It sounds pointless and pedantic, but I’ve definitely run into codegen-related problems when getting the capitalization wrong.