【Godot】Load file data

Godot 3.3 rc6

Then the last tutorial [GDScript] equipment bar

We started to load the item data. The previous way of using resources was to load a small amount of data. If it is a large amount of data, it is inevitable to load the data in the file.

For the code of loading data, see the previous article [GDScript] Load csv file and convert it into a dictionary

Start to set the data
insert image description here
of the file and save it as a csv file, we save the name asGoodsData.csv

Next we create a data folder in the file system and put the file GoodsData.csv in it

insert image description here
You can see that the wrong icon is displayed, and an error is reported in the output. This is because Godot doesn't recognize this kind of file, and the import is wrong.
insert image description here
We create a file named under the data folder as.gdignoreThe file, Godot will ignore this folder when importing, after creating, we exit the project, press Ctrl + Shift + Q or select the menu project - exit to the project list to exit. Let's reopen the project again.

You can see that there is no such folder in the file system

insert image description here

We create a GoodsFactory.gd script file in the autoload folder , and then add this script to autoload to get item nodes

(If you don't know where the automatic loading is, see the previous article [GDScript] Load csv file and convert it into the lower part of the dictionary

Open the GoodsProperty.gd script that was made before, modify the AttackRange of GoodsProperty to AttackRange, and change the previous small mistake
insert image description here
to
insert image description here

Then we write the following in the GoodsFactory.gd script

## GoodsFactory
## Item Factory
extends Node


## item properties
const GoodsProperty = preload("res://inventory/GoodsProperty.gd")
## Item scene file
const ScnGoods = preload("res://inventory/Goods.tscn")
## Item attribute list
const GoodsPropertyList = GoodsProperty.GoodsProperty

# Folder for storing item images (here I created these paths)
const GoodsPicturePath = "res://assets/texture/goods/"


## item data
var goods_data =  { }


# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# Node with methods
#------------------------------
func _ready():
	## Get item data
	goods_data = FileManager.parse_csv_file("res://data/GoodsData.csv", "Name")



# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# custom method
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
## return an item
## @goods_name item name
## @return returns an item's node
func get_goods(goods_name: String):
	if goods_data.has(goods_name):
		var data = get_goods_data(goods_name)	# 物品数据
		var res_property = GoodsProperty.new()
		
		## Set the attribute data of the item attribute resource
		res_property.set_property(data)
		
		## return item node
		var goods = ScnGoods . instance ( ) 
		goods . set_goods_property ( res_property ) 	# Set item properties
		 return goods
	 else : 
		print_debug ( "There is no [%s] this item"  % goods_name ) 
		return  null



## return item data
## -- -- -- -- -- -- --
## The reason for this method is because the attributes of the data may be inconsistent with the attribute data of the file
## Here we convert the data into data that conforms to the item attribute data format
func get_goods_data(goods_name: String):
	var data = goods_data[goods_name]	# 物品数据
	var temp = {}
	
	# Set the format of the resource: variable [ property name ]  = property value
	temp[GoodsPropertyList.Name] = data['Name']
	temp[GoodsPropertyList.Damage] = int(data['Damage'])
	temp[GoodsPropertyList.MoveSpeed] = int(data['MoveSpeed'])
	temp[GoodsPropertyList.AttackRange] = int(data['AttackRange'])
	temp[GoodsPropertyList.Price] = int(data['Price'])
	temp[GoodsPropertyList.Texture] = get_goods_texture(data['Picture'])
	return temp


## Get item image
## @picture_name item picture name
## @ return returns the picture of the item
 var file = File . new ( ) 
func get_goods_texture ( picture_name : String )  - > Texture : 
	var path = GoodsPicturePath + picture_name +  ".png" 	# Item picture path
	
	# If there is an image, return the image
	if file.file_exists(path):
		return load(path) as Texture
	# If it does not exist, return the default icon image
	else:
		return preload("res://icon.png")

  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

Now we have done the script about getting the item, here I don't have it inres://assets/texture/goods/Put pictures under the folder, but if you have a picture file, you can modify it to a picture file with the same name as your Picture, and then it will load by itself

Now we start to add the function of "adding items" to Inventory
. Open the Inventory.tscn scene, add a script, and write the following code:

## Inventory
extends PanelContainer


onready var grid = $Margin/Grid


# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# Node with methods
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
func _ready ( ) : 
	add_goods ( "Gold Sore Medicine" ) 
	add_goods ( "Peach Wood Sword" ) 
	add_goods ( "Rusty Iron Sword" )



# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# custom method
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
## add items
## @item_name item name
func add_goods(item_name: String):
	var goods = GoodsFactory.get_goods(item_name)
	if goods:
		grid.add_child(goods)

  • 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

Let's open the Goods.tscn scene we made before, select the TextureRect node, and add a script. The code is as follows:

extends TextureRect


## Point the mouse to display data
func _make_custom_tooltip(for_text):
	var label = Label.new()
	var goods_data = get_parent().goods_property.get_property()	# 物品数据
	label . text =  JSON . print ( goods_data ,  '\t' ) 
	return label # Return this label and display the data

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Then give the TextureRect node ofhint_tooltipAdd text to the attribute, any non-space string can be called_make_custom_tooltipmethod, I set it to 111, any character does not matter
insert image description here

Let's switch back to the Inventory.tscn scene, press F6 to run the test, point the mouse to the item, and then display the item information:

insert image description here

I made it this way to save time for demonstration, you can do it in the script of the Texture node above_make_custom_tooltipSet your own label in the method and let him show it.

It can be seen that the Chinese font is not displayed, because the built-in Godot tag does not support Chinese, we need to manually set the Chinese font for him

Click on the Inventory.tscn scenethemeProperties, click to create theme resource
insert image description here
Click the default font below to createDynamicFont
insert image description here
Drag and drop a font file in the font_data attribute of the resource , and I will give the project file below, which contains fonts, or you have a Fonts font folder in Windows (the location of your Baidu Fonts folder), select a Chinese font and copy it to In the folder of the Godot project, then set
insert image description here
and then press F6 to test the scene, this time you can see that there are Chinese fonts
insert image description here

Project file:
Baidu network disk link: https://pan.baidu.com/s/1qq9VGfWCrcDplqC-0tZVFw
Extraction code: gu6g

If there are any mistakes in the text, please correct me.

Related: 【Godot】Load file data