Strange behaviour using _on_maze_body_exited(body)

This is the code I’ve used:

func _on_maze_body_exited(body):
	get_tree().call_deferred("reload_current_scene")

The issue I’m having is that when I keep pressing down any of the keys I have assigned to make the space ship move, if it touches any of the “line” of the polygon maze that I’ve made, the game “resets itself” and returns me this error:

E 0:00:02:0768   reload_current_scene: Parameter "current_scene" is null.
  <C++ Source>   scene/main/scene_tree.cpp:1435 @ reload_current_scene()

If I don’t keep pushing down the "movement keys while I cross the lines the game does not give me any errors.

This is the image of my maze and lines I’m talking about:

I’m sorry for my english if my problem isn’t clear enogh let me know what’s not clear and I’ll do my best to explain it better.

Thanks in advance!

This is all very odd - multiple students have been having different issues with this part of the course, and none of them have been straightforward.

In your case, there appear to be two separate problems:

  • maze_body_exited seems to be getting called when touching internal lines (unexpected behaviour) as well as external lines (expected behaviour). This is what your diagram shows, based on what you have identified as a line - could you please confirm that this is the case? For example, that blue quad that the player is currently on - this error will occur regardless of which of the 4 edges your cross, correct?
  • reload_current_scene() is behaving strangely, and based on what I’ve seen in the other issues, I don’t think this is related to anything you’ve done in your project.

I am assuming you are using Godot 4.2.2. Could you try installing Godot 4.2.1 and running this project there? There shouldn’t be any sort of conversion process needed - I just tested that. Marc suspects there might be a problem with the latest engine release, and I’m quite inclined to agree at this point - let’s see if that helps =)

First of all thank you for the fast reply.

yep you’re correct the problem is exactly that. regardless of which of the 4 edges the space ship cross the _on_maze_body_exited funcion is called. however while it’s correct the it is called on external lines of the maze. it should be called on the internal ones.

the strange fact is that the function get called on the internal lines only when I keep pressing one of the “movement keys” (WASD) and the ship cross one of the internal lines. if I cross the one or more of the internal lines without pressing on the “movement keys” (WASD) thanks to the ship momentum the _on_maze_body_exited funcion does not get called.

right now I’m using the Steam version of Godot which is the 4.2.2.
I’ve also downloaded the 4.2.1 version and loaded the project inside it but unfortunately I keep getting the same error.

here is the whole project, hope it helps:
Speedy Saucer.zip

Thanks again for the help!

Cheers

Well the good news is that I’m able to reproduce both problems on my end in your project. I’ll see if I can figure out what’s going on.

EDIT: Ok, I managed to get it working, using a trick that I learned here when I was making this project:

Here’s what you will need to do:

  • On your CollisionPolygon2D, change its Build Mode from Solids to Segments. What this will do is, instead of creating quad-based collision areas for your player to exit, Segments will create collision lines for your player to enter (or rather, to stay within!)
  • Disconnect the body_exited() signal and connect the body_entered() signal instead. This is because instead of checking for the player leaving a chunk of maze, you are now directly checking if the player touches a boundary line.
  • Cut your code out of the on_maze_body_exited() function and put it in the on_maze_body_entered() function that you just connected. You can delete on_maze_body_exited() now as well, since it won’t be needed anymore.
  • In the player scene, resize your CollisionShape to match the player sprite - not only do you no longer need it to be a tiny dot, but having a CollisionShape that small actually makes the collision unreliable.

After following those steps, both problems should be fixed. Let us know how it goes =)

1 Like

Thanks a lot,
I’ve understood your explanation!
I’ve tested the solution and it worked perfectly.

I have just one more question if you don’t mind:
I would like to understand why the reload_current_scene function returned null.

E 0:00:02:0768   reload_current_scene: Parameter "current_scene" is null.
  <C++ Source>   scene/main/scene_tree.cpp:1435 @ reload_current_scene()

Let me know if I misunderstood how this works:
what the reload_current_scene function does is getting the current scene from the scenetree and after that it reloads the scene.
if the game is running there must be a scene instance, therefore the current_scene should never be null.

Probably I’m missing something XD

Thanks!

1 Like

Pretty much!

Yup. I can’t think of a situation in which this can legitimately be null.

I wouldn’t worry too much about C++ errors specifically. Do read into them of course because they can help you figure out what’s happening in your program, but C++ errors themselves are usually a domino effect caused by something else, and in this case, that would have been the broken polygon. I’m not sure exactly why it took this form, but the error itself is much more relevant to the engine contributors. In short, nothing to worry about =)

Understood!

I learned a lot,
thanks for your help!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms