Player NOT in the main scene AFTER exporting

Godot Version

Godot 4.6 stable

Question

I’ve been working on a game for a while but just when its the time to export it I encountered a strange problem. First , in the exported game the main scene would not load even tho the main menu will and no matter how much longer I wait , it just doesnt load even tho it works PERFECTLY fine in the editor preview. I happen to fix the not loading problem by copying the absolute path of the main scene instead of the normal path and it worked perfectly in the editor as before and now , the main scene loads in the exported game but the main scene doesnt have the player even tho the player scene is normally instantiated in the editor and appears in the editor preview but WHY is the player NOT in the exported game? If someone knows how to fix this problem and needs more explanation let me know.

This is a common problem if exporting on Windows, and can affect Macs too. Make sure all the folder and asset names are snake_case. Having uppercase letters or spaces in filenames is the most common cause of this weird export problem.

1 Like

Thank You for responding but I did use snake_case in filenames, script names, variables, functions etc

My next guess then is that you are hardcoding file paths instead or using UIDs. Can you show the code of the scenes loading the main menu and player? And if they are stored in exported variables, a screenshot of the Inspector.

Also, I recommend upgrading to 4.6.2.stable. (But not as a fix for this.)

This is the script of the loading screen scene if you’re referring to that:
extends Control
var next_scene_path :String = “D:/godot/zombie-apocalypse/scenes/main.tscn”
var progress:Array[float] =

Called when the node enters the scene tree for the first time.

func _ready() → void:
ResourceLoader.load_threaded_request(next_scene_path)

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
var status = ResourceLoader.load_threaded_get_status(next_scene_path, progress)
match status:
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
var pct = progress[0] * 100
ResourceLoader.THREAD_LOAD_LOADED:
var scene = ResourceLoader.load_threaded_get(next_scene_path)
get_tree().change_scene_to_packed(scene)

