Skip to content

Commit

Permalink
feat:auto setup
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenll committed Aug 28, 2023
1 parent e65d8a0 commit 913c1ef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
23 changes: 21 additions & 2 deletions addons/csv-data-importer/csv_data.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
extends Resource

@export var headers := [] #column name
@export var records := [] #origin data
## column name
@export var headers := []
## origin data
@export var records := [] :
set(v):
records = v
if _auto_setup:
setup()

var _data:= {} #column name to index
var _auto_setup = false
## _data getter
var data:
get:
return _data

func _init(auto_setup = true):
_auto_setup = auto_setup


func setup():
var field_indexs = {}
Expand All @@ -27,3 +42,7 @@ func setup():

func fetch(primary_key):
return _data.get(str(primary_key))


func keys():
return _data.keys()
7 changes: 3 additions & 4 deletions addons/csv-data-importer/import_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func _get_recognized_extensions():


func _get_save_extension():
return "res"
return "tres"


func _get_resource_type():
Expand Down Expand Up @@ -85,10 +85,9 @@ func _import(source_file, save_path, options, platform_variants, gen_files):
continue
lines.append_row(row)
file.close()

var data = preload("csv_data.gd").new()
# do not setup here
var data = preload("csv_data.gd").new(false)
var rows = lines.get_data()

data.records = rows
data.headers = meta.headers

Expand Down
2 changes: 1 addition & 1 deletion addons/csv-data-importer/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="CSV Typed Importer"
description="Import CSV files as native Array or Dictionary."
author="citizenl"
version="1.0"
version="1.1"
script="plugin.gd"
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Using CSV files as game data configuration tables in Godot is inspired by the [g

```gdscript
func _ready():
var example = preload("res://assets/example.csv").setup()
var example = preload("res://assets/example.csv")
var shooter = example.fetch(1)#fetch row data by primary key, here is 1
print(shooter)
#{ "id": 1, "name": "Shooter", "speed": 100, "max_speed": 150, " damage": 10, " crit": 0.5, "items": [1, 1, 2, { "foo": 1, "bar": 2 }, "test"] }
Expand Down

0 comments on commit 913c1ef

Please sign in to comment.