Engine crashing on something pathetic and simple

Hi

I’m not asking for miracles. I have this code inside my gamemode. ULocation represents a location in the world using my custom coordinate system.


void ANocturnalSynergyGameMode::BeginPlay()
{
	Super::BeginPlay();
	print("Outputting: Game Started");

	ULocation* loc = new ULocation(0, 6, 204);
}


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

#include <iostream>
#include <sstream>
#include "NocturnalSynergy.h"
#include "Location.h"

using namespace std;

ULocation::ULocation()
{

}

ULocation::ULocation(uint32 x, uint32 z)
{
	yIrrelevant = true;

	this->x = x;
	this->z = z;
}

ULocation::ULocation(uint32 x, uint32 y, uint32 z)
{
	this->x = x;
	this->z = z;
	this->y = y;
}

uint32 ULocation::getX()
{
	return x;
}

uint32 ULocation::getY()
{
	return y;
}

uint32 ULocation::getZ()
{
	return z;
}

string ULocation::toString()
{
	stringstream ss;

	if(yIrrelevant)
	{
		ss << to_string(x) << ", " << to_string(z);
	}
	else
	{
		ss << to_string(x) << ", " << to_string(y) << ", " << to_string(z);
	}

	return ss.str();	
}

Also, is there a ‘normal’ way to concatenate strings, like


"X is: " + x

because stringstream is hideous, having 4 lines of code, just to replace the “+” symbol.

Try to cast numbers in constructor to uint32 or make constructor that takes plain int32 as well as that might be type UE is providing… Not 100% sure, I’m lazy to test it…

Error? CallStack?

Using std raw types inside UObjects… good luck with that.

Yeah, a couple of things here, some of them were already mentioned by others above me:

  1. What exactly is this crashing on? Can you post an error log or a debug stack trace?

  2. Using the standard template library in Unreal Engine is not really recommended, unless you have no other choice (for example when binding to a third-party library that already uses the STL). Unreal has its own built-in standard library, including FString for string handling. Conversions to and from FStrings are well explained by Rama in this wiki article, and for more complex string formatting you’re expected to use FString::Printf, which works similar to the good old-fashioned C printf.

  3. Is ULocation a UCLASS that inherits from UObject? If so, you’re not supposed to instantiate it with the regular “new” operator, but to use the NewObject<> template instead. This puts it under guard of Unreal’s garbage collector, ensuring it will get cleaned up in due time.

  4. You should take a look at Unreal’s coding standards. It might be a little different from what you are used to from other C++ projects (instance methods and local variable names both start with an uppercase letter for instance), but being stubborn and going against the grain with naming conventions will only cause you more headaches down the road than is worth.

The engine crashes when I click Play, when I include the line


ULocation* loc = new ULocation(0, 6, 204);

When I remove it, the game plays fine. There is no CallStack; it just crashes with no error message.

It turns out that using NewObject<> stopped it crashing. I just really wish you could use custom constructors.

The errors and call stack will be in Visual Studio.

Launch a DebugEditor of your project from Visual Studio and cause the crash to occur - this will give you symbols you need.

You need the errors and callstack to troubleshoot.

Post the contents in this thread if you are still stuck.