Please format your code by using ``` on the lines above and below your code, like this:

```gd
#Paste code from your editor here to maintain indentation.
```

And it will look like this:

#Paste code from your editor here to maintain indentation.

You cannot use full directory paths based on your file system, paths for exported resources should start with res://

You probably want "res://scenes/main.tscn"

2 Likes

I know that but it’s somehow making it worse by not loading the main scene at all that’s why I used the absolute path so it at least loads the main scene even tho both methods work completely fine in the editor preview

extends Control
var next_scene_path :String = “D:/godot/zombie-apocalypse/scenes/main.tscn”
var progress:Array[float] = []

#Called when the node enters the scene tree for the first time.
func _ready() → void:
ResourceLoader.load_threaded_request(next_scene_path)

#Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta: float) → void:
var status = ResourceLoader.load_threaded_get_status(next_scene_path, progress)
match status:
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
var pct = progress[0] * 100
ResourceLoader.THREAD_LOAD_LOADED:
var scene = ResourceLoader.load_threaded_get(next_scene_path)
get_tree().change_scene_to_packed(scene)

Is this scene file in a totally separate folder? Where is your project located?

If you try to copy and paste the code you put on the form it’s formatting is already destroyed, re-paste your actual code.

1 Like

The main scene is not located somewhere else and is in the project folder .
Also here is the formatted code:

extends Control
var next_scene_path :String = “D:/godot/zombie-apocalypse/scenes/main.tscn”
var progress:Array[float] = []



#Called when the node enters the scene tree for the first time.

func _ready() → void:
    ResourceLoader.load_threaded_request(next_scene_path)

#Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
    var status = ResourceLoader.load_threaded_get_status(next_scene_path, progress)
    match status:
        ResourceLoader.THREAD_LOAD_IN_PROGRESS:
            var pct = progress[0] * 100
        ResourceLoader.THREAD_LOAD_LOADED:
            var scene = ResourceLoader.load_threaded_get(next_scene_path)
            get_tree().change_scene_to_packed(scene)

  1. Click and drag main.tscn from File System.
  2. Press the Ctrl key after you start dragging and before you drop it.
  3. Drop the file into your script.
  4. It will create a const something like this:
const MAIN = "uid://random_letters_and_numbers"
  1. Replace the file path you have with the UID.

Test it again.

2 Likes

I tried it and it worked perfectly in the editor but again , NOT in the exported game as it just showed the loading screen without actually loading the scene . Correct me if i am wrong but did you mean to change the string of the next scene path like this?
var next_scene_path :String = “uid://h12fchrigxkp”

How do you run your project in the editor?

It will be easy to find the problem by adding some debugging.

func _ready() → void:
    ResourceLoader.load_threaded_request(next_scene_path)
    print("ready was loaded")
func _process(delta: float) → void:
    var status = ResourceLoader.load_threaded_get_status(next_scene_path, progress)
    match status:
        ResourceLoader.THREAD_LOAD_IN_PROGRESS:
            var pct = progress[0] * 100
        ResourceLoader.THREAD_LOAD_LOADED:
            var scene = ResourceLoader.load_threaded_get(next_scene_path)
            get_tree().change_scene_to_packed(scene)
            print("Scene changed")

make sure to enable export with debug
image

Using D:/godot/zombie-apocalypse/scenes/main.tscn will break your game for other players, also it wouldn’t even work for you if the exported exe was moved to another folder

uhh i exported with debug and it gave some errors:
ERROR: Can’t load dependency: ‘res://scenes/character_body_2d.tscn’.
at: (core/io/resource_format_binary.cpp:459)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
ERROR: Error when trying to parse Variant.
at: (core/io/resource_format_binary.cpp:503)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
ERROR: Error when trying to parse Variant.
at: (core/io/resource_format_binary.cpp:490)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
ERROR: Failed loading resource: res://.godot/exported/133200997/export-3ad5c15c4f3250da0cc7c1af1770d85f-main.scn.
at: (core/io/resource_loader.cpp:343)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
SCRIPT ERROR: Parse Error: Could not preload resource file “uid://h12fchrigxkp”.
at: GDScript::reload (res://scripts/loading_screen.gd:2)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
ERROR: Failed to load script “res://scripts/loading_screen.gd” with error “Parse error”.
at: load (modules/gdscript/gdscript.cpp:2907)
GDScript backtrace (most recent call first):
[0] _on_start_pressed (res://scripts/main_menu.gd:64)
I dont know why its failing to load character body 2d even though it has no missing dependencies

Seems like the problem is cyclic dependencies.what is this? I don’t even know.But it’s quite a messy thing to fix.It’s just because of bad project structure.

Do you use preload() to load the character?

No i dont use preload to load the character

Yes, that’s what I meant for you to do. And here’s the thing, even though it is causing you problems still, it helped eliminate two different possible causes for the problem, and one bug you would see later (which is it wouldn’t work on anyone else’s computer with the hard-coded absolute path).

So, keep it using a UID, and let’s see what else is going on.

Next thing is to look at the lines of code being output by the errors. Every single one of them is on Line 64 of main_menu.gd. So what’s on that line?

Line 64 just changes the scene to the loading screen scene:
get_tree().change_scene_to_file("res://scenes/loading_screen.tscn")
the function is like this:
func _on_start_pressed() → void:
$AnimationPlayer/ColorRect.visible = true
animation_player.play(“new_animation”)
await get_tree().create_timer(1).timeout
get_tree().change_scene_to_file(“res://scenes/loading_screen.tscn”)

Please format your code the way I showed you at the top of the thread. It is really annoying to have to delete the backticks at the beginning and end of every line to edit your code.

A few things:

  • animation_player and $AnimationPlayer appear to be the same variable. You should use animation_player in your code. It’s a good habit to get into in case you ever move a Node or change its name in your Scene Tree.
  • In this case, I added an @onready color_rect variable for the same reason.
  • Awaiting animation_finished means you can update your animation and don’t have to change the amont of time you wait in your code.
@onready var color_rect: ColorRect = "$AnimationPlayer/ColorRect"

func _on_start_pressed() -> void:
	color_rect.visible = true
	animation_player.play("new_animation")
	await animation_player.animation_finished
	get_tree().change_scene_to_file("res://scenes/loading_screen.tscn")

So looking at line 64 and your errors, it seems the problem is inside loading_screen.tscn when it tries to load _on_start_pressed - which seems like a function attached to a button. So what’s in that code?