How to edit C++ variables in runtime using blueprints?

I started C++ in Unreal Engine yesterday and made a function to replace the blueprint version of it.


This is the blueprint function I am trying to replace with C++ code.

Below is the entire code(cpp, header) I made

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "UpdatePositionPawn.generated.h"

UCLASS()
class CTEST_API AUpdatePositionPawn : public APawn
{
	GENERATED_BODY()


public:
	// Sets default values for this pawn's properties
	AUpdatePositionPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;



	//Function #1: UpdatePosition. If other functions need to be added, add it right below this function.
	UFUNCTION(BlueprintCallable)
		void UpdatePosition();




public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;


protected:
	UPROPERTY(BlueprintReadOnly, Category = "Position")
		float Drag=0.25;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Position")
		float CurrentSpeed=7500;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Position")
		float ThrustSpeed=7500;

};

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


#include "UpdatePositionPawn.h"

#include "Kismet/KismetMathLibrary.h"
#include "Math/UnrealMathUtility.h"
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
AUpdatePositionPawn::AUpdatePositionPawn()
{
 	// Set this pawn 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 AUpdatePositionPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

// Called to bind functionality to input
void AUpdatePositionPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

void AUpdatePositionPawn::UpdatePosition()
{
	
	AddActorWorldOffset(UKismetMathLibrary::SelectFloat(ThrustSpeed, UKismetMathLibrary::FInterpTo(CurrentSpeed, ThrustSpeed,GetWorld()->DeltaTimeSeconds, Drag), (ThrustSpeed <= CurrentSpeed)) * GetWorld()->DeltaTimeSeconds * GetActorForwardVector(), true);
}


The UpdatePosition function right above is equal to all the blueprints in the image.
Values of ‘CurrentSpeed’ and ‘ThrustSpeed’ should be edited in runtime by any other blueprints


Like this.

But even I wrote EditAnywhere, BlueprintReadWrite in UPROPERTY, the value of each variables aren’t changing from the first 7500(I tried public instead of protected for the variables and it didn’t work).

Why can’t I change the values by blueprint in runtime?

The parameters should update with no problem. Your blueprints parent is your c++ class right?

What is connected to your finterp function?
Is it driven by a tick function or timeline?

2 Likes

Yes the C++ class is set as parent of an actor, and the image with the finterp is part of a ‘switch on int’ running every tick. So that is connected with the tick event and also the ‘UpdatePosition’ made by C++ is too.

Oh I figured it out it was my mistake sorry
I had to set CurrentSpeed before addActorWorldOffset like this because FInterp uses CurrentSpeed and ThrustSpeed as current and target.

void AUpdatePositionPawn::UpdatePosition()
{
	CurrentSpeed=UKismetMathLibrary::SelectFloat(ThrustSpeed, UKismetMathLibrary::FInterpTo(CurrentSpeed, ThrustSpeed,GetWorld()->DeltaTimeSeconds, Drag), (ThrustSpeed <= CurrentSpeed));
	AddActorWorldOffset(CurrentSpeed * GetWorld()->DeltaTimeSeconds * GetActorForwardVector(), true);
}
1 Like