Log in to reply
 

[C#] How to make Script create INI file with Keybindings



  • Re: How to make and use INI files [.NET]

    I don't know where to post this, i'm sorry if this is the wrong forum for this topic.

    I watched the video above to make my script read and load the Keybindings from the INI file, and it does work. But I have to create the INI file manually with the Keybindings and package it with Mod/Script, because the script doesn't create the ini file on its own.

    How can I make it so when the script loads in-game, the script automatically creates the INI file with the Keybindings in the "scripts" folder?



  • @TheTechGenius Well, you could have just posted this as a reply of the original topic, instead of a whole new topic :P Anyway, what you're looking to do is write a new file to the file system. It doesn't really have to do with GTA/Script Hook programming, you can do it with plain old C# libraries - here's a guide from Microsoft: How to: Write to a Text File



  • @TheTechGenius ScriptSettings cannot be created as a new object, so you're going to have to create the new file line by line in code.

    Add using System.IO; at the top of your script with the other using statements..

    Then you could do something like:

    private void CreateSettingsFile()
    {
        string _fileName = "scripts\\myModSettings.ini";
    
        if (!File.Exists(_fileName))
        {
            using (StreamWriter sw = File.CreateText(_fileName))
            {
                sw.WriteLine("[KEYS]");
                sw.WriteLine("ACTIVATIONKEY = F10");
                sw.WriteLine("ANOTHERKEY = J");
                sw.WriteLine("ONEMOREKEY = K");
            }
        }
    }
    

    That would check to see if the file exists and if not, would create it and write that text to it. If you wanted any other sections, add more lines like the sw.WriteLine("[KEYS]"); line with whatever section name you want and then the values below that.

    Or you could store those values in a string[] and loop through the elements in turn... whichever way you prefer really.



  • Oh wow, thank you so much! That is exactly what I was looking for!

    Thank you again!



  • @rappo said in [C#] How to make Script create INI file with Keybindings:

    @TheTechGenius Well, you could have just posted this as a reply of the original topic, instead of a whole new topic :P Anyway, what you're looking to do is write a new file to the file system. It doesn't really have to do with GTA/Script Hook programming, you can do it with plain old C# libraries - here's a guide from Microsoft: How to: Write to a Text File

    Oh yeah sorry about making a new topic. But when I went to reply to the topic about using INI files, it said that the Topic is over X months old, and it asked me if I wanted to make a new topic. So I thought that is what I was suppose to do.

    Sorry about that.



  • @TheTechGenius You're welcome, I always find that an example that fits the situation, is much better to relate to.


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.