Crash after using the code below

====================== h file =======================================

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

#pragma once

#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"

UCLASS()
class MYGAMEWITHCPP_API AFloatingActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFloatingActor();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	float RunningTime;
	
};
================================h file ==============================================
============================== cpp file===============================================

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

#include "MyGameWithCpp.h"
#include "FloatingActor.h"


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

// Called every frame
void AFloatingActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	FVector NewLocation = GetActorLocation();
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	NewLocation.Z += DeltaHeight * 30.0f;       //Scale our height by a factor of 20
	NewLocation.X = 30 % (int)RunningTime;
	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);
}

Which line does it crash exactly ?

Maybe (int)RunningTime is 0, so you get a divide by 0?

  1. NewLocation.X = 30 % (int)RunningTime;

I’ve checked and found that there was a division by 0.
No, why did the application crash instead of showing the corresponding error message?

There would be a performance overhead if C++ would check illegal arguments on every arithmetic operation for you during run-time just to give you a softer error reporting experience. That would mean every arithmetic operation would take slightly longer to execute, which is huge when a game engine like UE probably does millions of arithmetic operations per frame.

In many cases you already know the input values for a math operation will be legal, either through mathematical proof, intuition or when using functions guarantee to satisfy certain conditions. So in those cases, you don’t even want any unnecessary checks added by the compiler or engine. :slight_smile: In other cases, you can add your own checks.

Hope that explains!

Thanks :slight_smile: