Saturday, November 23, 2013

[TUT] Script + XBox 360 controller

A single of the regularly asked function is the possibility of use xbox 360 controller with the script, i will show two ways of obtaining xbox manage crucial presses.

Very first of all i wanna say thanks to pedro2555 for providing information about SlimDX, say thanks to Andrew for supplying info about XNA, thanks to MOrdecki for info about the crucial values for native calls and thanks to "Soyeldiablo" for giving tip about analogue sticks :)

Download the sample project right here, let's use this project as the base for this tutorial.

  • Standard native methods (recommended)
IS_Utilizing_CONTROLLER: Will return correct when you press one button or move an analogue stick in the control, if you move mouse or press an essential in keyboard it will return false once more.
IS_BUTTON_PRESSED: Right here we use two params: 1st one is the controller number, second is the quantity of the key in the controller, if the key is down this will return correct.
GET_POSITION_OF_ANALOGUE_STICKS: Return in the params the X and Y of the analogue sticks.

This is the list of all key values for controller:
BUTTON_Commence = 12
BUTTON_DPAD_Right = 11
BUTTON_STICK_Appropriate = 19


As you can see we want this objects of class Pointer (GTA.Native.Pointer) to be employed as parameter in the Native function get in touch with, the first param of the native method is the controller number.

Also we require to initialize this pointers indicating their kinds, if we mismatch the type the pointer will receive nil as outcome, so be cautious with the type, for this native call the right variety is Int32:
LeftStick_X = New GTA.Native.Pointer(GetType(Int32))
The values go from -127 to 127, idle its .

I wrote a modest class that you can use to deal with all the approaches

  • Microsoft XNA DLL
With Microsoft XNA we can have access to xbox controller and detect when user press an button, what we want? 1st let's download the Microsoft XNA Framework Redistributable four. and set up it. Following installing we need to uncover the DLL that must go with the script, if you use the windows file search you will uncover the dll in this folder probably:

Copy this file to the GTAIV.exe folder, when you release your script you need to have to release this dll too, and tell to the user that it have to be placed in GTAIV.exe folder.

Now in our project let's add an reference to this dll file:

We will need an worldwide object of Microsoft.Xna.Framework.Input.GamePadState:

And in the tick event we require to verify for updates carrying out the adhere to:

If you put an straightforward message in the keyDown occasion you will see that none of the handle buttons trigger this event, so to check if an button of the handle is pressed we require to do this in the tick:

Verify if the handle is connected and then check for every single crucial that we need to know if its pressed, in this case i showed an message indicating if button A is pressed. We also can verify the triggers pressed, the distinction right here is that we have an float worth that goes from to 1 and from to -1:

the identical can be accomplished with Thumb sticks specifying if we are hunting for X or Y value:

Easy right? Yes, that is, the dilemma is when you require to use the button press like an hotkey, we require to develop an global object of sort int64 (or maybe int32) to store the final PacketNumber of the handle, each and every time that we press an button this PacketNumber is elevated, so in the tick we verify if the last Packet number is different from the actual a single and then we think about it an button press event:

and in the tick:

this PacketNumber will alter every single time that we press an button and when we release too or when we change the Thumb or Trigger position.

The most intriguing way is produce boolean variable for each and every button and float variables for each Thumb and Trigger that we program to use in the script:

and makes use of an technique to verify this buttons states:

I'm not positive if we want to send only the XNA dll with the script or the complete packet (Microsoft XNA Framework Redistributable 4.), in my tests just the dll was enough.

Download this script right here

  • SlimDX DLL
Other tool that we can use to make this checks is the SlimDX, you can download it right here, download the version for .Net four. that is used by the ScriptHook. Soon after installing we need to have to locate the dll like we did with the XNA, if you use the windows file search you probably will locate it right here:

Like XNA dll, we want to copy this dll to the GTAIV.exe folder and add as reference in the project.

The object that will do the checks is the following:

Now what modifications is how we get the PacketNumber and how we detect what important is pressed:

Download the script right here

If you do not have an XBox 360 controller you can simulate one particular utilizing an basic gamepad, the result its not quite precise but it helps, to do this you can use this software:

You will need to have to download the main files and the application, put then inside the GTAIV.exe folder and run the application to configure the buttons of your gamepad to behave like an XBox 360 controller )
[TUT] Script + XBox 360 controller
9out of 10 based on 10 ratings. 9 user reviews.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.