forked from UVASGD/student-game-developers-at-uva-spring-2025-student-game-developers-at-uva-fall-2024-ProjectTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPCS.gd
34 lines (29 loc) · 983 Bytes
/
NPCS.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
extends Node
@onready var npcs = {}
var npc_folder_path = "res://assets/scenes/npcs/"
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
load_npcs()
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func load_npcs():
var dir = DirAccess.open(npc_folder_path)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if file_name.ends_with(".tscn"):
var file_path = npc_folder_path + file_name
var scene = load(file_path) as PackedScene
if scene:
var instance = scene.instantiate()
var npc_name = instance.get("npc_name")
if npc_name:
npcs[npc_name] = scene
instance.queue_free() # Free the instance after getting the name
file_name = dir.get_next()
dir.list_dir_end()
func get_npc(speakername: String) -> PackedScene:
return npcs.get(speakername, null)