【GDScript】Properties of Design Items

Godot 3.2.4

In the game, we can directly set the attribute of the design item to aResource(resource file), so that we can easily design each item as a resource file of the same type but not the same file, and then place it on the item.

We first design the properties of the item and create aGoodsProperty.gdscript file

## Goods Property
## Resource type for GoodsList display properties
tool # Tool class for displaying effects in real time in the editor
extends Resource

## Item attributes (the attribute name of the item is written below, which is used to get and set the attribute of the item later)
## In this way, we only add the key value here, and then modifying it will not affect the subsequent key value
## Code to set properties
const GoodsProperty = {
	Name = "goods_name",
	Texture = "goods_texture",
	Price = "price",
	Damage = "damage",
	MoveSpeed = "move_speed",
	AttackSpeed = "attack_speed",
}


signal goods_texture_changed(texture_) # Item texture changed signal


export var goods_name = "" # item name
export (Texture) var goods_texture setget set_goods_texture # item image
export (String, MULTILINE) var goods_description # item description
export var price = 0 # price
export var damage = 0 # damage value
export var move_speed = 0 # move speed
export var attack_range = 0 # attack range


#--------------------------------------------
# setget
#--------------------------------------------
## set the image of the item
func set_goods_texture(value) -> void:
	goods_texture = value
	emit_signal("goods_texture_changed", value)

## Return the properties of the item
func get_property() -> Dictionary:
	var property_list = GoodsProperty.values() # Get all property names
	var data = {}
	for property in property_list:
		var property_value = get(property)
		data[property] = property_value
	return data

## Set the properties of the item
func set_property(property_dict: Dictionary) -> void:
	for key in property_dict:
		var value = property_dict[key]
		set(key, value)
  • 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

we create aResourceResource file, right-click to create in the FileSysteme panel on the left side of the editor, selectNew Resoure
insert image description here
chooseResource
insert image description here
Enter the name DefaultGoods , the default commodity resource file.
insert image description here
double clickDefaultGoodsfile, then on the right side of the editorInspector(Properties check box), set theGoodsProperty.gdDrag and drop the script file toScriptproperties, as follows:
insert image description here

Then right click on the createdDefaultGoodsfile, selectedDuplicatemake a copy
insert image description here
namedWeapon_01, click Duplicate
insert image description here
and repeat twice to create three files, such as mine:
insert image description here
We create a new Goods item scene node
insert image description here
insert image description here
Check use_top_left to center the image
insert image description here
insert image description here
Press Ctrl + A on the keyboard to add a TextureRect node
insert image description here
Rename the CenterContainer root node toGoods, as follows:
insert image description here
selectedTextureRectnode, setrect_min_sizeproperties, as follows:
insert image description here

Then add a script to the Goods node, the code is as follows:

## Goods
## thing
tool
extends CenterContainer

const GoodsProperty = preload("res://src/scene/inventory/GoodsProperty.gd") # The file path of the GoodsProperty.gd just now

export (Resource) var goods_property setget set_goods_property

onready var texture_rect = $TextureRect


#--------------------------------------------
# setget
#--------------------------------------------
func set_goods_property(value):
	if not value is GoodsProperty:
		goods_property = null
		texture_rect.texture = null
		return
	goods_property = value
	
	# When texture_rect is null, it means it has not been loaded into the scene
	# So we have to yield and wait for the current node to send the ready signal
	# After the current node is ready, texture_rect is loaded before him
	if texture_rect == null:
		# The current node sends a ready signal and then continues to execute the following statement
		yield(self, "ready")
	
	# Connect to the goods_texture_changed signal of the resource
	# goods_texture_changed signal with texture attached
	# Connect to the set_texture method of texture_rect
	When the # goods_texture attribute changes,
	# will call the set_texture method of texture_rect
	if goods_property.is_connected("goods_texture_changed", texture_rect, "set_texture"):
		goods_property.connect("goods_texture_changed", texture_rect, "set_texture")
	texture_rect.texture = value.goods_texture
  • 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

Let's make an item to try and give theWeapon_01.tresTry to modify the file, double clickWeapon_01.tresfile, modify the properties:
insert image description here
then select the Goods node we just made and
insert image description here
drag it toGoods PropertyAttributes
insert image description here
Then the item image is displayed in the scene:
insert image description here
(To be continued...)

Tags: 【GDScript】Properties of Design Items

Godot Godot gdscript game development thing

Related: 【GDScript】Properties of Design Items