RestartGame() working and not working within the same function

Started by EmmaGundersen, Mon 20/01/2025 22:09:08

Previous topic - Next topic

EmmaGundersen

I made a prototype combat system, and after the combat is complete, the game is supposed to restart. There is a "win condition" and a "lose" condition in the restart function, and while the game restarts in the "lose" condition, if you win it plays out all the code except for RestartGame() and then just freezes, preventing any further player input (including using f9 to restart) and you manually have to close and relaunch it.

This is the code in the function:

Code: ags
function EndCombat(int winCondition)
{
  //1=win 2=lose
  combatOngoing=false;
  gCombat.Visible=false;
  character[enemyNum].UnlockView();
  player.UnlockView();
  
  if (winCondition==1){
    
    character[enemyNum].LockView(character[enemyNum].GetProperty("DeathView"));
    character[enemyNum].Animate(0, 10, eOnce, eBlock);
    character[enemyNum].LockViewFrame(character[enemyNum].GetProperty("AttackView"), 0, 0);
    
    Wait(20);
    character[enemyNum].ChangeRoom(0);
    
    player.LockView(TESTVICTORY);
    player.Animate(0, 5, eOnce, eBlock);
    Display("You won!");
    RestartGame();
  }else {
    Wait(10);
    aEnemy_Win.Play(eAudioPriorityHigh, eOnce);
    player.LockView(3);
    player.Animate(0, 10, eOnce, eBlock);
    Display("You died!");
    RestartGame();
  }
}

Now as far as I can tell, I'm not doing anything different between winCondition 1 and 2, but only one of them works. So if anyone can figure it out/knows what's happening inside the engine, please let me know. Potentially this is a bug with the engine rather than an error on my end?

eri0o

Which AGS version? Can you run from the editor and throw a breakpoint so you can follow the code and see if it's looping somewhere?

Spoiler
Other than this, this code looks something done very weirdly, at least I would at least pair each lock view with an unlock view. Wouldn't use function and would use a proper type instead, and also would name the local and global variables with some difference to not mix them up.
[close]

Crimson Wizard

Besides the breakpoints, something that you could try is to comment parts of the code and see if it starts working at some point.
TBH just looking at the code I cannot see anything critically wrong with it.
Maybe we'll need to make a test and try to replicate this, but given it's a part of a larger script, the error could be elsewhere.

Quote from: eri0o on Mon 20/01/2025 22:51:31Other than this, this code looks something done very weirdly

I cannot agree that this code is "very weird"; besides the missing UnlockView it is a typical code style that people write in AGS.
Majority of users don't know why not to use "function", and 95% of code examples on this forum likely were using it.

eri0o

I know at least in 3.6.2 that if no restart point exists it will simply display a message and log an error - this is, if you never called SetRestartPoint(). It should not just lock, so it has to be in something else afaict.

Khris

I assume you're seeing the "You won!" message and can click it away? What exactly happens next? Does the game freeze at that exact point? I.e. can you still see the current room? Or does it fade out then freeze?

EmmaGundersen

Quote from: eri0o on Mon 20/01/2025 22:51:31Which AGS version?

3.6.1.29

Quote from: eri0o on Tue 21/01/2025 01:18:08I know at least in 3.6.2 that if no restart point exists it will simply display a message and log an error - this is, if you never called SetRestartPoint(). It should not just lock, so it has to be in something else afaict.

The Restart Point is set in game_start() and clearly it exists since the game restarts when you lose.

Quote from: Khris on Tue 21/01/2025 10:48:53I assume you're seeing the "You won!" message and can click it away? What exactly happens next? Does the game freeze at that exact point? I.e. can you still see the current room? Or does it fade out then freeze?

Yeah you click it away and then it freezes without fading out the room. Just saw that it seems to do that even if I comment out RestartGame()...



As for the other stuff:
  • I didn't include UnlockView because the game is just a proof of concept + is supposed to restart anyway and I thought it would look neater if the killed character didn't magically stand up in the last second
  • Maybe I'm doing breakpoints wrong but whenever I reach them the executable just crashes

EmmaGundersen

Quote from: Crimson Wizard on Mon 20/01/2025 23:12:49Besides the breakpoints, something that you could try is to comment parts of the code and see if it starts working at some point.

It seems like the error is caused by the enemy character changing rooms! Don't know how I overlooked this when I went through the code before.
I'm not sure why it happens since all the code causing him to do stuff is set not to be called if combatOngoing is set to false. I'll have to hunt around for it. But it does seem my original issue is solved!

Thank you to everyone who replied.

eri0o

QuoteMaybe I'm doing breakpoints wrong but whenever I reach them the executable just crashes

