[English] Creating own dbt-tables (originally by Sublogic)

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • [English] Creating own dbt-tables (originally by Sublogic)

      From the user sublogic, written in the Runeforge Forum, 01 February 2015

      Just figured this out myself, which prompted to overhaul my experimental mods greatly, to potentially make things a lot easier in the future (both to develop and to allow others to modify simple settings). Thought I would share this to those who may have the need but have not discovered the means.

      The need and the road so far:
      * In many mods, we usually have a tabular kind of data that we need to refer to often.
      * Naively, this can be done by using a massive if-else statement.
      * Then I started to put that in a function so that it can be reused elsewhere. Often, having a function just to return size of some virtual list so that I don't have hard-code that value in more than one location.
      * Then, I try to explore using globals (setting it once, on first use, then used by others), but requires tricks to "serialize" an array into simple values.
      * Then, I realised how troublesome it was to add another item, which will require script changes, and every time a risk to breaking something...
      * Then, I decided to invest time into discovering how dbt works and to design scripts such that adding new "items" of similar type will no longer require script changes, and that various "magic constants" are instead correctly derived from dbt (reflecting changes if necessary)

      The Goal:
      * Future changes should also be easily concentrated in one place, instead of being spread over many areas in many script files.
      * Once the script is "done", adding new items should not need script changes anymore (which should make the task easier). It should be just adding new dbt items and other resource files like textures/models/sound etc.

      Step 1. Design and Create a new dbt
      Create a new empty text file, e.g. MyNewTable.dbt in the DB subdirectory from the game's main directory.
      Copy the following "sample" content. (the comments are probably off, but I'm just lazy)

      The first 2 columns most commonly used are provided. The "id" column is mandatory. The "name" column is useful for human reading during debugging and such.
      The row under "Table Description:" is the schema, where each column, delimited by the pipe character "|", consists of a string Name, a type (INT/FLOAT/STRING/LIST) and something which I am not sure of but usually -1 for first id column, and 0 for the rest.

      The data is specified after the line "Data:" where each value corresponds to a column in the specified order, delimited by WHITESPACE(any number of space/tab) and must end with a pipe character "|" (Note: this is very important. In my modding, one of the most common mistakes in dbt changing is forgetting or accidentally deleting the last pipe character. The second most common mistake is forgetting to enclose a string properly in double quotes)

      Comments are lines starting with "//" which will be ignored when the dbt is being parsed.


      1. //Table File, Version 1.02 (c) 2004 4head studios
      2. //Exported
      3. Table Description:
      4. "id" INT -1 |"name" STRING 0 |
      5. Data:
      6. 0 "unknown" |

      You can add new columns by adding to the schema and adding a new corresponding value in each row.
      Example, if I want to add a new column called "mydata" which is a FLOAT type, I will change the above dbt contents to the following:


      1. //Table File, Version 1.02 (c) 2004 4head studios
      2. //Exported
      3. Table Description:
      4. "id" INT -1 |"name" STRING 0 |"mydata" FLOAT 0 |
      5. Data:
      6. 0 "unknown" 3.14 |

      Step 2. Add it to tables.dbt
      This is what got me when I first failed to use my own dbt.
      The dbt created will not be available in-game unless they are first declared in this dbt itself (making tables.dbt possibly the only table which is always read by the game engine).

      To add your table, add in a new line at the end, using an "id" which is not used, and use the filename(excluding the ".dbt" extension) as the "exportname" value. Below shows an example assuming my new dbt file was named "MyNewTable.dbt".


      1. //Table File, Version 1.02 (c) 2004 4head studios
      2. //Exported
      3. Table Description:
      4. "id" INT -1 |"exportname" STRING 0 |
      5. Data:
      6. 1 "textcategories" |
      7. 19 "tables" |
      8. 26 "Properties" |
      9. 27 "PropertiesTypes" |
      10. ...
      11. 102 "Lordship" |
      12. // including in my custom table
      13. 103 "MyNewTable" |
      Alles anzeigen

      Step 3. Use it in your script
      By using the "GetDatabaseValue" function.

      The following is copied from the script function documentation.


      1. Number or String GetDatabaseValue(String TableName, Number RowID, String ColumnName)
      2. Parameters:
      3. String TableName :
      4. The name of the database table.
      5. Number RowID :
      6. The ID of the row.
      7. String ColumnName :
      8. The name of the column for the desired value.
      9. Return Value:
      10. Number or String :
      11. The desired entry.
      12. Description:
      13. Returns an entry from a database table.
      Alles anzeigen

      To use it on our table, example to extract the column "mydata" which I added, use the following:


      1. local rowid = 0 -- the value in the "id" column to look for
      2. local mydata = GetDatabaseValue("MyNewTable", rowid, "mydata")
      3. -- probably a good idea to check if the value exists
      4. if( mydata ~= nil ) then
      5. -- use the data
      6. end

      Step 4? Database-Script Synergy?
      This tutorial just teaches how to create and use dbts accessible to in-game scripts. However, it still take a good modder to design how dbts should be use with respect to his/her scripts. The possible ways of using dbts would stretch into rdbms design stuff which would be too much to cover, but with the right use, it can greatly streamline your scripts (e.g. avoid tall "if-else towers") towards trying to achieve to "Goal" listed near the start of this tutorial.

      Good Modding to all ;)