Need help with Menu and Code?

Godot Version

4.2.2

Question

My question is quite complex, well, it is for me so I am hoping that seeing a code snippet from one of the functions and screenshot that things will become immediately apparent to someone more advanced than me (not very :grinning:). My mind is split a hundred different ways with things I need to fix, so, my mind cannot see what the problem might be. The code is following a tutorial that for some reason or another would not work the way it was, or, could not follow, so, was modified by me sometimes quite heavily.

The screenshot is this:

Now, the menu has been made to deliberately appear for testing purposes. Everything up to this point is fine. I can play the game albeit a bit hard with the menu :grinning:, but, works. The Level Select Button really doesnā€™t do anything at this point. The quit button works fine, however, it is the Retry button doesnā€™t not work at all. I have marked out in the code with comments which code does NOT work.

this is the code:

#----------------------------------------------------------
# --  btn functions on user interface  --
#----------------------------------------------------------
func retry_btn():
	print("game manager - retry button");
	game_over_menu.set_control(false); #does not work
	
	await ui_fade.fade_to_black();
	
	#do restart/retry we need to:
	#1. unload the level
	#2. reload the level with a variables stored in the File.data script
	#first: is easy just queue.free() level1 from memory
	level1.queue_free();
	#second: Next reload same level
	#what a novel idea
	File.data.retry();
	#	turn 'resource' into a node that can be loaded into Godot
	#	aha now we change this and add the level we need
	#   level1 = load("res://Level/level_1-1.tscn").instantiate();
	level1 = load("res://Level/level_"+ str(File.data.world) + "-" +str(File.data.level) + ".tscn").instantiate();
	add_child(level1);
	spawn_player();
	
	#if I do this here: player can't be re-enabled below
	player.set_enabled(false); # turn off player control so roger can't move

	jolly_roger.roger_revive();# revive character - set w/ 3 lives (no it doesn't)
	#added
	ui.collect_coin();		#supposed to set UI coins, does not
	ui.update_skulls();		#supposed to set UI lives, does not
	
	await ui_fade.fade_to_clear();
	#turn controls back on doesn not work
	player.set_enabled(true);
	
	#pass 

So when I hit the retry button is hit it loads the level as specified, but, the UI/HUD does not update, the menu does not disappear and player movement is completely disabled, it doesnā€™t get re-enabled as per the code although the fades do work.

And should I hit the Retry button again the game crashes with this error:

So, Iā€™m lost. Iā€™m almost sure that the problem come with the loading of the level1, but, I do not know enough about this sort of thing.

Any help, any help at all would be greatly appreciated!

Regards.

What are the children of the level1-scene? and what is the parent of the player and the ui?
If you queue_free() a node you also queue_free() all their children

Good question. This are all the contents of the level1 scene and there are a LOT of them especially things like in the decorations:

DO you add the player as a child of level_1?

Sorry, for the late reply, life decided that it really wanted to get in the way ā€¦

This is something that I shouldā€™ve sent as part of the question but totally forgot to mention, there is a ā€œGame Sceneā€, the master scene that controls everything in a general sense that has the player script ā€œJolly Rogerā€ along with a player script donā€™t ask, I didnā€™t write the tutorial, anyway, this adds the level1 scene. Here:

So, what I think is happening, think, is that the Level 1 scene is being destroyed and then when ā€˜reloadingā€™ it is having trouble finding everything, in particular the User Interface also in Level1 (with the prefix ā€˜uiā€™) and that is why things arenā€™t updating and in the end causing the crash. Do you think that this would be the case of the problems. If so, I could load that User Interface into the master scene, with a whole host of other problems. Or, do you think it could be something else :question:

Another thing I have just found it that if I hit the ā€œRetryā€ button first to reload the screen and the the ā€œQuitā€ button I get this error:

To quote Rachel Zegler, weird!

you have to reassign your ui when you load the level again since its also a new ui

I am not sure what you mean by reassign. Would you please explain this a little?

When you queue_free your level, your ui also gets queued_free.
This leads to your reference of ui in your game-scene to be ā€œnullā€.
That means you have to set the value of the ā€œuiā€-property again when you reload the level. e.g.:

	level1 = load("res://Level/level_"+ str(File.data.world) + "-" +str(File.data.level) + ".tscn").instantiate();
	add_child(level1);
    ui = level1.ui

The same is for all the properties you have referencing something in the level1-scene

1 Like

Okay, wow ā€¦ that throws a real spanner into the works!

I tried what you suggested with the code above and the game crashed at that line. Then I went through and changed all the previously set onready vars with this, to ā€˜seeā€™ if it would work:

	level1 = load("res://Level/level_"+ str(File.data.world) + "-" +str(File.data.level) + ".tscn").instantiate();
	add_child(level1);
	#ui = level1.ui;
	
	ui 				= $"Level_1-1/user_interface"
	ui_fade 		= $"Level_1-1/user_interface/Fade"
	health 			= $"Level_1-1/user_interface/health_gauge"
	game_over_menu	= $"Level_1-1/user_interface/game_over_menu"

And no good! I accidentally got hit by an enemy after pushing the retry and the hero code crashed when it accessed the UI code for the health gauge.

So, it looks like I will need to factor out the UI scene and put it in as part of the ā€œGame Masterā€ scene as I call it and see if that works - Iā€™m optimistic that it will - but Iā€™ve been wrong many times :grin:. Otherwise, itā€™s start over from scratch time :unamused:.

Thanks for the help. Without you I would still be scratching my head wondering what to do.

Regards.

1 Like

Theres also the option to not delete the level and instantiate it new, but to have a reset-method that resets everything inside the level

1 Like

Thatā€™s an interesting option - really interesting :exploding_head:. Unfortunately itā€™s a little above my current paygrade. I will be making a backup so if what I am doing fails (still somewhat optimistic), I will be able to come back to it and try and learn a little more about it.

Thankyou greatly for responding. Itā€™s been a big help :sunglasses:.

Cheers.

1 Like

Just to let anyone whose interested know.

I did the conversion of the user_interface scene and script and it went ā€œincrediblyā€ smoothly. There were 22 instances of the scene that needed changing, but, after I found all of them it worked first go. I mean I was feeling somewhat optimistic, but, I thought I would take at least of couple of hours of tinkering.

Anyway, crisis solved.

Regards.

1 Like

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