Editor crash when running Console Command during Play

link text

Hi there!

When running a console command “TestCombat” (A function that I’ve created to test Turn based combat) the editor closes (also to note that the Crash/Report Crash window doesn’t appear).

I’ve uploaded the Report.wer file inside the attached zip, but was also curious if there was a method to find where the crash originated from myself?

Thanks so much!

Hey robert-alphawave,

When you crashed, did you get a Unreal Crash Window? It looks like this:

Also, is your project Blueprint or C++?

Hi!

Nah, the Crash Reporter doesn’t show (I mentioned that above but I think I called it Crash Window, woops).

My project uses a combination of both (it was started from a Third Person template if that helps).
I’ve been following the Building an RPG in Unreal book if that gives any indication.

Is there anything I should look at specifically within either the blueprints or c++ scripts?

Alrighty!

Ok, just ran it

First exception : Read Access Violation
This was nullptr
UserWidget.cpp

void UUserWidget::AddToViewport(int32 ZOrder)
{
  AddToScreen(nullptr, ZOrder);
}

Pressing continue only changes the warning to
Unhandled exception thrown : read access violation
this was nullptr

So this means that somewhere, the Add to Viewport function (I’m guessing when combat enters, since it’s supposed to add the CombatUI to viewport) is throwing.

If you run the project from Visual Studio and re-create the crash, Visual Studio should stop and show you the line of code that is broken, as well as give you the call stack up to that issue.

  • Navigate to you project folder and open, .sln.
  • Press the “Local Windows Debugger” button (on the top menu bar with a green play icon)
  • When the editor is running, recreate the issue.
  • Post again with the line of code that is shown
  • You can find the call stack in the tab called, “Call Stack”

My guess is directly passing a nullptr into the AddToScreen( ) function.

void UUserWidget::AddToViewport(int32 ZOrder)
{
AddToScreen(nullptr, ZOrder);
}

In the AddToScreen parameter here.

You need to get a reference to the Local Player. If you directly pass a nullptr into something, you are going to cause an issue. nullptr is a way to initialize a pointer so it has a value but the program will still stop running if one is used. Pointers need a reference to be used, always.

You can try something like:

AddToScreen(GetWorld( )->GetFirstLocalPlayerFromController( ), ZOrder);

I did a bit of digging and within the GameMode script (RPGGameMode.h) there’s the TestCombat function (the console command I’ve created that’s throwing the error) and within it is a couple of lines of code…

this->CombatUIInstance = CreateWidget<UCombatUIWidget>(GetGameInstance(), this->CombatUIClass);
	this->CombatUIInstance->AddToViewport();

I have a sneaking suspicion that it might have something to do with this.
UCombatUIWidget (CombatUIWidget) is supposed to be the script that exposes a bunch of Blueprint functions for use. Why it’s not being called/pointing to nothing is really puzzling…

(Sorry, I had literally just pressed comment at the same time you replied!)

Hmmm…is there a way to debug where a null pointer’s being passed within the widget blueprints without the engine crashing?
(or at least while the build is running?)

Oh I get that, I figured that the AddToScreen function was a script from the engine itself, so it was a symptom of a larger problem.
I had a look at the definition and it says that the first argument where the nullptr is now should be a Local Player.

Does that mean that it’s lost the reference to the player controller or is that two separate things?

(Apologies if I’m seeming dense about this/missing something. Just jumping into c++ since a few days ago)

Alright, this is partially my fault. I thought this was your code.

So, to begin. I highly recommend not editing any base file, such as UserWidget.cpp. Ultimately, this is up to you but is generally considered a bad idea, especially if you do not know what you’re doing.

Secondly, AddToViewport( ) is what calls AddToScreen( ), so when you call AddToViewPort( ), you are in extension calling AddToScreen( ). With that said, there isn’t a great explaination as to why there is a nullptr being used in the way it is; there are cleaner ways to decide what to do with AddToScreen( ).

Finally, can you tell me what your goal is with what you’re doing? Maybe I can lead you in a better direction if I understand your intent.

Thanks

Right! But the part that I’m confused about is that I never actually call AddToScreen in any of my scripts or blueprints.

I’ve even tried to go and edit the UserWidget.cpp line where AddToScreen is with the edit you posted and just like I thought, UserWidget.cpp was a read-only script and wants me to save a copy rather than that. (obviously as part of UMG)

