Reputation: 1
So I had this problem for 2 days now and I can't seem to solve this issue, I browsed on the internet and still can't solve it. This is a 2d type game.
extends Node2D var selectableCharacter = CharacterSelected.PlayerSelect func _process(delta): match CharacterSelected.PlayerSelect: 0: get_node("PlayerSelect").play("C1")
selectableCharacter = "C1"
1:
get\_node("PlayerSelect").play("C2")
selectableCharacter = "C2"
2:
get\_node("PlayerSelect").play("C3")
selectableCharacter = "C3"
3:
get\_node("PlayerSelect").play("C4")
selectableCharacter = "C4"
4:
get\_node("PlayerSelect").play("C5")
selectableCharacter = "C5"
5:
get\_node("PlayerSelect").play("C6")
selectableCharacter = "C6"
6:
get\_node("PlayerSelect").play("C7")
selectableCharacter = "C7"
7:
get\_node("PlayerSelect").play("C8")
selectableCharacter = "C8"
func _on_Left_pressed():
if CharacterSelected.PlayerSelect > 0:
CharacterSelected.PlayerSelect -= 1
func _on_Right_pressed():
if CharacterSelected.PlayerSelect < 8:
CharacterSelected.PlayerSelect += 1
func _on_Play_pressed():
CharacterSelected.PlayerSelect = selectableCharacter
get_tree().change_scene("res://Level 1.tscn")
The code above is for the character selection, and it is a type where you click an arrow and the sprites changes and you hit play.
I've also created an autoload script with:
extends Node
var selectableCharacter = {
"C1" : preload ("res://C1r.tscn"),
"C2" : preload ("res://C2.tscn"),
"C3" : preload ("res://C3.tscn"),
"C4" : preload ("res://C4.tscn"),
"C5" : preload ("res://C5.tscn"),
"C6" : preload ("res://C6.tscn"),
"C7" : preload ("res://C7.tscn"),
"C8" : preload ("res://C8.tscn") }
var PlayerSelect = 0
And as for the World script:
extends Node2D
var player_character_path = ...
func _ready():
var player_character = load(player_character_path).instance()
add_child(player_character)
I can't wrap my head around how to solve this issue, I am still a newbie on godot.
Does anyone know how to solve this? Any help is greatly appreciated
Searching google, watching yt tutorials, read the official singletons in godot, I expected to be able to solve this, but no luck
Upvotes: 0
Views: 302
Reputation: 1
Okay, so I have some experience in Godot, but I don't know your full project, so take all I say with a grain of salt.
In the future, I would also suggest using the code
element, which I found out by searching into StackOverflow, as I'm a StackOverflow noob as of now :D
Okay, so I'd suggest creating a "global" variable in the Project Settings (https://docs.godotengine.org/en/stable/classes/class_projectsettings.html) and then, from the character select screen, set_setting()
a new global variable that you would make called "ActiveCharacter" or something, and then it would be saved across scenes.
Now in the World scene, you have all nine characters already added, and then you use if
and elif
to see which character is selected. Let's say that Character 5 is selected, and the variable with that is stored in global/character
in the ProjectSettings. Well, this code might work for that (I am a bit rusty):
extends Node2D
func _ready():
if ProjectSettings.get_setting("global/character") == 1:
$Char2.queue_free()
$Char3.queue_free()
$Char4.queue_free()
$Char5.queue_free()
$Char6.queue_free()
$Char7.queue_free()
$Char8.queue_free()
$Char9.queue_free()
Etc, etc.
elif ProjectSettings.get_setting(global/character) == 5:
$Char1.queue_free()
$Char2.queue_free()
$Char3.queue_free()
$Char4.queue_free()
$Char6.queue_free()
$Char7.queue_free()
$Char8.queue_free()
$Char9.queue_free()
etc, etc...
If the character was character 5, then all other characters would be deleted, leaving the player controlling only character 5. You would need to fill in the etc's with the if
s, and then you would have a functioning character selection screen.
Upvotes: 0