File access (read and write)

IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
Hi, I've done some googling (read and write files site:http://forums.unknownworlds.com/) and I can't seem to find a completely definitive answer as to the current state of working with files and ns2 mods.

What I want to do is make a config file (text encoded, either json or txt). Then I want to read data from it and write out data to it.

I have a workaround in mind if this isn't possible which doesn't involve reading and writing files but isn't exactly ideal (prebaking presets into the mod source code).

So what's the current status on file access for ns2 mods? Back in 2010 I read that there were rumblings to enable something that would let me do what I want to do but I haven't seen anything conclusive as to whether it exists or not.

Comments

  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    You can read and write files just fine in NS2. Look at all those XML files floating around the game directories containing all those settings, they get read and written to all the time...
  • GhoulofGSG9GhoulofGSG9 Join Date: 2013-03-31 Member: 184566Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Reinforced - Supporter, WC 2013 - Supporter, Pistachionauts
    edited August 2015
    You can handle files just normally via the lua IO library (write access is restricted to the config:// path aka %appdata%/Natural Selection 2/).

    The game files path can be accessed via game:// and the users temp folder via temp://

    For json de-/encoding ns2 includes dkjson ( can be accessed via global json ).

    The methods of core/ConfigFileUtility.lua might help you out as-well ;)

    Beside that there are various Shared API methods to handle game asset files and to get details about the File System, have for that a look into the docs/api/Shared.json file.

    And last but not least there is Script.Load to parse and load lua files from the File System into the Lua VM.
    Soul_Rider wrote: »
    You can read and write files just fine in NS2. Look at all those XML files floating around the game directories containing all those settings, they get read and written to all the time...

    TinyXML is not accessible via FFI links, so you would need to include a lua-based XML parser into your mod (a list of those can be found at the luawiki)
  • IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
Sign In or Register to comment.