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 nameChapter
) with exported variables such aschapter_name
(of typeString
) andnext_chapter
(of typeChapter
).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 nameChapter
) with exported variables such aschapter_name
(of typeString
) andnext_chapter
(of typeChapter
).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)
1 Answer
Reset to default 1If 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
- Set up the main scene with all of the chapter nodes not visible via the eye-shaped button
- Access the button press signal via script, (click on button node, then go to Node tab -> Signals -> right click pressed() -> connect)
- Make a specific chapter node visible
- 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.
- Without seeing your code, this may be possible already. Try setting the variables of the scene and then manually calling
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744589677a4582525.html
评论列表(0条)