Godot game freezing randomly

I do have an AnimatedSprite2D on all of my enemies

when or what did it froze again, if you see the pattern

The entire game freezes, it stays on the same frame and no inputs or anything work, after a bit Windows marks it as not responding (tints the window white and adds (not responding) to the window name). That’s why my first thought was an infinite loop of some sorts, as I know they completely freeze the game and stop it from moving to the next frame. But, as I found out by testing all my loops, I have not written an infinite loop.

i mean how it made it freeze, did you recall the last input or if anything interact with anything, or any event or signal fired

also the freeze has no error log on debugger?

As I said, the freeze is not replicable, I have not found a consistent way to make the game freeze, it seems to be at complete random when playing, although as I also said, there are some patterns, like only freezing when the player is moving

No, there is no error or anything.

ok so you will want to check the godot log it’s in appdata

the path looks like
user_name\AppData\Roaming\Godot\app_userdata\project_name\logs

I checked all 5 at the file location, they were all the same saying: “Godot Engine v4.1.3.stable.mono.official.f06b6836a - https://godotengine.org
Vulkan API 1.3.260 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3050 4GB Laptop GPU”

ok now that’s no telling what made it freeze, but freezing can occur without any error because it’s processing something, it shouldnt be an infinite loop, but a very long process can made it so.
how about you add break at each while or just add print() to tag them to see if anything actually repeat, but you said you alrdy done that, hmm

Yeah, I’ve added print statements before and after every while/for/foreach loop, something along the lines of print(classname methodname while/for/foreach loop started/finished), and found that every loop started and finished. Also as I said I waited for 3 minutes, and it was still frozen so I doubt it’s just a really long load.

can you share the movement code?

Sorry for the mess I need to get better at organising.

int direction = 0;
		if(!Shop.Active) { // If in shop, stop the player from receiving movement inputs
			if (Input.IsActionJustPressed("Jump") && IsOnFloor()) {
				velocity.Y = jumpVelocity;
				jumped = true;
				
				jumpSFX.Play();
				jumpParticles.Restart();
				jumpParticles.Emitting = true;
			}
			else if(canDoubleJump && hasDoubleJump && Input.IsActionJustPressed("Jump")) {
				velocity.Y = doubleJumpVelocity;
				jumped = true;
				
				hasDoubleJump = false;
				jumpSFX.Play();
				dJumpParticles.Restart();
				dJumpParticles.Emitting = true;
			}
			
			if(Input.IsActionPressed("PlatformDown")) {
				CollisionMask = 2;
			}
			else if(CollisionMask == 2 && !inPlatform) {
				CollisionMask = 18;
			}
			
			if(jumped && Input.IsActionJustReleased("Jump") && velocity.Y < 0.0f) {
				jumped = false;
				velocity.Y *= jumpStoppingValue;
			}
			
			
			direction = Input.IsActionPressed("MoveLeft") ? -1 : 0;
			direction += Input.IsActionPressed("MoveRight") ? 1 : 0;
		}
		if (direction != 0.0f)
		{
			velocity.X += direction * speed * speedMult;;
			velocity.X *= Mathf.Pow(1.0f - inertia, delta * 10.0f);
		}
		else
		{
			velocity.X *= Mathf.Pow(1.0f - stopInertia, delta * 10.0f);
		}
		
		if(direction != 0) {
			sprite.Scale = new Vector2(direction, 1.0f);
		}
		
		sprite.Rotation = velocity.X * spriteRotateFactor;

Heres the first part that I forgot:

if (!IsOnFloor()) {
	velocity.Y += gravity * delta;
	if(velocity.Y > maxFallingSpeed)
		velocity.Y = maxFallingSpeed;
}
else {
	hasDoubleJump = true;
	jumped = false;
}

i just realized it’s in c#, well um might want to search for random freeze for c# godot too

what i would do from this is, disable a block at a time and test run, might be able to find where block of code is the culprit

I tried but there’s not much for freezes on actual games, you only find posts about the engine freezing.

In what process do you run the code? process, physics_process, or somewhere else? Have you run it with the FPS visible on screen, to see that it isn’t a severe lag that might cause it? (I know this last part is likely, but want to just through out some ideas.)

It runs in physics process, and as I’ve said I waited it out for 3 minutes to make sure it wasn’t just a really long load or something. Also, I have finally found the issue: It was an infinite loop in the part of the code I initially thought.

The reason I couldn’t find it is because, I think, GD.Print() caches all of the prints, and only prints everything AFTER the frame, so if I print at the start of a frame, and the program gets stuck somewhere through the frame, it doesn’t reach the end and doesn’t print anything.

I placed a test infinite loop in the first frame with a print before and after the loop, and nothing printed. I put a more rigorous print around the original loop, and added an infinite loop check to break out of it if it takes too long, and my suspicions were confirmed, the loop tried to go over a million times, before the check broke the loop. Now I have to figure out why the loop is infinite in the first place, because I can’t see any obvious reasons why.

Alright, just fixed it, it was an infinite loop caused by using a wrong value, which would occasionally prevent the loop from ever progressing. As always it’s a small syntax or variable issue.

For future people: GD.Print() (At least in C#), I think, caches all print calls in a frame, and then prints them all at the end of the frame. This makes it impossible for print to catch a problem like this, since it never reaches the end of the frame it never prints. So if you put a print statement before and after a loop, even if it is an infinite loop, you will never get either messages to show as the frame never reaches the end.

1 Like