onsdag 22 juli 2015

GTA V Scripting PC Part 3: Keyboard and Controller support

Keyboard support example

For keyboard support you need these functions:

bool get_key_pressed(int nVirtKey)
{
    return (GetAsyncKeyState(nVirtKey) & 0x8000) != 0;
}

We need to check if the key is valid with isprint(): http://www.cplusplus.com/reference/cctype/isprint/
We also want a delay between possible keystrokes. We use GetTickCount() for this:

DWORD trainerResetTim;

void reset_mod_switch()
{
    trainerResetTime = GetTickCount();
}

We want to get the key pressed every 400 milliseconds. Insert is used as key if activateKeyChar is not a valid key:

bool on_off_switch_pressed(){
    if (isprint(activateKeyChar)){ // activateKeyChar is your key
        return ((GetTickCount() > trainerResetTime + 400) &&   get_key_pressed(activateKeyChar));
    }
    else{
        return ((GetTickCount() > trainerResetTime + 400) && get_key_pressed(0x2D));
    }
}

Read the previous post about how to read from the .ini file. You can in this way let the user choose a key code from here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx and write it in the .ini file so that your mod can get it and use it.

Now you can in your script loop check if your button is pressed:

if (on_off_switch_pressed()){
// do something
}

Controller support example

Controller support is similar:

bool on_off_mod_with_controller_switch_pressed(){
    return ((GetTickCount() > trainerResetTime + 400) && isControllButtonPressed(activateControllerButtonChar)
        && canActivateWithController);
}

canActivateWithController is a bool used to make it so that the controller only is usable when wanted.

Controller button keys:http://pastebin.com/X6X2hUB7

int xInputIndex = 2; // always 2
bool isControllButtonPressed(int controllerButton){
    if (canActivateWithController){
        if (CONTROLS::IS_CONTROL_PRESSED(xInputIndex, controllerButton)){
            return true;
        }
    }
    return false;
}



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

How to make scripts for GTA V PC

Scripting mods for GTA V is actually quite easy. I will show you how to create some neat stuff. First, let's watch this GTA V scripting tutorial made by Johnny Manson, about how to get started and making your first script.


Johnny is directly using C++ to code with. You can also add scripts that let you write in .NET and LUA. In this tutorial we will only write in C++

So, Jonny gave us a template to start with. I made it even smaller and you can check it out here: code.
Mods are of course usually larger. You can remove the line "PED::SET_PED_CAN_BE_DRAGGED_OUT(playerPed, false);" and replace with something else.
Download the latest Script Hook SDK and replace everything in the file "script.cpp" with the contents of code.

Before you TAB down from the game to make changes in your code in visual studio, hold CTRL and press R. A small beep will sound which means that all mods are inactivated. You can now create a new version of your mod and replace the old one with it. When you're done press CTRL+R again to reload all your mods. You should hear three beeps. Note that for this to work you need an empty file named "ScripthookV.dev" in your game folder.

When you start scripting you need some sources and information about how to do different things. The above all most important source is Alexander Blades http://www.dev-c.com with the native database /nativedb/. Not everything is known about how functions work or which parameters are used. Sometimes you need to guess or try and error.

When your mod is ready for it's first release you can publish it on sites like gta5-mods.com and gtainside.com.

In the next post we will look into how to read and write to configuration files (.ini).