- Edited
Hello! I am very excited to share with you a bit of code today. This code is a direct translation of Pananag's tutorial on how to make a database for your game in Blender Game Engine. This is a very useful piece of code for which my games rely on, and now that I am turning my Blender Game Engine prototype into Godot for its build and distribution capabilities it was necessary that I translate it over.<br><br>I have taken Pananag's Python code and turned it into Godot Script for your database convenience.<br><br>Before anything further, here are links to Pananag in order to credit him for this excellent insight on how to organize your databases into dictionaries. Thank you Pananag for sharing your code.<br><br>Blender Artists:<br>https://blenderartists.org/forum/showthread.php?302504-Series-Basic-inventory-system-for-the-Blender-Game-Engine<br><br>Youtube main channel:<br><a rel="nofollow" target="_blank"><img width="640" alt="" height="385"></a><br><br>Creating the database:<br><a rel="nofollow" target="_blank"><img width="640" alt="" height="385" title="Image: null"></a><br><br>-------------------------------------<br>This code can be used for:<br>Taking your CSV files and making dictionaries within the Godot Engine that are nicely organized<br>-Making databases that have header attributes with corresponding values<br>-Magic databases<br>-Item databases<br>-Character databases<br>-Enemy databases<br><br><br><br>CSV example:<br>[ID, Name, Birthday, Favorite Weapon, Strength Stat]<br>[0001, Henry, July3rd, Swords, 1400]<br><br>What this code does:<br>The following code takes a .CSV (coma separated value file) and reads each line into an array. The first (top) line in the .CSV is read as an array of what will be used as the HEADERS I.E. ATTRIBUTES of each COLUMN of the .CSV file.<br><br>Example:<br><br>These values are now a dictionary. The definitions are shown on the next line as '' ( empty strings )<br>{ID : '', Name : '', Birthday : '', Favorite Weapon : '', Strength Stat : ''}<br><br>The above dictionary, made from the top line of the .CSV file, will then be added as the definition of each key in a new dictionary made up of the first value of each subsequent line in the .CSV file<br><br>{0001 : {ID : 0001, Name : Henry, Birthday : July3rd, Favorite Weapon : Swords, Strength Stat : 1400} }<br>{0002 : {ID : 0002, Name : Billydilly, Birthday : December40th, Favorite Weapon : Sponges, Strength Stat : -30} }<br>ETC.<br><br><br><img alt="CSV EXAMPLEpng"><br><br>USAGE:<br><br>print(db_characters[CHAR_ID]['Name'])<br>print(db_items[ITEM_ID]['Price'])<br>print(db_monsters[MONSTER_ID]['Drop_1'])<br><br>EDIT:<br>***********************************************************************************************************<br>So we all want to write:<br><br>for key in database.keys():<br>print(key.keys())<br><br>But this does not work. The loop recognizes the keys as only strings, and not as the dicts which they represent.<br><br>In order to loop through the keys of keys like we would like, <br>you have to save each database.csv's first line (header) as a list. This requires editing the code below, which I will do with comments. This will also require adding an extra argument to the func.<br><br>Example:<br><br>
for item in database_1_headers:
node.set_meta(item,database_1[key][item])
print(node.get_meta(item)
<br><br>COMMON ERROR:<br><br>Using different programs to open up the save .csv file can cause formatting differences which will cause problems with how .csv files are read by programs trying to call them with read/write related methods.<br><br>Notably: Sometimes an extra line is added at the end of a .csv file after editing. This will cause the code to give an error. <br><br>I use Notepad++ and OpenOffice's database program to do my .csv files, and in using the two the extra line is added, so I have to open up Notepad++, delete the extra line and save before running the program. Of course, this only happens when editing your .CSV and not just randomly.<br><br><br><br>********************************************************THE CODE****************************************
<br>WITHOUT INDENTS or comments<br><br>
func db_import(csv_file,dict_to_append):#EDITED: In order to loop through keys of keys, EXTRA ARG<br> var file = File.new()<br> file.open(csv_file,file.READ)<br> var pos = 0 #artifact<br> var file_len = file.get_as_text().length() #artifact<br> var is_header = true<br> var attributes_list = []<br> file.seek(0)<br> var temp_dict = {}<br> while !file.eof_reached(): <br> var line = file.get_csv_line()<br> if is_header == true:<br> attributes_list = line<br>for item in attributes_list: #EDITED: In order to loop through keys of keys<br>DICT_HEADERS_LIST.append(item) #EDITED: In order to loop through keys of keys<br> is_header = false<br> else:<br> for i in range(0,attributes_list.size()):<br> temp_dict[attributes_list[i]] = line[i]<br>dict_to_append[line[0]] = temp_dict<br>temp_dict = {} #Edited as of 8/26/2016<br> print(dict_to_append)