fredag 10 juli 2015

GTA V Scripting PC Part 2: How to read and write from configuration files (.ini).

Often you want your script to be customizable. This can be done by reading and writing to a configuration file that the script uses.

Files can be set to be read from continuously, which lets you make changes while playing and those changes to be updated directly without having to restart the game.

First, lets create a new file in your GTA5 folder where your GTA5.exe is located. Name it "VehicleColor.ini". Make sure the file ends with .ini and not .txt.
 Open the file and add this:

[Vehicle_Color]
red=50
green=50
blue=200

Save the file and start Visual Studio. For this new project you need a new copy of the Script Hook SDK. You can go back and watch the installation tutorial that Johnny Manson made if you like, linked to in my last post.

Inside the SDK, open up the project called "NativeTrainer.sln" inside the samples folder. Now, replace everything inside the "script.cpp" file with the code here.

With this simple mod we want to change the color of the vehicle we are currently using. So we need to get a reference to the players car. Replace this line "PED::SET_PED_CAN_BE_DRAGGED_OUT(playerPed, false);"
with
if (PED::IS_PED_IN_ANY_VEHICLE(PLAYER::PLAYER_PED_ID(), true)){
Vehicle playerVeh;
}

Now we need the RGB values from our .ini file so that we can paint our vehicle.

Add these values and function to the top of the script.

int red, green, blue;

void updateConfigValues(){
    red = GetPrivateProfileInt("Vehicle_Color", "red", -1, ".\\VehicleColor.ini");
    green = GetPrivateProfileInt("Vehicle_Color", "green", -1, ".\\VehicleColor.ini");
    blue = GetPrivateProfileInt("Vehicle_Color", "blue", -1, ".\\VehicleColor.ini");
}

Now it's time to do something with our RGB value.

VEHICLE::SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(playerVeh, red, green, blue);
VEHICLE::SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(playerVeh, red, green, blue);
VEHICLE::SET_VEHICLE_TYRE_SMOKE_COLOR(playerVeh, red, green, blue);    VEHICLE::_SET_VEHICLE_NEON_LIGHTS_COLOUR(playerVeh, red, green, blue);

There's no need to run this too often so we tell the script to wait for 2 seconds. Under update_features(); in the while loop add "WAIT(2000);"

Now it should look something like this

Now when you're done, build the solution in Visual Studio and place the .asi file into your GTA5 folder where your GTA5.exe is located. TAB down during gameplay, open up your VehicleColor.ini file and change the three numbers/colors to something else (0-255). Save the file and TAB back into the game. Your vehicles color should now have been changed.

Now it's time to test it and add more code to it. :)

Writing to a file is pretty straight forward, it looks something like this:

WritePrivateProfileString(TEXT("Vehicle_Color"),
    TEXT("red"),
    TEXT("255"),
    TEXT(".\\VehicleColor.ini");
    }

Next post about support for keyboard and controller http://mwasteson.blogspot.se/2015/07/gta-v-scripting-pc-part-3-keyboard-and.html

Inga kommentarer:

Skicka en kommentar