Godot Version
4.2.2
Question
Hello friends,
I’m completely new to godot, so please bear with me. I made a dailog_manager.gd file that opens a chat bubble whenever a character tries to interact with an npc. It’s a singleton. This is what the code looks like partially:
@onready var text_box_scene = preload("res://dialog/text_box.tscn")
@onready var player = get_tree().get_first_node_in_group("player")
var dialog_lines: Array[String] = []
var current_line_index = 0
var text_box
var text_box_position: Vector2
var is_dialog_active = false
var can_advance_line = false
signal dialog_finished()
func start_dialog(position: Vector2, lines: Array[String]):
if is_dialog_active:
return
player.disable_movement()
dialog_lines = lines
text_box_position = position
_show_text_box()
is_dialog_active = true
Initially, this worked great! However when I created a new scene and added it to my flow, it messed up my code, get_tree().get_first_node_in_group("player")
started returning null. The new scene is a simple screen with a start button that redirects to the main_scene where the player can play the actual game. I didn’t remove any of the nodes from the player
group, so I dont know why the code is returning null.
Here’s what the start screen looks like:
class_name StartMenu
extends Control
@onready var start_button = $MarginContainer/HBoxContainer/VBoxContainer/StartButton
@onready var settings_button = $MarginContainer/HBoxContainer/VBoxContainer/SettingsButton
@onready var exit_button = $MarginContainer/HBoxContainer/VBoxContainer/ExitButton
@onready var main_scene = preload("res://main_town/main_town.tscn") as PackedScene
func _ready():
start_button.button_down.connect(_on_start_pressed)
settings_button.button_down.connect(on_settings_pressed)
exit_button.button_down.connect(_on_exit_pressed)
func _on_start_pressed() -> void:
get_tree().change_scene_to_packed(main_scene)
func on_settings_pressed() -> void:
pass
func _on_exit_pressed() -> void:
get_tree().quit()
I was able to fix this somehow by passing the body around and getting it from _on_body_entered(body)
signal. What I don’t understand is, why did the get_tree().get_first_node_in_group("player")
code started returning null when I added the start screen?