Cant access variable in blueprint from c++

Hi everybody!
It may be a very small problem but i am a beginner to unreal engine, just need some help.
I created code to find FPS,it runs fine, but when pass these FPS values through UPROPERTY function,into the blueprint for result level , blueprint does not get the variable … I dont know what iam doing wrong. For information this FPS code will run in a different level, and results will be passed to different level blueprint.

thanks in advance

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "MyActor.h"

// Sets default values
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;


// Called when the game starts or when spawned
void AMyActor::BeginPlay()

	FTimerHandle FPSTimerHandle;
	GetWorld()->GetTimerManager().SetTimer(FPSTimerHandle, this, &AMyActor::ShowFrameRate, 1.f, true);

int FPS = 0;
int highestFPS = 0;
int lowestFPS = 100;
int count = 0;
int fhFPS = 0;
int flFPS = 0;

// Called every frame
void AMyActor::Tick( float DeltaTime )
	Super::Tick( DeltaTime );

void AMyActor::ShowFrameRate()
	//GEngine->AddOnScreenDebugMessage(0, 1.f, FColor::Yellow, "FPS: " + FString::FromInt(FPS) + " ms:\n " + FString::SanitizeFloat(FApp::GetDeltaTime() * 1000));
	if (lowestFPS > FPS)
		lowestFPS = FPS;
	if (highestFPS < FPS)
		highestFPS = FPS;
	GEngine->AddOnScreenDebugMessage(0, 1.f, FColor::Yellow, "lowestFPS: " + FString::FromInt(lowestFPS) + " highestFPS: " + FString::FromInt(highestFPS) + "FPS: " + FString::FromInt(FPS) + " ms:\n " + FString::SanitizeFloat(FApp::GetDeltaTime() * 1000) + "seconds are: " + FString::FromInt(count));

	if (count == 30)
		count = 0;
		 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FPSneeded")
	int32 HFPS = highestFPS;
	int32 LFPS = lowestFPS;

	FPS = 0;

You need to declare your property as a class member variable and put the UPROPERTY() macro above it, in the header.

Open up MyActor.h
In the public section (and if you don’t have one, declare a public section).
Add int32 HFPS;
And above that, put your UPROPERTY() macro.

Something like:

class AMyActor : AActor

    AMyActor();  // Constructor

    void BeginPlay() override;
    void Tick( float DeltaTime ) override;
    void ShowFrameRate();
    UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = "FPSNeeded" )
    int32 HFPS;

Now over in your CPP, you ought to:

 : HFPS( 0 ) // Initialize
    PrimaryActorTick.bCanEverTick = true;

// All the rest as is

// Previously line 63, in your answer, change to
HFPS = highestFPS;
// or this.HFPS = highestFPS;  // Whichever notation you prefer.

So you’ve declared it in the Header and called it in the CPP. Now these properties are class member variables, so the Blueprint macro can access them.

giving me error on the initializing in cpp file, “error trailing ‘:’ illegal in base/member initializer”
: HFPS( 0 ) // Initialize

also if i initialize them when we declared the variables in .h file doesnot do the job. Doesnot give me the variable in blueprint


This is a quick example of having a C++ class that can be used for Blueprint:


#pragma once

#include "GameFramework/Actor.h"
#include "ExampleBlueprintActor.generated.h"

class AH501596_API AExampleBlueprintActor : public AActor
	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;

    // Blueprint can read this and user can change / add to default settings
    // Must be created and attached to in C++
    UPROPERTY( BlueprintReadOnly, VisibleAnywhere, Category = "Example" )
    USkeletalMeshComponent *Mesh;
    // Blueprint can read and write to this variable
    UPROPERTY( BlueprintReadWrite, Category = "Example" )
    float ExampleReadWrite;

    // Blueprint can only read this variable (must be set in C++)
    UPROPERTY( BlueprintReadOnly, Category = "Example" )
    FVector ExampleReadOnly;
    // Blueprint can call this function
    UFUNCTION( BlueprintCallable, Category = "Example" )
    void ExampleCalledFunction( );
    // This is a function called in C++ but in Blueprint is an event you can call 
    // so you can do something when C++ does
    UFUNCTION( BlueprintNativeEvent, Category = "Example" )
    void ExampleNativeEvent( );        


#include "AH501596.h"
#include "ExampleBlueprintActor.h"

	PrimaryActorTick.bCanEverTick = true;
    Mesh = CreateDefaultSubobject<USkeletalMeshComponent>( TEXT("Mesh") );
    SetRootComponent( Mesh );

void AExampleBlueprintActor::BeginPlay()

void AExampleBlueprintActor::Tick( float DeltaTime )
	Super::Tick( DeltaTime );

void AExampleBlueprintActor::ExampleCalledFunction( ) 
    ExampleReadOnly.X = FMath::FRandRange( 0.f, 256.f );
    ExampleReadOnly.Y = FMath::FRandRange( 0.f, 256.f );
    ExampleReadOnly.Z = FMath::FRandRange( 0.f, 256.f );
    UE_LOG( LogTemp, Warning, TEXT("ExampleReadOnly: %f %f %f"), ExampleReadOnly.X, 
           ExampleReadOnly.Z );
    UE_LOG( LogTemp, Warning, TEXT("ExampleReadWrite: %f"), ExampleReadWrite );

