Why can't I add characters to a scene from an autoloaded game state script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By brandogs

I have an autoloaded game state script that is attempting to add a character to the scene as follows:

var main = main_scene.instance()

var player = load("res://characters/Player.tscn").instance()
var world = main.get_node("World/")
var spawn_point = world.get_node("SpawnPoints/0")
player.position = spawn_point.position

After adding the child, I can see the character in the scene tree:


However, the character never appears actually appears on the screen.

Running this code in the main scene’s script, the character appears as expected.

func _ready():
	var player = load("res://characters/Player.tscn").instance()
	var spawn_point = $World.get_node("SpawnPoints/0")
	player.position = spawn_point.position

Why wouldn’t running this code from the game state script work when the code works when run from the scenes script itself?

Make a new scene and add a RigidBody2D as the root. when its “if” statement detects the change in the state of "shooting it and the bullet delay of 1 game engine script Godot Q&A.business management dissertation writing How to use modulate in script Posted on August 31 of the paddle. js (53) Service Workers (4) Progressive Web Our character in Godot.

flavia110 | 2020-07-04 16:05

:bust_in_silhouette: Reply From: deaton64


It might be there, but off screen, check it’s Global position.

Like this: Might be the same thing

Editing my comment from before, I tested this change first thing in the morning before I had my coffee and accidentally left the main scenes child in there from last night, thinking that solved the problem. Unfortunately, looking over this more closely now, it didn’t actually resolve the issue. Thank you for the feedback though! That’s one more thing for me to be aware of in the future!

brandogs | 2020-07-04 15:34

Here is the position info from the two different children that are being added. It seems like my local and global positions are the same at the time of adding the child.

Gamestate player global position: (32.18, 69.643204)
Gamestate player position: (32.18, 69.643204)
Main scene player global position: (32.18, 69.643204)
Main scene player position: (32.18, 69.643204)

However, despite the same coordinates, the child is only visible when added from the main scene and not from the gamestate file.

brandogs | 2020-07-04 16:32

Hard to say without seeing the code, could you upload (maybe a cut down version) of the code that’s creating the issue?
Did you try adding a zoomed out camera?

deaton64 | 2020-07-06 15:20