mlpcardgame/Scenes/Global/CardData.gd

37 lines
977 B
GDScript3
Raw Normal View History

extends Node
var loadedSets := {}
var loadedCards := {}
func _ready():
# Search for sets in MLPData
var dir := Directory.new()
var err := dir.change_dir("res://MLPData/Sets")
if err != OK:
print(err)
dir.list_dir_begin(true, true)
while true:
var next := dir.get_next()
if next == "":
break
# Check for cached resource
var file := File.new()
file.open("res://MLPData/Sets/" + next, File.READ)
var result = parse_json(file.get_as_text())
if typeof(result) == TYPE_DICTIONARY:
loadedSets[next] = result.Name
add_to_dict(result.Cards)
print("Loaded set: ", result.Name, " (", next, ", ", len(result.Cards), " cards)")
else:
breakpoint
func add_to_dict(cards: Array):
for card in cards:
loadedCards[card.ID] = CardInfo.new(card)
func get_by_id(id: String) -> CardInfo:
if not loadedCards.has(id):
print("[WARN] Trying to get unknown card: ", id)
#TODO return error or something
return CardInfo.new({})
return loadedCards[id]