Godot Version
4.2
Question
Hi everyone, I’m new to Godot. I am currently working on a school project that is extremely important to me. if I were not able to complete it I would also make the work of my team useless.
Our project is a first-person point and click game (like rusty like games). There are several scenes, each with their own areas 2d. Each area2d has different effects, and some of these, if clicked, take you to other scenes. This image is an example of a typical scene from my game:
the 2d areas that are on the doors and at the bottom of the screen take you to other scenes. The whole scene is managed by a script, which manages the various clicks made on the scene. For example, the following image is a part of the script for this scene, which manages the transitions from this scene to another when I click on areas 2d (if you need images of the entire script I will attach them without problems):
Now: The problem I’m currently having concerns change_scene_to_file(), basically it sometimes (pretty rarely) crashes with the error: “cannot call the method ‘change_to_file’ on a null value”. Searching online I understood that it is an error that happens when the script is asked to change multiple scenes at the same time, in my example double clicking in the back area (i.e. the 2d area below) or in the doors areas (the one on the left, and the one on the front) would generate a crash due to the fact that the scene tries to change twice, but the second time fails because get_tree() returns null.
The point is that when I start the transition with the
TransitionScreen.transition()
a ColorRect appears on the screen which, from what I can see, blocks mouse input. So basically during the:
await TransitionScreen.on_transition_finished
the mouse inputs should be blocked.
Below I attach the image of the transition_screen scene and the related script
However, yesteday i noticed that this crash occures every time I:
- Keep the right mouse button pressed
- Click on the area2d that bring me to another scene with the left mouse button (keeping the right mouse button pressed)
- Click another time on the same area2d (keeping the right mouse button pressed)
So somehow holding down an input nullifies the effect of ColorRect and allow me to double click the area while the fade in/out effect of the transition script is running (basically during the await).
As I said, the game crashes every time this is done, however I’m not one hundred percent sure that this is the only behavior that can negatively affect the scene change, and honestly I have no idea how to understand exactly what generates the error Cannot call the method ‘change_to_file’ on a null value
What I tried:
Yesterday, noticing this behavior, and following some advice given to me on discord, I tried to insert a change to the scene script, here’s what it consists of:
That boolean is a variable that is setted to false when the transition animation is ended (you can see it in the transition_scene script).
So, this seems to resolve the issue of keeping the right mouse button pressed while clicking with the left mouse button, but i have still some doubts
What i need to know:
do you think that checking that boolean can actually completely eliminate crashes due to double recorded input? Is there a safer solution? Im so desperate because i don’t know if every time the project has crashed with the error “cannot call the method ‘change_to_file’ on a null value” was because i misclicked the right mouse button while pressing the left one.
As i said this project is really important to me. If anyone is available, a call on discord is also ok for me so that I can explain the project more completely