Open smytheagain opened 2 years ago
Possibly related to issue #19 since I was leaning on the 'Z' key in the battles to speed up reproduction of the issue. I can't remember if the first time I saw it was using the mouse or the 'Z' key. I will replace the AttackCurrentMonster code with that suggested in #19 and report back.
Thank you for sharing that (along with providing all those details).
Thanks Scott. I'm actually a software tester by trade so I tend to throw in detail where I can.
Unfortunately the issue is still present with the modified AttackCurrentMonster code but can take a long time before it presents itself (my character has 195 experience points - that's 39 snake kills haha).
I have tested around a little bit with this issue. One dirty fix is to clear the GameMessages textbox every now and then, to stop it from growing too large in size. Doing so, seems to completely stop the issue from happening.
Code snippet from MainWindow.xaml.cs if you want to test it on your machine:
private void OnGameMessageRaised(object sender, GameMessageEventArgs e)
{
GameMessages.Document.Blocks.Clear();
GameMessages.Document.Blocks.Add(new Paragraph(new Run(e.Message)));
GameMessages.ScrollToEnd();
}
Furthermore I've tried creating a unit test to emulate the player behavior of holding down the attack-button (it does not crash):
[TestMethod]
public void TestALoadOfMonsterKillingForCrashing()
{
GameSession gameSession = new();
// Move player to Herbalist's Garden
gameSession.MoveNorth();
gameSession.MoveNorth();
// Set weapon to rusty sword.
gameSession.CurrentPlayer.CurrentWeapon = ItemFactory.CreateGameItem(1002);
// Check if the weapon was equipped correctly, and make sure there are enemies at this location.
Assert.IsTrue(gameSession.CurrentPlayer.CurrentWeapon.ItemTypeID == 1002);
Assert.IsTrue(gameSession.CurrentLocation.MonstersHere.Any());
// Attack an absurd amount of monsters, to see if it crashes.
// Furthermore; ensure the player moves back to the Herbalist's Garden.
for (int i = 0; i < 10000; i++)
{
while (!gameSession.CurrentLocation.MonstersHere.Any())
{
gameSession.MoveNorth();
}
gameSession.AttackCurrentMonster();
}
// If the player has more than 100 experience points (in reality, it should be in the thousands), the test is concluded.
Assert.IsTrue(gameSession.CurrentPlayer.ExperiencePoints > 100);
}
And one last thing I want to add, if you use a weapon that one-shots the monsters, the issue doesn't seem to appear at all, no matter how much text fills up in the RichTextBox. Also thanks for the tutorials, Scott!
Thanks @FrejGMNielsen
I might check that code out this week. I just started a new project for a client last week, and haven't had much time to look at anything except their code.
In one of the later changes to the code, I added a change that limited the number of messages allowed to bind to the display. I also changed the UI control from a RichTextBox to a FlowDocument. Were you seeing the error with that change, or was it happening with the RichTextBox?
private void OnGameMessageRaised(object sender, GameMessageEventArgs e)
{
if (GameMessages.Count > 250)
{
GameMessages.RemoveAt(0);
}
GameMessages.Add(e.Message);
}
Were you seeing the error with that change, or was it happening with the RichTextBox?
private void OnGameMessageRaised(object sender, GameMessageEventArgs e) { if (GameMessages.Count > 250) { GameMessages.RemoveAt(0); } GameMessages.Add(e.Message); }
It was indeed on an earlier lection using RichTextBox. I imagine your change would've eliminated the issue, but I can't confirm it myself since I am still early on in the lections.
This happens when the player is killed and sent back to the Home location where normally the CurrentMonster in GameSession would be set to null. I believe we have a race condition where sometimes the currentBattle object somehow becomes null before the currentMonster object does and so when we go through the CurrentMonster setter, we hit a NullReferenceException.
Steps to reproduce the behavior: (very intermittent)
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=GameEngine StackTrace: at GameEngine.ViewModels.GameSession.set_CurrentMonster(Monster value) in \Engine\ViewModels\GameSession.cs:line 55