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.
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.
AMyActor::AMyActor()
: 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”
AMyActor::AMyActor()
: HFPS( 0 ) // Initialize
This is a quick example of having a C++ class that can be used for Blueprint:
[.h]
#pragma once
#include "GameFramework/Actor.h"
#include "ExampleBlueprintActor.generated.h"
UCLASS()
class AH501596_API AExampleBlueprintActor : public AActor
{
GENERATED_BODY()
public:
AExampleBlueprintActor();
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( );
};
I appreciate the help, 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:
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)
Add a Variable. Name it “MyActorInstance” or something better. And change the type to your MyActor class.
Off an Event in GameInstance (your choice), use Spawn Actor from Class, and select your MyActor
Drag in the variable you created in step #2 and choose Set. Connect this up with the output from the Spawner in step #3.
Over in your UMG, right click and search for Get Game Instance.
Drag off this Game Instance node and type “Cast to” and you want “Cast to [Your GameInstance]”
Now off the cast, drag off and search for “MyActorInstance” (or whatever you named it in #2)
Now off your MyActorInstance, you can drag off your “HFPS” property.
Rejoice, as this should work AND you have things set-up for you to do this in the future
(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)
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?).
Thanks Jawdy!
I am working on creating instance with the help of this tutorial A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community 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.