forked from gdquest-demos/godot-4-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
030-no-strings.gd
59 lines (24 loc) · 902 Bytes
/
030-no-strings.gd
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
func has_higher_value(a: Item, b: Item) -> bool:
return a.price > b.price
func sort_by_value(inventory: Array[Item]) -> void:
inventory.sort_custom(has_higher_value)
#####################################################################
func _run() -> void:
var inventory = create_items(["sword", "shield", "potion", "pelt", "boots"])
print(inventory)
sort_by_value(inventory)
print(inventory)
func create_items(names:Array[String]) -> Array[Item]:
var name_to_item := func (name: String) -> Item:
return Item.new(name, randi_range(1, 10))
var array: Array[Item] = []
array.assign(names.map(name_to_item))
return array
class Item extends Resource:
var name := ""
var price := 10
func _init(initial_name: String, initial_price: int) -> void:
name = initial_name
price = initial_price
func _to_string() -> String:
return "(%s:$%s)"%[name, price]