void AExampleBlueprintActor::ExampleNativeEvent_Implementation( )
    UE_LOG( LogTemp, Warning, TEXT("ExampleNativeEvent!") );

This allows you to see the variables and function / event:


I appreciate the help Kyle, but i already tried them but it’s not any good. Can you please run my code and tell me what i am doing wrong? I am attaching the snapshot of blueprint if that’s of some help.
thanks in advance!

Or does it have to do any thing with the Timer i used? Because i need variables values saved after 30 seconds and that’s where i am using UPROPERTY variables…

From your image, what is “My Blueprint”?
Did you go to C++ Classes in the Content Browser, right click on your C++ MyActor class and say “Create Blueprint Class”? Call it something like “MyActor_BP” so it’s easier to track/follow.

Once you have that, you can then access the C++ member variables within MyActor_BP.
Some of which might not be available at Event Construct time, so you may need to do it on BeginPlay or similar.

Finally, you don’t access the properties in the way you’re thinking. In the Blueprint, on the left you have Functions, Macros, Variables etc. Above them is an “Eye” symbol, click that and show inherited/parent.
Then your “HFPS” variable should appear in the Variable list.
Now drag that do your BP, choose “Get” and connect that to your String Appends second input node.

Do you fully understand object oriented programming design and polymorphism? I mean no offense, it’s just that some of the problems you’re hitting seem to be down to a lack of or misunderstanding of class inheritance and how a BP inherits from a base CPP class.

All of these elements do work, as I do them regularly.
First, you always want to pre-initialize your variables with some value. One main reason is with floats. On some platforms they are initialized with FLOAT_MIN, others FLOAT_MAX, some 0. So if you ever read them without first setting them, you have no guarantees on what value is set.

Declaring them in the Header and then using the UPROPERTY() macro does work, this is entirely how it works throughout the source, so there has to be something more going on.

Can you upload both your .h and .cpp files?
Or at least copy the code up here.

Thanks for reply Jawdy!
1-> I didnot create the blueprint from right click on c++ class then create blueprint, because i needed a widget blueprint class and “My Blueprint” is just a dummy name, just to show you that i cannot see the variables. .
2->This is a new thing with Event construct time and begin play i’ll definitely try that.
3->What you are telling me with the “eye thing”, that is not happening in my blueprint. I cant find variables, another way is to rightclick on blue print and find it through the category name.
4-> I totally understand object oriented prgramming concepts. You are right about , i dont know much about the how a BP inherits from a base CPP class.
5->No offence taken. I have already tried the way you said to initialize, that was providing errors so i just initialized it in a different way, but what concept you told is not working uptil now. I am not saying you are wrong, i am saying i cant find the problem, thats where i need help.

OK, I think I’m understanding what you’re trying to achieve.

We need to get an instance of your MyActor into the world, and then you can freely access the properties you’re looking for. With you wanting to get this via some onscreen UMG, an approach I would take would look something like:

  1. Create/Open your GameInstance (if you don’t have one, these are super useful and I would create one. You could do this same approach in a GameMode, then you’d have BeginPlay node)
    (Be sure to set your Game Properties to use your custom GameInstance (or GameMode). You can override these in the Map settings, too)
  2. Add a Variable. Name it “MyActorInstance” or something better. And change the type to your MyActor class.
  3. Off an Event in GameInstance (your choice), use Spawn Actor from Class, and select your MyActor
  4. Drag in the variable you created in step #2 and choose Set. Connect this up with the output from the Spawner in step #3.
  5. Over in your UMG, right click and search for Get Game Instance.
  6. Drag off this Game Instance node and type “Cast to” and you want “Cast to [Your GameInstance]”
  7. Now off the cast, drag off and search for “MyActorInstance” (or whatever you named it in #2)
  8. Now off your MyActorInstance, you can drag off your “HFPS” property.
  9. Rejoice, as this should work AND you have things set-up for you to do this in the future :slight_smile:

(Also, I was banging my head against my own code that wasn’t working and I think some of my frustrations leaked out in to my comments! Sorry about that)

So there is no direct way to access variables in widget blueprint from c++?
No problem about the frustration… We all do that…

No: Class member variables accessed via an instance (i.e. what we have above)

Yes: Static variables/functions
Something like:

With the first option (members), Blueprints still utilise and inherit from C++, so the C++ approach to programming still applies. In this case, from a base code level, one could not just access a property without first knowing where to look (which class definition; is it static?; if it’s from an instance, where’s the instance?; do you pass the instanced object?).

Hey Asad_kaleem,

What you are asking is possible and I highly suggest that you read the documentation. Here are some links:

Thanks Jawdy!
I am working on creating instance with the help of this tutorial A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums
Now i’ve made SolusGameInstance.cpp and SolusGameInstance.h correctly, but dont know how to use the “int32 InterlevelPersistence” or GetInstance() function in my actor.cpp class. As i told you iam new to unreal engine 4, thats why iam not getting some new concepts.

Thank you that helped!

Just need a little help!