The FVector Set operator/Function

I am trying to continue my C++ knowledge, so I am trying to convert a Speed Ball game I created from Udemy into C++. I made an FVector CurrentCheckpoint variable. When I try and use the set function it does work and comes back with the function takes 3 params, but is called with 1 argument. The other problem I am having is that the GetActorLocation is not getting the checkpoint locations when I run the game. I am not sure if I should nest the if statements or not.

Here is my code:

Here is the code from the course I took. I can’t seem to duplicate the end in C++. Up until this point everything seemed to working.

I’m not sure if the forums are glitching again, but this post is currently showing up in the Fortnite / UEFN section of the Unreal Engine forums.

Unfortunately, I don’t know if there is anyone here that would be able to provide you the help that you need.
You might be able to edit the post to set it to a different section of the forum, but I’m not certain, and you might have to create another post.

I don’t want to leave you empty handed, so I’ll try and point you in the right direction with the code - it looks like there are quite a bit of syntax errors within your code and there might be some variables that have not been forward declared or initialized before you have used them. If you navigate to the files listed on the error list (not the ones labelled with .gen, as I assume those are code-generation files) you can head to the line indicated by the parenthesis.
From what I can see on the file that you do show, where you try to reference “SetCheckpoint01”, it looks like it might not have been declared in a header file, though that might be wrong. You can hover over the red underlined text to see the error information.
Learning how to debug your code is half of the battle in development, and I’d recommend that you give a shot in trying to read through the errors & find out what they are referring to - unfortunately, C++ is one of the harder languages to debug, but I trust that you will get it with persistence and time.

Good luck.

Thank you for your help. I have reposted in the Unreal Engine Forums.

I been trying for the whole Japan time(All Day Saturday) its 0225 in Japan and I am about to go to bed. I made a C++ PlayerStart class and trying to reference in a LevelScriptClass but I am getting no where. I will give it another shot when I get up.

You should use assignment “=” not Set.

It’s kind of hard to see what you’re trying to do here. It looks like your Checkpoint01, etc are not defined… generally trying to access persistent objects in c++ isnt a good idea. Try creating a function where you pass it an object and it does the procedure, something like:

UFUNCTION(Blueprintcallable)
void AddCheckpoint(AActor* checkpointactorref);

UFUNCTION(Blueprintcallable)
FVector GetCheckpoint();

Call these from blueprints, then work backwards from there. Start with something super simple (maybe using dummy info) then build from that.

Read over this on how to integrate with BPs :

Thank you I will give it a try. I am still very knew to C++ and Unreal Engine. Thanks for your help.

I would like to thank everyone that has helped me. I fixed the code up some and it working sort of. I can’t seem to set the first checkpoint as the starting checkpoint. Whenever I run the game it randomly selects a checkpoint even though I set the first check point as the current starting check point. In the screenshot of the Blueprint code the current checkpoint it the First Checkpoint.

Is there a reason why my code is randomly selecting check points at the start of the game?
My updated code is below.

There is no reason in the code you posted it will select something ‘randomly’. Make sure your vars PS_01, Ball, etc are defined properly. I’m assuming things are set otherwise you’d get a crash on line 41.

I would set a breakpoint at line 20, run your game to activate the break, then step thru your function line by line to see what is going on.

I found out what was happening. I wasn’t getting the ball->StaticMesh. Kept returning a nullptr on the static mesh. I had to get the player control then cast to the Ball and StaticMesh. The UE_Log became my best friend for two hours. I always ignored using it in projects course I did online. After using it to troubleshoot I will most definitely have to add it to my work flow. As well as learn to use Rider if I am going to pay for it. :rofl: I really do appreciate your help.

Here is my updated code for my project.
void ASpeedBallevelScriptActor::BeginPlay()
{
Super::BeginPlay();

APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(this, 0);
Ball = Cast<ABall>(PlayerPawn);
MyGameMode = Cast<ASpeedBallGameModeBase>(UGameplayStatics::GetGameMode(GetWorld()));
if (MyGameMode != nullptr)
{
	if (PS_01 != nullptr)
	{
		MyGameMode->Checkpoint.Add(TEXT("FirstCheckpoint"), PS_01->GetActorLocation());		
	}
	if (PS_02 != nullptr)
	{
		MyGameMode->Checkpoint.Add(TEXT("SecondCheckpoint"), PS_02->GetActorLocation());
	}
	if (PS_03 != nullptr)
	{
		MyGameMode->Checkpoint.Add(TEXT("ThirdCheckpoint"), PS_03->GetActorLocation());
	}
	
	MyGameMode->SetCurrentCheckpoint(PS_01->GetActorLocation());
	
	if (Ball && Ball->StaticMesh)
	{
		FVector* SetFirstCheckpoint = MyGameMode->Checkpoint.Find(TEXT("FirstCheckpoint"));
		Ball->StaticMesh->SetWorldLocation(*SetFirstCheckpoint);
	}
	
	if (Ball != nullptr && MyGameMode != nullptr)
	{
		FVector* SetFirstCheckpoint = MyGameMode->Checkpoint.Find(TEXT("FirstCheckpoint"));
		if (SetFirstCheckpoint != nullptr)
		{
			Ball->StaticMesh->SetWorldLocation(*SetFirstCheckpoint);
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("FirstCheckpoint key not found in Checkpoint map."));
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Ball or Ball->StaticMesh is null."));
	}
}

}

1 Like