Skip to content

Persistent Database in NWN:EE

I'm developing a PW and currently the main missing ingredient is persistent database. I assume that if I don't have a server database set up, then nothing is saved including the characters the players create. Is that correct?

What would you recommend I use for a database? I'm running windows. Should I use NWNX (which is apparently only available on Linux) with "Docker" (a linux simulator)?

I'm trying to implement CNR/HCR, so player crafting skills and their last known location for example is also something that needs to be recorded somewhere so after the server restarts it remembers it.

Comments

  • SherincallSherincall Member Posts: 387
    If you are only looking for the database connection and don't care about other features of NWNX (e.g. functions to modify characters, administration, rules changes, player visibility, discord hooks, etc) there is a windows version available:

    https://github.com/mtijanic/nwnxlite

    All this does is allow you to connect NWN to MySQL on Windows and execute your SQL queries.


    If you find this pointlessly complicated, you can just go with the built-in database with the {Get,Set}Campaign{Int,Float,String,Object} functions. They work reasonably well for small servers.
  • ValeriyaValeriya Member Posts: 57
    I'll try the built-in database first. Can you tell me where all the "Campaign" database info is stored? In the module itself? In a different folder as a separate file? I can't find that info.
  • SherincallSherincall Member Posts: 387
    It is a separate file in the "database/" folder in your user directory. You might not have this folder until a module writes something to it.

    You can also use these wrappers, which make it trivial for you to switch from the built-in to SQL later, without changing your code:
    https://neverwintervault.org/project/nwn1/script/persistence-facade-ee
  • ValeriyaValeriya Member Posts: 57
    Excellent, thanks for the help!
  • SherincallSherincall Member Posts: 387
    Just a note on
    then nothing is saved including the characters the players create. Is that correct?


    That's not quite correct. The characters the players create are saved, their XP/levels are saved, their entire inventory is saved. CNR can be configured to be saved without the database, but it's better if it goes in the database.

    More scripting specific, local variables on the player object are not saved, but local variables on items in the player's inventory (including the hidden ones, like the character 'skin') are saved, and can be used for persistence as well.
  • ValeriyaValeriya Member Posts: 57
    Yes, I now realized that information is saved in the servervault folder.
    BTW I used The Get,Set Campaign functions and it works fine; pretty fast too. It only takes like half a second to update, I don't get why people prefer NWNX for database (I do understand that NWNX can help change the core game I just have no need for that at the moment...wondering about it purely from a database PoV).
  • SherincallSherincall Member Posts: 387
    It depends on what you want to keep in the database. The builtin database is fast enough for basic use cases, but if you hit like.. 100k entries it'll choke horribly. mysql can process millions easily if structured properly.

    mysql also lets you have relational tables and SQL queries that greatly simplify a lot of things. For example, suppose you want to make a loot system; you make one chest where you can put items that will show up as loot, with a script like:

    // CREATE TABLE loot ( INT id PRIMARY KEY AUTO_INCREMENT, INT value, TEXT item ); NWNX_SQL_PrepareQuery("INSERT INTO loot VALUES(?,?)"); NWNX_SQL_PreparedInt(0, GetGoldPieceValue(oLootItem); NWNX_SQL_PreparedObjectFull(1, oLootItem);

    And then when you want a piece of loot, you have:

    object SpawnLoot(object oOwner, int nLowPrice, int nHighPrice) { NWNX_SQL_ExecuteQuery("SELECT item FROM loot WHERE value >= " + IntToString(nLowPrice) + " AND value <= " + IntToString(nHighPrice) + " ORDER BY RAND() LIMIT 1"); return NWNX_SQL_ReadFullObjectInActiveRow(0, oOwner); }

    You can then add stuff like item types so you only spawn weapons, etc. Or sets of items, so goblins spawn different than elves.

    What I wanna say is, it opens many other possibilities that a basic persistence mechanism does not have. A lot of servers are built with the SQL being the central pillar and everything is controlled from it. That way, you can also live edit the data from an external program and have the changes show up in game without any restart.
Sign In or Register to comment.