Edit Blueprint Variable in C++

I have a blueprint with a float variable, SpeedMultiplier.

213361-variable.png

I have created an Actor class and declared a pointer to the Blueprint class.

213362-speedunitmultiplier.png

I want to access the SpeedMultiplier variable through the code (Vehicle object). What can I write to solve this?

Why would you do this in the first place? Why not creating the variable in your c++ class? I can’t think of a single use case where you want to declare variables in the Blueprint and access these BP variables from c++ code. I’m confused.

I’m new to both C++ and Blueprint. The graph for the VehicleBlueprint ([file][1]) that is hard to comprehend. So I figured I could just create an actor class and modify the float variable from the code.

I’m trying to change the SpeedMultiplier variable by code.

You can create the same variable in C++ and expose it to Blueprints like that:

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float SpeedMultiplier;

I have made that change in the code. How do I hook the variable to the Blueprint such that any changes made to the variable in the code reflects to the Blueprint?

Hi teddi
You can use the reflection system, check I made this node for you:

bool UGenericClass::changeFloatInBlueprints(AActor* InActor, FName PropertyName, float value)
{
	UProperty* Property = InActor->GetClass()->FindPropertyByName(PropertyName);
	if (Property)
	{
		float* ptrValue = Property->ContainerPtrToValuePtr<float>(InActor);
		if (ptrValue)
		{
			*ptrValue = value;
			return true;
		}
	}
	return false;
}

is working is this way:

Summary:

As you can see you can access the UClass and later you can find a UProperty* by name, with that you can access the proper pointer and then you can change the value, this is the power from unreal reflection system

Hope this help
Cheers

1 Like

In case you don’t have a name clash now by adding the same variable name in the c++ file, you should just be able to access it in Blueprints like everything else.

I changed the parent class of the Blueprint to the one with the declared variables. That worked for me. Thanks for pointing me to the right direction.