-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.gd
51 lines (40 loc) · 891 Bytes
/
Storage.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
# Usage:
# $Storage.store("logs", 10)
#
# $Storage.has("logs") // true
# $Storage.has("logs", 12) // false
# $Storage.has("logs", 5) // true
#
# $Storage.retrieve("logs", 4) // 4
# $Storage.retrieve("logs", 7) // false
# $Storage.retrieve("logs") // 6
extends Node2D
var storage = {}
func store(key, amount):
#print("Storing ", key, " (", amount, ")")
if !storage.has(key):
storage[key] = amount
else:
storage[key] += amount
func retrieve(key, amount = null):
if !has(key, amount):
return false
if amount == null:
var everything = storage[key]
storage[key] = 0
return everything
storage[key] -= amount
return amount
func has(key, amount = null):
if !storage.has(key):
return false
if amount == null:
return true
if storage[key] < amount:
return false
else:
return true
func quantity(key):
if !storage.has(key):
return 0
return storage[key]