2020-10-07 20:24:10 +00:00
extends Node
const SQLite = preload ( " res://addons/godot-sqlite/bin/gdsqlite.gdns " )
2021-04-05 20:59:49 +00:00
var path = " user://storage.db "
2020-10-07 20:24:10 +00:00
var db_name = " RingOfRaces "
var db = null
var verbose = true
# Called when the node enters the scene tree for the first time.
func _ready ( ) :
pass # Replace with function body.
2020-10-08 19:56:03 +00:00
func CreateWorldDatabase ( ) :
print ( " Creating new database " )
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 )
var items : Dictionary = Dictionary ( )
for i in range ( 40 ) :
items [ " id " ] = i
items [ " item_id " ] = 0
items [ " item_name " ] = " No Item "
items [ " amount " ] = 0
items [ " shortdesc " ] = " No item here "
# Insert a new row in the table
db . insert_row ( " player_inventory " , items )
items . clear ( )
2020-10-07 20:24:10 +00:00
func OpenConnection ( ) :
self . db = SQLite . new ( )
2020-10-08 19:56:03 +00:00
var file = File . new ( )
2020-10-07 20:24:10 +00:00
self . db . path = path
self . db . verbose_mode = verbose
2020-10-08 19:56:03 +00:00
var create = false
2021-04-05 20:59:49 +00:00
print ( path )
# This does not seem to work. The file is in the right place, but being recreated everytime. The file is findable in Res:// and C:/ .. But not after the user folder
2020-10-08 19:56:03 +00:00
if ! file . file_exists ( path ) :
2021-04-05 20:59:49 +00:00
print ( " File not existing, so creating new db " )
2020-10-08 19:56:03 +00:00
create = true
2020-10-07 20:24:10 +00:00
self . db . open_db ( )
2021-04-03 14:06:23 +00:00
if create :
2020-10-08 19:56:03 +00:00
CreateWorldDatabase ( )
2020-10-07 20:24:10 +00:00
func OpenConnectionIfClosed ( ) :
if self . db == null :
OpenConnection ( )
func GetInventoryItems ( ) :
OpenConnectionIfClosed ( )
var ret = [ ]
2020-10-08 19:56:03 +00:00
ret = db . select_rows ( " player_inventory " , " " , [ " * " ] )
2020-10-07 20:24:10 +00:00
return ret
2021-04-03 14:06:23 +00:00
2021-04-05 20:59:49 +00:00
func SaveInventory ( player_inventory_items ) :
print ( " Now on inventory save file " )
if ( player_inventory_items == null or len ( player_inventory_items ) != 40 ) :
2021-04-03 14:06:23 +00:00
Global . Log ( " Bad inventory save! " , 3 )
return
OpenConnectionIfClosed ( )
2020-10-07 20:24:10 +00:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass