简体   繁体   English

如何加载从角色选择场景中获取的玩家以加载到 Godot 中的世界场景

[英]How can I load a player thats taken from a character selection scene to load to a world scene in Godot

So I had this problem for 2 days now and I can't seem to solve this issue, I browsed on the inte.net and still can't solve it.所以我已经遇到这个问题 2 天了,我似乎无法解决这个问题,我在 inte.net 上浏览仍然无法解决。 This is a 2d type game.这是一个二维类型的游戏。

extends Node2D var selectableCharacter = CharacterSelected.PlayerSelect func _process(delta): match CharacterSelected.PlayerSelect: 0: get_node("PlayerSelect").play("C1")扩展 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(): func_on_Left_pressed():

if CharacterSelected.PlayerSelect > 0:如果 CharacterSelected.PlayerSelect > 0:

CharacterSelected.PlayerSelect -= 1

func _on_Right_pressed(): func_on_Right_pressed():

if CharacterSelected.PlayerSelect < 8:如果 CharacterSelected.PlayerSelect < 8:

CharacterSelected.PlayerSelect += 1

func _on_Play_pressed(): func_on_Play_pressed():

CharacterSelected.PlayerSelect = selectableCharacter CharacterSelected.PlayerSelect = 可选角色

get_tree().change_scene("res://Level 1.tscn") 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 = { var 可选字符 = {

"C1": preload ("res://C1r.tscn"), “C1”:预加载(“res://C1r.tscn”),

"C2": preload ("res://C2.tscn"), “C2”:预加载(“res://C2.tscn”),

"C3": preload ("res://C3.tscn"), “C3”:预加载(“res://C3.tscn”),

"C4": preload ("res://C4.tscn"), “C4”:预加载(“res://C4.tscn”),

"C5": preload ("res://C5.tscn"), “C5”:预加载(“res://C5.tscn”),

"C6": preload ("res://C6.tscn"), “C6”:预加载(“res://C6.tscn”),

"C7": preload ("res://C7.tscn"), “C7”:预加载(“res://C7.tscn”),

"C8": preload ("res://C8.tscn") } “C8”:预加载(“res://C8.tscn”)}

var PlayerSelect = 0 var 播放器选择 = 0

And as for the World script:至于世界剧本:

extends Node2D扩展 Node2D

var player_character_path =... var player_character_path =...

func _ready():功能_就绪():

var player_character = load(player_character_path).instance() var player_character = load(player_character_path).实例()

add_child(player_character) add_child(player_character)

I can't wrap my head around how to solve this issue, I am still a newbie on godot.我不知道如何解决这个问题,我仍然是 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搜索谷歌,观看 yt 教程,阅读 godot 中的官方单例,我希望能够解决这个问题,但没有运气

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.好的,所以我在 Godot 方面有一些经验,但我不知道你的完整项目,所以请对我所说的一切持保留态度。

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将来,我还建议使用code元素,这是我通过搜索 StackOverflow 发现的,因为我现在是 StackOverflow 菜鸟: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.好的,所以我建议在项目设置 ( https://docs.godotengine.org/en/stable/classes/class_projectsettings.html ) 中创建一个“全局”变量,然后,从字符 select 屏幕, set_setting()一个新的全局变量,您可以将其称为“ActiveCharacter”或其他名称,然后它会跨场景保存。

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.现在在 World 场景中,您已经添加了所有九个字符,然后使用ifelif来查看选择了哪个字符。 Let's say that Character 5 is selected, and the variable with that is stored in global/character in the ProjectSettings.假设选择了 Character 5,其变量存储在 ProjectSettings 的global/character中。 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.如果角色是角色 5,那么所有其他角色都将被删除,玩家只能控制角色 5。您需要用if填充 etc,然后您将拥有一个正常运行的角色选择屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM