Basic saving of inventory works

This commit is contained in:
Eljakim Herrewijnen 2021-04-08 22:58:04 +02:00
parent 285ee79e16
commit bbc1e6f24e
5 changed files with 62 additions and 25 deletions

View File

@ -14,7 +14,6 @@ func AddInventoryItem(itemid, amount):
if(player_inventory_items[x].item_id == itemid):
print(str(player_inventory_items[x]))
player_inventory_items[x].amount += amount
Database.SaveInventory(player_inventory_items)
return
#if we reached here then no exisiting item is found and we iterate the array again
print("adding item")
@ -25,7 +24,6 @@ func AddInventoryItem(itemid, amount):
player_inventory_items[x].shortdesc = "desc"
player_inventory_items[x].item_id = itemid
player_inventory_items[x].amount = amount
Database.SaveInventory(player_inventory_items)
return
func GoToScene(scene):
@ -43,6 +41,11 @@ func GoToScene(scene):
func LoadSave():
Database.OpenConnection()
#Save everything
func Save():
Database.SaveInventory(player_inventory_items)
print(player_inventory_items)
func _input(event):
pass

View File

@ -45,6 +45,7 @@ func InteractWithCell():
Global.AddInventoryItem(plant_cell_mouse/2, 1)
plants_map.set_cell(int(world_position[0] / cell_size.x), int(world_position[1] / cell_size.y), (plant_cell_mouse-1))
AnimationOnInteraction(1)
Global.Save()
elif plant_cell_character > 0 and plant_cell_character % 2 == 0:
Global.AddInventoryItem(plant_cell_character/2, 1)
plants_map.set_cell(int(self.position.x / cell_size.x), int(self.position.y / cell_size.y), (plant_cell_character-1))
@ -74,5 +75,5 @@ func AnimationOnInteraction(Item):
# remove_child(itemimage)
func _ready():
Global.player_inventory_items = Database.GetInventoryItems()
Global.player_inventory_items = Database.GetInventoryItems().duplicate()

View File

@ -1,7 +1,7 @@
extends Node
const SQLite = preload("res://addons/godot-sqlite/bin/gdsqlite.gdns")
var path = "user://storage.db"
var path = "res://storage.db"
var db_name = "RingOfRaces"
var db = null
var verbose = true
@ -12,16 +12,30 @@ var host = null
func _ready():
host = OS.get_name()
class Inventory_structure:
var id = 0
var item_id = 0
var item_name = "item_name"
var amount = 0
var shortdesc = "shortdesc"
func _init(id, item_id, item_name, amount, shortdesc):
self.id = id
self.item_id = item_id
self.item_name = item_name
self.amount = amount
self.shortdesc = shortdesc
func CreateWorldDatabase():
print("Creating new database")
var SQLite = load("user://gdsqlite.gdns")
#Inventory
var player_inventory : Dictionary = Dictionary()
player_inventory["id"] = {"data_type":"int", "primary_key": true, "not_null": true} #slot id
player_inventory["item_id"] = {"data_type":"int", "not_null": true} #item id
player_inventory["item_name"] = {"data_type":"text", "not_null": true} #item name
player_inventory["amount"] = {"data_type":"int", "not_null": true} #amount
player_inventory["shortdesc"] = {"data_type":"char(80)", "not_null": true} #short description
db.create_table("player_inventory", player_inventory)
self.db.create_table("player_inventory", player_inventory)
var items : Dictionary = Dictionary()
for i in range(40):
items["id"] = i
@ -31,12 +45,14 @@ func CreateWorldDatabase():
items["shortdesc"] = "No item here"
# Insert a new row in the table
db.insert_row("player_inventory", items)
self.db.insert_row("player_inventory", items)
items.clear()
#Other world data
func OpenConnection():
if(str(OS.get_name()) == "X11"):
path = "res://storage.db"
if(str(OS.get_name()) == "Android"):
path = "user://storage.db"
self.db = SQLite.new()
var file = File.new()
self.db.path = path
@ -56,18 +72,35 @@ func OpenConnectionIfClosed():
if self.db == null:
OpenConnection()
func CloseConnection():
if self.db != null:
self.db.commit()
self.db.close()
self.db = null
func GetInventoryItems():
OpenConnectionIfClosed()
var ret = []
ret = db.select_rows("player_inventory", "",["*"])
return ret
return db.select_rows("player_inventory", "",["*"])
func SaveInventory(player_inventory_items):
print("Now on inventory save file")
if(player_inventory_items == null or len(player_inventory_items) != 40):
Global.Log("Bad inventory save!", 3)
return
OpenConnectionIfClosed()
var items : Dictionary = Dictionary()
for item in player_inventory_items:
if(item['item_id'] == 0):
continue
items["id"] = item["id"]
items["item_id"] = item["item_id"]
items["item_name"] = item["item_name"]
items["amount"] = item["amount"]
items["shortdesc"] = item["shortdesc"]
#self.db.query("select * from player_inventory")
self.db.update_rows("player_inventory", "id == " + str(items['id']),items)
#items.clear()
Global.Log("Inventory save succeeded", 1)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):

View File

@ -42,13 +42,13 @@
0/shapes = [ ]
0/z_index = 0
[sub_resource type="OccluderPolygon2D" id=6]
[sub_resource type="OccluderPolygon2D" id=2]
polygon = PoolVector2Array( 0, 0, 32, 0, 32, 96, 0, 96 )
[sub_resource type="ConvexPolygonShape2D" id=7]
[sub_resource type="ConvexPolygonShape2D" id=3]
points = PoolVector2Array( 0, 0, 32, 0, 32, 96, 0, 96 )
[sub_resource type="TileSet" id=2]
[sub_resource type="TileSet" id=4]
0/name = "interaction_map.png 0"
0/texture = ExtResource( 18 )
0/tex_offset = Vector2( 0, 0 )
@ -112,18 +112,18 @@ points = PoolVector2Array( 0, 0, 32, 0, 32, 96, 0, 96 )
4/region = Rect2( 128, 0, 32, 96 )
4/tile_mode = 0
4/occluder_offset = Vector2( 0, 0 )
4/occluder = SubResource( 6 )
4/occluder = SubResource( 2 )
4/navigation_offset = Vector2( 0, 0 )
4/shape_offset = Vector2( 0, 0 )
4/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
4/shape = SubResource( 7 )
4/shape = SubResource( 3 )
4/shape_one_way = false
4/shape_one_way_margin = 1.0
4/shapes = [ {
"autotile_coord": Vector2( 0, 0 ),
"one_way": false,
"one_way_margin": 1.0,
"shape": SubResource( 7 ),
"shape": SubResource( 3 ),
"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 )
} ]
4/z_index = 0
@ -156,10 +156,10 @@ points = PoolVector2Array( 0, 0, 32, 0, 32, 96, 0, 96 )
6/shapes = [ ]
6/z_index = 0
[sub_resource type="CapsuleShape2D" id=3]
[sub_resource type="CapsuleShape2D" id=5]
radius = 20.0
[sub_resource type="CircleShape2D" id=4]
[sub_resource type="CircleShape2D" id=6]
[node name="Map1" type="Node2D"]
@ -178,7 +178,7 @@ format = 1
tile_data = PoolIntArray( 1179654, 0, 8, 1179655, 0, 0, 1245190, 0, 4, 1245191, 0, 5, 2097157, 0, 2, 2097158, 0, 7, 2097159, 0, 5, 2097160, 0, 1, 2162693, 0, 7, 2162694, 0, 6, 2162695, 0, 1, 2162696, 0, 7, 2162697, 0, 3, 2162698, 0, 4, 2162699, 0, 3, 2162700, 0, 7, 2162701, 0, 8, 2162702, 0, 5, 2228227, 0, 7, 2228228, 0, 4, 2228229, 0, 4, 2228230, 0, 0, 2228231, 0, 2, 2228232, 0, 7, 2228233, 0, 7, 2228234, 0, 8, 2228235, 0, 1, 2228236, 0, 4, 2228237, 0, 2, 2228238, 0, 1, 2228239, 0, 4, 2293763, 0, 5, 2293764, 0, 0, 2293765, 0, 2, 2293767, 0, 5, 2293768, 0, 4, 2293769, 0, 1, 2293770, 0, 8, 2293771, 0, 2, 2293772, 0, 6, 2293773, 0, 4, 2293774, 0, 3, 2293775, 0, 3, 2359299, 0, 6, 2359300, 0, 4, 2359301, 0, 3, 2359302, 0, 3, 2359303, 0, 6, 2359304, 0, 3, 2359305, 0, 8, 2359306, 0, 7, 2359307, 0, 1, 2359308, 0, 4, 2359309, 0, 2, 2359310, 0, 3, 2359311, 0, 0, 2359312, 0, 0, 2424835, 0, 0, 2424836, 0, 3, 2424837, 0, 5, 2424838, 0, 3, 2424839, 0, 5, 2424840, 0, 4, 2424841, 0, 1, 2424842, 0, 0, 2424843, 0, 1, 2424844, 0, 0, 2424845, 0, 6, 2424846, 0, 7, 2424847, 0, 8, 2424848, 0, 1, 2424849, 0, 3, 2490371, 0, 7, 2490372, 0, 5, 2490373, 0, 5, 2490375, 0, 4, 2490376, 0, 1, 2490377, 0, 5, 2490378, 0, 1, 2490379, 0, 1, 2490380, 0, 3, 2490381, 0, 4, 2490382, 0, 6, 2490383, 0, 4, 2490384, 0, 0, 2490385, 0, 1, 2555907, 0, 6, 2555908, 0, 0, 2555909, 0, 4, 2555910, 0, 0, 2555911, 0, 2, 2555912, 0, 2, 2555913, 0, 8, 2555914, 0, 2, 2555915, 0, 0, 2555916, 0, 6, 2555917, 0, 4, 2555918, 0, 1, 2555919, 0, 2, 2555920, 0, 6, 2555921, 0, 2, 2555922, 0, 8, 2621443, 0, 2, 2621444, 0, 8, 2621445, 0, 7, 2621446, 0, 0, 2621447, 0, 8, 2621448, 0, 8, 2621449, 0, 7, 2621450, 0, 0, 2621451, 0, 6, 2621452, 0, 6, 2621453, 0, 2, 2621454, 0, 5, 2621455, 0, 8, 2621456, 0, 7, 2621457, 0, 0, 2621458, 0, 8, 2621459, 0, 5, 2686980, 0, 3, 2686981, 0, 6, 2686982, 0, 1, 2686983, 0, 4, 2686984, 0, 0, 2686985, 0, 6, 2686986, 0, 5, 2686987, 0, 4, 2686988, 0, 1, 2686989, 0, 2, 2686990, 0, 8, 2686991, 0, 8, 2686992, 0, 8, 2686993, 0, 8, 2686994, 0, 4, 2752517, 0, 1, 2752518, 0, 4, 2752519, 0, 6, 2752520, 0, 8, 2752521, 0, 2, 2752522, 0, 6, 2752523, 0, 2, 2752524, 0, 8, 2752525, 0, 5, 2752526, 0, 8 )
[node name="interaction_map" type="TileMap" parent="."]
tile_set = SubResource( 2 )
tile_set = SubResource( 4 )
cell_size = Vector2( 32, 32 )
cell_custom_transform = Transform2D( 16, 0, 0, 16, 0, 0 )
centered_textures = true
@ -314,7 +314,7 @@ texture = ExtResource( 4 )
flip_h = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
shape = SubResource( 3 )
shape = SubResource( 5 )
one_way_collision_margin = 96.0
[node name="MapInteraction" type="TouchScreenButton" parent="Player/CollisionShape2D"]
@ -322,7 +322,7 @@ show_behind_parent = true
position = Vector2( -150, -150 )
scale = Vector2( 15, 15 )
z_index = 11
shape = SubResource( 4 )
shape = SubResource( 6 )
action = "map_interaction"
[node name="Tween" type="Tween" parent="."]

Binary file not shown.