Accessing blueprint variables through C++

Hey, I’ve gotten fairly far in prototyping my game through blueprints. Now I’ve started rewriting some of the math heavy areas in custom C++ blueprint nodes. This is going well but I haven’t been able to find a way to access variables made in blueprints through C++. Is there a way to get access to the blueprint created variables?

Also, is it a decent idea to make the heavier parts of the blueprints into C++ nodes? Rather than rewriting all blueprints in C++(I’m new to C++, so that’s why I ask). Or will it not help much?

Thanks, sorry for the kind of confusing questions.

You can access your blueprint variables from C++… but it’s tedious. I would instead suggest that you move these variables to native instead. The you won’t accidentally rename a variable that C++ code use.

It totally makes sense that you rewrite your calculation heavy stuff to C++, and just expose that functionality as nodes, so that you keep your overall logic in blueprint. It’s really easy to do in C++.
Also, it helps you keep your blueprints more clean, so it’s more logic (where blueprints excel), and less math.

Cheers,

Thanks a bunch . How do I go about accessing the blueprint variables through C++ though? What is this tedious method you speak of. o.O

This code snippet:

static const FName MyProperty( TEXT("MyInt32Variable") );

UClass* MyClass = GetClass();

for( UProperty* Property = MyClass->PropertyLink; Property; Property = Property->PropertyLinkNext )
{
	UIntProperty* IntProperty = Cast<UIntProperty>( Property );
	if( IntProperty && Property->GetFName() == MyProperty )
	{
		// Need more work for arrays
		int32 MyIntValue = IntProperty->GetPropertyValue( Property->ContainerPtrToValuePtr<int32>(this) );
		
		UE_LOG( LogSteve, Log, TEXT("Value is = %i"), MyIntValue );
		break;
	}
}

will print a blueprint variable named “MyInt32Variable” that is of type Integer to the log in your class.

And it won’t have any compile time checking, so if you change your type or name of the variable, it won’t find it. So it’s very sensitive to changes in the blueprint.

Cheers,

3 Likes

Sweet, I assume this is to be used inside of the blueprint c++ file with the variable. How would I find the blueprint with the variable from a different c++ file?

Sorry for the newb questions, it took me a couple hours of experimenting to find out that much. :smiley:

I’m trying to make a BP node with this inside of it. The function not being static causes some weirdness also. But it gives errors with it being static.

.h

UFUNCTION(BlueprintPure, meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", FriendlyName = "My Print Var", CompactNodeTitle = "PrntVar", Keywords = "My Print Var"), Category = Custom)
	void MyPrintVar(int32 &ints);

.cpp

void UMyEdGraphNode::MyPrintVar(int32 &ints)
{
static const FName MyProperty(TEXT(“MyTestInt”));

UClass* MyClass = GetClass();

for (UProperty* Property = MyClass->PropertyLink; Property; Property = Property->PropertyLinkNext)
{
	UIntProperty* IntProperty = Cast<UIntProperty>(Property);
	if (IntProperty && Property->GetFName() == MyProperty)
	{
		// Need more work for arrays
		ints = int32(IntProperty->GetPropertyValue(Property->ContainerPtrToValuePtr<int32>(this)));

	}
	else
	{
		ints = 2;
	}
}

}

you might want to check out Unreal Engine 4 C++ Version 4.5: Weapon Essentials 1 Part 1 - YouTube it’s a tutorial about creating a blueprint for a weapon and creating variables from C++ to the blueprint for real time use. I’m creating classes and them plugging them to a blueprint for easy orientation of the objects ect. I then make the action/gameplay stuff via C++

Hey Spazchicken, I’m actually trying to go the other way. Getting/Editing a variable from blueprints in C++.

Either way I’ll watch your tutorial, no doubt I’ll learn something. Thanks!

like what?

Making a variable in blueprints and then editing it in C++.

ohh you mean through the visual scripting , I don’t know about that, sorry :frowning:

I would not suggest going this route! It’s very error prone, and I just showed it as a prof of concept. Only use it in EXTREME situations when you know EXACTLY what you are doing. Going down this path will just be a pain when doing it too much.

I would suggest watching Spazchicken tutorials. And then instead of making a node that changes your Blueprint variables, make a native function that does some fancy heavy operation and returns a value that is then assigned to your blueprint variable. That way you will have much clearer vision in the blueprint and can see how the flow happens.

Thanks for the suggestion , I’ll take it. And thanks for helping out!

。。。well you win. I finish watching the video by using 15minites! embarrassing!