Hello, I’m working on a dice game while I am also going through a Unreal C++ course so I can use what I learn to teach myself further, so please go easy on me for any coding on unreal mistakes.
So I have a Dice Board that I am rolling the dice onto. I am attaching a C++ component to the board that will handle what numbers I have rolled. The dice themselves have box components attached labelled “1,2,3,4…” corresponding to the number on the opposite side of the die, so when the box overlaps the board, I know what number is facing up.
This is the code I have written so far for my scoring/rolling attached to my Board Actor:
my .h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
#include "Components/ActorComponent.h"
#include "ScoringRolling.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FARKLE_API UScoringRolling : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UScoringRolling();
AActor* Board;
TArray<UPrimitiveComponent*> NumbersRolled();
TArray<UPrimitiveComponent*> OnesRolled;
TArray<UPrimitiveComponent*> TwosRolled;
TArray<UPrimitiveComponent*> ThreesRolled;
TArray<UPrimitiveComponent*> FoursRolled;
TArray<UPrimitiveComponent*> FivesRolled;
TArray<UPrimitiveComponent*> SixessRolled;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
Heres my .cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ScoringRolling.h"
#include "PlayerBase.h"
#define OUT
// Sets default values for this component's properties
UScoringRolling::UScoringRolling()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
AActor* Board = GetOwner();
}
// Called when the game starts
void UScoringRolling::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UScoringRolling::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
TArray<UPrimitiveComponent*> UScoringRolling::NumbersRolled()
{
//get dice rolled
TArray<UPrimitiveComponent*>NumbersRolled;
Board->GetOverlappingComponents(OUT NumbersRolled);
for (const auto& Numbers : NumbersRolled)
{
if (Numbers->GetFName() == FName(TEXT("1")))
{
OnesRolled.Add(Numbers);
}
}
return NumbersRolled;
}
Now what happens is, every time I put NumbersRolled() into the Tick or BeginPlay function, the game instantly crashes. I’m positive its something in my function, I’m just not sure what.
Weirdly I’m not getting any errors on compile.