最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.
发布评论

评论列表(0)

  1. 暂无评论