Crashing is definitely not expected. Could you paste the error message you get? What does crashing mean here? This looks like a bug in the engine and it would be nice to be able to reproduce this so it could be fixed. Breakpoints should work and they are meant to be used in the day-to-day debugging of AGS games - though they are much more useful in ags4, where it's possible to also read the values of the variables.

EmmaGundersen

Quote from: eri0o on Tue 21/01/2025 14:21:13
QuoteMaybe I'm doing breakpoints wrong but whenever I reach them the executable just crashes

Crashing is definitely not expected. Could you paste the error message you get? What does crashing mean here? This looks like a bug in the engine and it would be nice to be able to reproduce this so it could be fixed. Breakpoints should work and they are meant to be used in the day-to-day debugging of AGS games - though they are much more useful in ags4, where it's possible to also read the values of the variables.

By crashing in this case I mean the executable goes "Not Responding" after it hits the breakpoint. It's not the engine shutting it down so from AGS I just get "The game engine does not appear to have shut down properly"

Crimson Wizard

I don't think that marking this as "solved" is correct though. It was found that the issue is not caused by RestartGame, but the crashing or hanging is still a bug, and changing character room is a simple action that must not cause such issues, so there's nothing solved yet.

Snarky

But isn't that just normal behavior when the game hits a breakpoint? It hangs because that's what a breakpoint does: make the engine stop at that instruction. You have to step through or unpause it in the IDE to make it respond again.

Crimson Wizard

Quote from: Snarky on Tue 21/01/2025 15:22:14But isn't that just normal behavior when the game hits a breakpoint? It hangs because that's what a breakpoint does: make the engine stop at that instruction. You have to step through or unpause it in the IDE to make it respond again.

Indeed, I was about to post a similar note.

The breakpoints work like this: as soon as the game reaches a breakpoint, the engine will halt, and will remain halted until you order it to run with F5 in the editor. If it would not halt, then that would defeat breakpoint's purpose.


EmmaGundersen

Quote from: Crimson Wizard on Tue 21/01/2025 14:52:55I don't think that marking this as "solved" is correct though. It was found that the issue is not caused by RestartGame, but the crashing or hanging is still a bug, and changing character room is a simple action that must not cause such issues, so there's nothing solved yet.

I think it was just an invalid room number

Crimson Wizard

Quote from: EmmaGundersen on Tue 21/01/2025 15:50:40I think it was just an invalid room number

Non-player characters are allowed to be assigned a non existing room number, even negative ones. That's one of the ways to hide them, and should not cause any trouble.

EmmaGundersen

Quote from: Crimson Wizard on Tue 21/01/2025 16:06:42
Quote from: EmmaGundersen on Tue 21/01/2025 15:50:40I think it was just an invalid room number

Non-player characters are allowed to be assigned a non existing room number, even negative ones. That's one of the ways to hide them, and should not cause any trouble.

Well that's what I thought too. However changing the room number to an existing room fixed the bug.

Crimson Wizard

Quote from: EmmaGundersen on Tue 21/01/2025 16:10:10Well that's what I thought too. However changing the room number to an existing room fixed the bug.

This means that likely there was something else affecting the situation.

Khris

Did a quick test using the same AGS version:

Breakpoints don't cause "Not responding" for me, instead the editor is focused and shows the breakpoint line marked in yellow. I can now step into the script or let it run again.

Sending an NPC to room 0 had no effect; the game just keeps running normally.

RestartGame() works as expected.

Not sure what could be causing this. @EmmaGundersen If you are curious and don't mind putting the source code online, feel tree to do just that and link us to it.

EmmaGundersen

Quote from: Khris on Tue 21/01/2025 18:51:07Not sure what could be causing this. @EmmaGundersen If you are curious and don't mind putting the source code online, feel tree to do just that and link us to it.

What's the best way/place to that?

Khris

Zip the game folder so it's only one file (on Win 11, right-click the game folder and select "Compress to..." -> ZIP file).

Next, upload the resulting zip file somewhere. An easy way without signing up is mediafire: https://www.mediafire.com/
You can also use Google Drive, OneDrive, iCloud, Dropbox etc; you only need to make sure that in the sharing settings you set the option that anybody with the link can download the file.

Finally, paste the link here.

EmmaGundersen

Here's a zip of the game folder

For debug I usually just set the enemy skill to 10 or so so they're easy to beat. The controls are:
Q- High attack    E- High block
A- Middle attack  D- Middle Block
Z- Low Attack      C - Low block
It works similar to Sid Meier's Pirates! if you've played that.

I am aware that the code is a bit inefficiently written (I'm not even sure all the variables are currently even in use), this was mostly my way of figuring out how this type of combat system could be done. So I'm sorry in advance

SMF spam blocked by CleanTalk