ItemList.gd 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. extends Node
  2. var game_item_list = null
  3. # class member variables go here, for example:
  4. # var a = 2
  5. # var b = "textvar"
  6. func _ready():
  7. game_item_list = init_game_item_list()
  8. #FIXME: name
  9. func find_game_item_data_by_id(id):
  10. for item in game_item_list:
  11. if not item.has("id"):
  12. print ("Warning: item without id found")
  13. continue
  14. if item["id"] == str(id):
  15. return item
  16. print ("item with id " + str(id) + " not found ??")
  17. return
  18. #pass
  19. #FIXME: name
  20. func get_game_item_value(item_data, key):
  21. if not item_data.has(key):
  22. print ("Warning: item_data has no key '" + key + "'")
  23. return
  24. else:
  25. return item_data[key]
  26. pass
  27. func get_item_value(id, key):
  28. var item_data = find_game_item_data_by_id(id)
  29. if (item_data == null):
  30. return
  31. return get_game_item_value(item_data, key)
  32. func init_game_item_list():
  33. var item_list_file = File.new()
  34. var path = "res://items.json"
  35. if not item_list_file.file_exists(path):
  36. print ("res://items.json - path not found")
  37. return
  38. item_list_file.open(path, File.READ)
  39. var result_json = JSON.parse(item_list_file.get_as_text())
  40. if result_json.error == OK: # If parse OK
  41. var data = result_json.result
  42. #print(data)
  43. return data.values()[0]
  44. else: # If parse has errors
  45. print("Error: ", result_json.error)
  46. print("Error Line: ", result_json.error_line)
  47. print("Error String: ", result_json.error_string)
  48. return