#Changing from main menu to secondary menu broke level loading

79 messages · Page 1 of 1 (latest)

final plover
#

So i am trying to make a level manager that I saw in a tutorial and far as I can tell I followed everything correctly. The main menu shows but the moment I click play it just dies. if any other screenshots or information is needed to help me wrap my head around this let me know.

I have had to watch several different people for different things. Everything was working until I tried to make this LevelManager.

For reference the video I watched for LevelManager
https://www.youtube.com/watch?v=mKlKMBSM2Ss&ab_channel=MuddyWolf

Learn how to build the ultimate 2D platformer in godot with this series! In this episode we learn how to use Godot 4.2's User Interface (UI) nodes to create a Main Menu, we also create a custom Scene/Level Loader to load and unload levels!

First Lesson: https://youtu.be/c5msTt9Jeyg

Source code: https://www.patreon.com/posts/ultimate-2d-in-9567...

▶ Play video
civic inlet
#

We need to see your level data resource code

final plover
civic inlet
#

Not sure that's just what the error message said

final plover
civic inlet
#

I don't see where the actual line the error is on but I'm guessing you skipped a step or renamed a variable

#

Yeah your level data has a level property but not vice versa

final plover
#

so im not sure what to put where to fix it

civic inlet
#

Okay what line is throwing that error about LevelData not having a level property?

#

But yeah the way it's set up currently you can't get the level from the LevelData only it's path

final plover
civic inlet
#

Line 37

#

lvl.level_id not lvl.level.id

final plover
#

Wow. 🤦‍♂️

#

I feel like an idiot

#

Thank you i fought with it for over an hour.

civic inlet
#

It's okay that's how you learn better next time. Not wanting to look silly is a powerful motivator. 😛
But nah I've made similar mistakes. It's a question of learning to read the error and follow through step by step until you squish it.

#

You're welcome. You got this. Now go back to the fun part 🙂

final plover
civic inlet
#

You'll get there. You're very welcome. 🙂

final plover
civic inlet
final plover
#

ok so what I did was move a level loading from the main menu to a new sub menu. I thought that everything was fine.

final plover
#

i copy pasted what i thought i needed. like the deactive stuff and had things done the same way

civic inlet
#

You freed your main scene

final plover
#

im not sure what that means or how i did it

final plover
civic inlet
#

Is game_main_scene an autoload?

#

Or I guess what is it. Can you maybe show me the part where you leave a level?

final plover
#

like when u first start up the game?

#

leave a level?

#

i dont have transitions or anything yet. so far they are all separate

final plover
civic inlet
#

This error says you free the main scene at some point

final plover
#

im just not sure where.

civic inlet
#

You're looking for calls to game_main_scene.free() or queue_free()

final plover
#

game main

#

i have a few queue_free() but those are under func _on_body_entered for killzone, coin, and slime

#

level manager

final plover
#

if you want the ones for coin etc i can get those

civic inlet
#

Can you copy paste LevelManager with the markup?

final plover
#

markup?

earnest prismBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

civic inlet
#

The swift thing

final plover
#

`extends Node

var levels : Array[LevelData]

var game_main_scene : Node2D = null
var loaded_level : Level = null

func unload_level() -> void:
if is_instance_valid(loaded_level):
loaded_level.queue_free()

loaded_level = null

func load_level(level_id : int) -> void:
print("Loading Level: %s" % level_id)
unload_level()

var level_data = get_level_data_by_id(level_id)

if not level_data:
    return

var level_path = "res://scenes/%s.tscn" % level_data.level_path
var level_res := load(level_path)

if level_res:
    loaded_level = level_res.instantiate()
    
    game_main_scene.add_child(loaded_level)
else:
    print("Level does not exist!")

func get_level_data_by_id(id : int) -> LevelData:
var level_to_return : LevelData = null

for lvl : LevelData in levels:
    if lvl.level_id == id:
        level_to_return = lvl

return level_to_return

`

#

like that?

civic inlet
#

Okay when does game_main_scene get assigned? 🤔

final plover
#

bottom 1/3?

#

this is game_main:

`extends Node

@export var available_levels : Array[LevelData]

@onready var _2d_scene = $"2DScene"

func _ready() -> void:
LevelManager.game_main_scene = _2d_scene
LevelManager.levels = available_levels
`

civic inlet
#

Okay we're getting there

#

What's 2DScene?

final plover
final plover
#

basically meant to easier load/unload levels

#

the only change i know of between it working and not working is i made 2 new UI screens and the scripts for them. I moved the level trigger from one script to another, and added a folder for the level and renaimed it. I also edited that info in the array already.

#

I can show file paths if that makes any difference.

civic inlet
#

Nah I'm stumped tbh

final plover
#

I can move a set of code back to see what happens

#

But that code was the deactivate

civic inlet
#

Yeah or show me what code you moved

final plover
#

func deactivate() -> void: hide() set_process(false) set_physics_process(false) set_process_unhandled_input(false) set_process_input(false)

#

and

func _on_knight_pressed() -> void: print("Knight pressed") LevelManager.load_level(1) deactivate()

#

went from main menu to difficulty menu

civic inlet
#

Yeah can't see how that would do it.

final plover
#

i move both those back and it works again...

#

well not the on knight press

#

but yeah

#

the button is a different name

#

main menu now (Works)

`class_name MainMenu
extends Control

func _on_tutorial_pressed() -> void:
print("Tutorial pressed")
get_tree().change_scene_to_file("res://scenes/levels/tutorial_level_00.tscn")

func _on_start_game_pressed() -> void:
print("Start pressed")
LevelManager.load_level(1)
deactivate()

func _on_options_pressed() -> void:
print("Options pressed")

func _on_credits_pressed() -> void:
print("Credits pressed")
get_tree().change_scene_to_file("res://scenes/UI/credits_screen.tscn")

func _on_exit_pressed() -> void:
print("Exit pressed")
get_tree().quit()

func deactivate() -> void:
hide()
set_process(false)
set_physics_process(false)
set_process_unhandled_input(false)
set_process_input(false)`

#

versus main menu not work:

`class_name MainMenu
extends Control

func _on_tutorial_pressed() -> void:
print("Tutorial pressed")
get_tree().change_scene_to_file("res://scenes/levels/tutorial_level_00.tscn")

func _on_start_game_pressed() -> void:
print("Start pressed")
get_tree().change_scene_to_file("res://scenes/UI/difficulty_menu.tscn")

func _on_options_pressed() -> void:
print("Options pressed")

func _on_credits_pressed() -> void:
print("Credits pressed")
get_tree().change_scene_to_file("res://scenes/UI/credits_screen.tscn")

func _on_exit_pressed() -> void:
print("Exit pressed")
get_tree().quit()
`

#

maybe i need to switch it to activate?

#

nope that didnt help

final plover