So I created a custom Game Instance and I am trying to utilize it elsewhere. It fails to compile. I’m not sure if I have not set up my Game Instance correctly, or if my attempt to use it elsewhere was wrong. For reference, I am working off of the PuzzleGrid C++ example using UE 4.7.6.
The compile error:
Error D:\Programs\Epic Games\4.7\Engine\Source\Runtime\CoreUObject\Public\Templates\Casts.h(135) : error C2664: 'BCInstance *TCastImpl<From,To,UObjectToUObject>::DoCast(UObject *)' : cannot convert argument 1 from 'UGameInstance *' to 'UObject *'
Info with
Info
Info From=UGameInstance
Info , To=BCInstance
Info ]
Info Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Info D:\Unreal\BalloonCollector\Source\BalloonCollector\BalloonCollectorBlockGrid.cpp(26) : see reference to function template instantiation 'To *Cast<BCInstance,UGameInstance>(From *)' being compiled
Info with
Info
Info To=BCInstance
Info , From=UGameInstance
Info ]
Info BCInstance.cpp
Instance CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "BalloonCollector.h"
#include "BCInstance.h"
BCInstance::BCInstance()
{
// 0 = Mansion, 1 = park
view = 0;
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<UStaticMesh> TileMesh;
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> ParkTileMaterial;
ConstructorHelpers::FObjectFinderOptional<UStaticMesh> ParkBenchMesh;
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> ParkBenchMaterial;
FConstructorStatics()
: TileMesh(TEXT("/Game/Park/Mesh/tilerounded_ao.tilerounded_ao"))
, ParkTileMaterial(TEXT("/Game/Park/Material/MI_Tile_Park.MI_Tile_Park"))
, ParkBenchMesh(TEXT("/Game/Park/Mesh/bench_ao.bench_ao"))
, ParkBenchMaterial(TEXT("/Game/Park/Material/MI_Bench.MI_Bench"))
{
}
};
static FConstructorStatics cs;
TileMesh = cs.TileMesh.Get();
ParkTileMaterial = cs.ParkTileMaterial.Get();
}
BCInstance::~BCInstance()
{
}
Instance header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
class BALLOONCOLLECTOR_API BCInstance
{
public:
int view;
class UStaticMesh* TileMesh;
class UMaterialInstance* ParkTileMaterial;
BCInstance();
~BCInstance();
};
Segment of file attempting to utilize the Game Instance
void ABalloonCollectorBlockGrid::BeginPlay()
{
Super::BeginPlay();
BCInstance* gi = Cast<BCInstance>(GetGameInstance());
// Number of blocks
const int32 NumBlocks = Size * Size;
// Loop to spawn each block
for(int32 BlockIndex=0; BlockIndex<NumBlocks; BlockIndex++)
{
const float XOffset = (BlockIndex/Size) * BlockSpacing; // Divide by dimension
const float YOffset = (BlockIndex%Size) * BlockSpacing; // Modulo gives remainder
// Make postion vector, offset from Grid location
const FVector BlockLocation = FVector(XOffset, YOffset, 0.f) + GetActorLocation();
// Spawn a block
ABalloonCollectorBlock* NewBlock = GetWorld()->SpawnActor<ABalloonCollectorBlock>(BlockLocation, FRotator(0,0,0));
// Tell the block about its owner
if(NewBlock != NULL)
{
NewBlock->OwningGrid = this;
NewBlock->BlockMesh->SetStaticMesh(gi->TileMesh);
NewBlock->BlockMesh->SetMaterial(0, gi->ParkTileMaterial);
}
}
}
Sorry if this problem seems simple, I am a novice!