I searched the entire solution just to be sure I didn’t call AddToScreen anywhere else and it still points to the UserWidget.cpp script.

Oh no no! This has been a giant misunderstanding! haha

Yeah, that’s where I was confused because I’m not calling it and I thought you were suggesting altering the base UserWidget class and generally, that’s a no no.

I totally understand that about the AddToViewport function which is why I’m so confused why this is happening in the first place.

So, just a bit of background, I’m following the Building an RPG in Unreal book and I’ll try and break it down as briefly as possible.

The general idea is to create a turn based framework inside of UE (which I’m attempting to port my current game from Unity to UE, so I’m following the book to get a better understanding on how turn based combat works in c++)

I’ll skip to the AddToViewport function in TestCombat, but the idea of TestCombat is to run a battle sequence as a console command to test delivering damage and spawning Combat UI.

When in TestCombat, it uses the following code to spawn the UI to select an attack and display the players and enemies within the widget.

this->CombatUIInstance = CreateWidget<UCombatUIWidget>(GetGameInstance(), this->CombatUIClass);
this->CombatUIInstance->AddToViewport();

CombatUIInstance is declared in RPGGameMode’s header file as

UPROPERTY()
		UCombatUIWidget* CombatUIInstance;

Now in the Tick function within RPGGameMode’s cpp file, when the battle is won and the CombatPhase is set to CPHASE_Victory it executes the following code

this->CombatUIInstance->RemoveFromViewport();
			this->CombatUIInstance = nullptr;

When it comes to this code:

 this->CombatUIInstance = CreateWidget<UCombatUIWidget>(GetGameInstance(), this->CombatUIClass);
 this->CombatUIInstance->AddToViewport();

This is your UserWidget class, yeah? As in, a class you’ve created that inherits from UUserWidget?

If so, can you change the code to:

if( CombatUIClass )
{
	CombatUIInstance = CreateWidget<UCombatUIWidget>(GetGameInstance(), CombatUIClass);
	if( CombatUIInstance )
	{
		CombatUIInstance->AddToViewport();
	}
	else
	{
		UE_LOG( LogTemp, Warning, TEXT("CombatUIInstance failed to create" ); 
	}
}
else
{
	UE_LOG( LogTemp, Warning, TEXT("CombatUIClass has not been set, check Constructor of MyUserWidget" );
}

Back in the editor, if you do not have the Output Log window open, go to Window->Developer Tools->Output Log.

Then, run your game and keep a eye out in your Output Log. Let me know if either log is showing.

So, so far this code seems to be okay.
However, when playing the game to test out if the player controller (FieldPlayer) data exists, I went inside the Pause menu which displays the current character’s stats and all the details are 0 (HP 0, MP 0, Current Level 0) which leads me to believe that the Player Controller reference is getting lost somewhere.

Inside the pause menu, just as an example, the text field fetches the data of the character like so:

Get Editable Soldier HP Text 0 > Return Node

To the return value of Return Node it retrieves the Character’s HP as

Character Target (which is a reference to Game Character (a script class) > Get HP (which takes an input of Target (Game Character) and output of HP (int) > To Text (int) > Return Value of Return Node

This is a bit of a breeze over what’s happening and what I thinks going on, hopefully that helps

Done!

Ok, so it crashes before I get a chance to look at the Output Log. But I ran a build from Visual Studio again to see if the output change and sure enough, it did!

Exception thrown : read access violation
this was nullptr

ProcessEvent(FindFunctionChecked(D---AROUND_AddPlayerCharacterPanel), &Params);

D—AROUND is the project name that I’ve replaced the obvious profanity here with —. Basically a mess around project. Anyway.

What this seems to be pointing to is a function in the CombatUIWidget header

UFUNCTION(BlueprintImplementableEvent, Category = "Combat UI")
		void AddEnemyCharacterPanel(UGameCharacter* target);

So! Back in the CombatUI blueprint, it’s implemented as

At this point, I am not 100% sure whats going on, as your project is a bit bigger than what I originally thought.

Is there any chance you can zip it up and upload it somewhere?

If not, it will be pretty difficult for me to see exactly whats going on as debugging via screenshots is difficult.

Sorry for the long delay, Australian Time Zone’s and all that!

Yeah that’s no problem, I’m just uploading it up to dropbox now.

Did you want the link here or is there a way to PM you?

Awesome, here it is!