godot - How to change to instance of a scene? - Stack Overflow

I'm trying to make a pretty linear game that has Chapter 1, Chapter 2, etc, obviously with each on

I'm trying to make a pretty linear game that has Chapter 1, Chapter 2, etc, obviously with each one having a different content but sharing similarities (e.g. that each chapter has a chapter title). I want these chapters to happen one after another (for now, they should just show the chapter title and then start the next chapter).

What's the best way to go about this?

So far I have:

  • created a Chapter scene (chapter.tscn) (class name Chapter) with exported variables such as chapter_name (of type String) and next_chapter (of type Chapter).

  • added several of those Chapter scenes to my main game scene (Chapter1, Chapter2, …), giving them names and assigning the next chapters.

  • added an exported variable first_chapter in my main game node so I can refer to the first chapter in the code.

In the script of my main game scene (which is my main menu), I want to switch to the scene of the first_chapter (which is of type Chapter) when the “New Game” button is pressed. (Then that scene would do some stuff like show the chapter title and switch to the next chapter.)

Unfortunately, I don’t know how to switch to that first_chapter scene.

I know how to switch to a scene file (get_tree().change_scene_to_file(“res://chapter.tscn”)) but that is not what I want to do here; I don’t want to switch to the Chapter scene in general, but specifically the instance Chapter1 in my tree (in my first_chapter variable)

I'm trying to make a pretty linear game that has Chapter 1, Chapter 2, etc, obviously with each one having a different content but sharing similarities (e.g. that each chapter has a chapter title). I want these chapters to happen one after another (for now, they should just show the chapter title and then start the next chapter).

What's the best way to go about this?

So far I have:

  • created a Chapter scene (chapter.tscn) (class name Chapter) with exported variables such as chapter_name (of type String) and next_chapter (of type Chapter).

  • added several of those Chapter scenes to my main game scene (Chapter1, Chapter2, …), giving them names and assigning the next chapters.

  • added an exported variable first_chapter in my main game node so I can refer to the first chapter in the code.

In the script of my main game scene (which is my main menu), I want to switch to the scene of the first_chapter (which is of type Chapter) when the “New Game” button is pressed. (Then that scene would do some stuff like show the chapter title and switch to the next chapter.)

Unfortunately, I don’t know how to switch to that first_chapter scene.

I know how to switch to a scene file (get_tree().change_scene_to_file(“res://chapter.tscn”)) but that is not what I want to do here; I don’t want to switch to the Chapter scene in general, but specifically the instance Chapter1 in my tree (in my first_chapter variable)

Share Improve this question edited Mar 16 at 19:05 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 16 at 18:33 Petra1999Petra1999 751 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If all of your chapters are Packed Scenes that exist as children of your chapter-select screen, as you described, then all of the chapters actually are present, just "underneath" the menu. In this case, all you would need to do is set only specific nodes to be visible.

For example, given the scene tree that you described:

-main
  |-chapterOne
  |-chaptertwo
  |-(other chapters, etc.)
  |-menuUI
    |-buttonA
    |-buttonB
    |-(other ui nodes, etc.)

You would need to

  1. Set up the main scene with all of the chapter nodes not visible via the eye-shaped button
  2. Access the button press signal via script, (click on button node, then go to Node tab -> Signals -> right click pressed() -> connect)
  3. Make a specific chapter node visible
  4. Make sure everything else isn't visible unless desired.

This could look like:

#... in main's script...

# for each button, set up:
func _on_button_a_pressed() -> void:
    $menuUI.visible = false
    $chapterOne.visible = true

# You will also eventually need a function to change between chapters.
# This could look like:
func change_visible_chapter(old_chapter, new_chapter):
    get_node(old_chapter).visible = false
    get_node(new_chapter).visible = true

Now, I do need to mention that having all of your chapters exist simultaneously will not be performant as you scale up what those chapters contain. If you find that the program is running slowly, consider either

  • Creating separate scene s for each chapter, and switching between with get_tree().change_scene_to_file(...)
  • Create functionality in your chapter scene to take the various exported variables as inputs. Then, you could just load the Chapter scene, and set its parameters instead of having a bunch of unique copies.
    • Without seeing your code, this may be possible already. Try setting the variables of the scene and then manually calling chapter_scene._ready() afterwards to initialize with the new variables.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744589677a4582525.html

相关推荐

  • godot - How to change to instance of a scene? - Stack Overflow

    I'm trying to make a pretty linear game that has Chapter 1, Chapter 2, etc, obviously with each on

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信