My character wont load in the world scene when selected in the character selection

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrandeHolt
func _ready():
var selected_character_key = CharacterSelected.PlayerSelect
if selected_character_key in selectable_characters:
	var character_path = selectable_characters[selected_character_key]
	var player_character = load(character_path).instance()

this is my code for the world scene, it works perfectly fine, but somehow the character doesn’t load

:bust_in_silhouette: Reply From: Gluon

Hello Grandeholt I think I mentioned this in your previous question the best way to do it is as below

const Char1 = preload("res://Path/To/File/Character1.tscn")
const Char2 = preload("res://Path/To/File/Character2.tscn")
const Char3 = preload("res://Path/To/File/Character3.tscn")

func _ready():
    var char
    if foobar.character == 1:
        char = Char1.instance()
    if foobar.character ==2:
        char = Char2.instance()
    if foobar.character ==3:
        char = Char3.instance()
    char.global_position = position2d.global_position

So that way you have created constants with the exact path to all of your different characters but you only load and add the character which has been selected. In the above I have put foobar as a global script which has held the character chosen by the player in the previous player selection screen.

IT FINALLY WORKED! THANK YOU, xD i totally forgot about my last question, I have been modifying my character selection code for 2 days, it finally worked thank you!

GrandeHolt | 2022-12-29 05:14