Jibble

Author Topic: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1  (Read 633 times)

Dualnames

  • Local Moderator
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
1. Prologue:

I know that Wyz is an incredibly busy man, and has barely managed to maintain the source of the AGS Joystick Plugin, given that, and given that I'm also implementing a version of it with SDL, it seems fitting to disengage the two things between them. A majority of the code has been written/based off code by eri0o and Wyz, so kudos go to them! The inclusion of the SDL library allows for two important things:
1) XInput recognition
2) Cross-platform

2. Instructions/Going through the functions

The only thing u have to do, is make sure u put SDL2.dll inside ur debug and compiled folders, otherwise you'll get error messages. The dll will be provided below.

int ControllerCount ()

This returns the number of gamecontrollers found, from 0 to N controllers.





Controller Properties & Functions

The plugin uses a custom typedef which is 'Controller'. Controller is self-explanatory, and has a couple of functions and attributes.

  • int ID

    Returns the ID of the controller.
  • int ButtonCount

    Returns the number of buttons on the controller.
  • int AxesCount

    Returns the number of Axis on the controller.
  • Open (int ID);

    Opens specified controller. (0-15)

    A good practice to do is put this on room_load, or game_start
Code: Adventure Game Studio
  1. Controller*gamepad;
  2. function game_start()
  3. {  
  4.   gamepad = Controller.Open(0);
  5. }
  6.  
  7.  

  • void Close ();

    Closes the controller, probably not really needed to be honest.
  • bool Plugged ();

    Returns if the controller is currently plugged or not (true / false)
  • String GetName ();

    This returns a string containing the name of the gamecontroller. Will return "" on error.
  • int GetAxis (int axis);

    Returns axis by number -32768 to 32768

Code: Adventure Game Studio
  1. if (gamepad.GetAxis(0) < -200)
  2. {
  3. //LEFT
  4. }
  5. else if (gamepad.GetAxis(0) > 200)
  6. {
  7. //RIGHT
  8. }
  9.  

  • int GetPOV();

    Returns POV value. (0-8)

Code: Adventure Game Studio
  1. if (gamepad.GetPOV() == ePOVUp)
  2. {
  3.     cEgo.y--;
  4. }
  5.  

  • bool IsButtonDown (int button);

    Returns true when the specified button is currently down. (0-31)

Code: Adventure Game Studio
  1. int i=0;
  2. String app="";
  3. while (i < gamepad.ButtonCount+2)
  4. {
  5.   app = app.Append(String.Format("button %d is %d[",i, gamepad.IsButtonDown(i)));
  6.   i++;
  7. }
  8. cEgo.SayBackground(app);
  9. //this will parse all the buttons' states and print them on the screen.
  10.  

  • bool IsButtonDownOnce(int button);

    Returns true when the specified button is currently down (single press). (0-31)

Code: Adventure Game Studio
  1. if (gamepad.IsButtonDownOnce(11))
  2.     {
  3.        //click on UI
  4.     }
  5.  

  • void Rumble(int left,int right,int duration);

    Rumbles the Controller for Duration (in loops). Left and right are motors. Values go from 0 to 65535

Code: Adventure Game Studio
  1. if (gamepad.IsButtonDownOnce(11))
  2.     {
  3.       gamepad.Rumble(65535, 65535, 40);    
  4.     }
  5.  

    v1.1

    -Added two new functions

    • void BatteryStatus();

      Returns the status of the controller battery. (-1 - 5) UNKNOWN = -1, LESS THAN 5% = 0, LESS THAN 20% = 1, LESS THAN 70% = 2, 100% = 3, WIRED = 4, MAX = 5

    Code: Adventure Game Studio
    1. lblstatus.Text=String.Format("BATTERY STATUS: %d",gamepad.BatteryStatus());
    2.  

      • int PressAnyKey();

        Returns the first button the player hits on the controller, otherwise returns -1. (0-31)

      Code: Adventure Game Studio
      1. while (gamepad.PressAnyKey()==-1)
      2.     {
      3.       Wait(1);
      4.     }
      5.     player.Say("Button pressed is button %d",gamepad.PressAnyKey());
      6.  




        3. Download links

        AGSController.dll
        SDL2.dll

        *ZIP file containing both*

        4. Source

        The source files can be found on GITHUB
        « Last Edit: 06 May 2019, 18:06 by Dualnames »
        No more military army stuff. I'm alive and back.

        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #1 on: 30 Apr 2019, 08:12 »
        This is really nice Dualnames, fantastic work! Somehow does this revisited version will prevent the error: Joystick is plugged in then you save the game, unplug the joystick and try to restore the game properly fine? Or Joystick is plugged in then you save the game, export the saved game file to another computer with same ags version, unplug the joystick and try to run the game? Because in the version I have of the plugin this will crash. bool isPlugged or bool isValid doesn't seem to fix the problem.

        Dualnames

        • Local Moderator
        • Pretty Badass
          • Dualnames worked on one or more games that won an AGS Award!
          •  
          • Dualnames worked on one or more games that was nominated for an AGS Award!
        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #2 on: 30 Apr 2019, 09:18 »
        Pretty sure, I can specifically run this test for you!

        EDIT: Yep, no crashes!
        « Last Edit: 30 Apr 2019, 09:27 by Dualnames »
        No more military army stuff. I'm alive and back.

        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #3 on: 30 Apr 2019, 10:03 »
        Alright thanks a lot for testing this! Sorry for the dumb question, so it is AGSController.dll different from agsjoy.dll? I don't think if I just replace the plugin my script would still work.

        Dualnames

        • Local Moderator
        • Pretty Badass
          • Dualnames worked on one or more games that won an AGS Award!
          •  
          • Dualnames worked on one or more games that was nominated for an AGS Award!
        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #4 on: 30 Apr 2019, 20:28 »
        You need to replace the functions as well. And it is different. Did a minor update for the upcoming release of AGS (which includes SDL and would cause issues)
        No more military army stuff. I'm alive and back.

        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #5 on: 30 Apr 2019, 22:22 »
        Alright thanks for the further clarification, I look forward to attempt this plugin on a next time.

        Dualnames

        • Local Moderator
        • Pretty Badass
          • Dualnames worked on one or more games that won an AGS Award!
          •  
          • Dualnames worked on one or more games that was nominated for an AGS Award!
        Re: [PLUGIN] AGS Controller Support (OPEN SOURCE)
        « Reply #6 on: 06 May 2019, 18:05 »
        v1.1

        -Added two new functions

        • void BatteryStatus();

          Returns the status of the controller battery. (-1 - 5) UNKNOWN = -1, LESS THAN 5% = 0, LESS THAN 20% = 1, LESS THAN 70% = 2, 100% = 3, WIRED = 4, MAX = 5

        Code: Adventure Game Studio
        1. lblstatus.Text=String.Format("BATTERY STATUS: %d",gamepad.BatteryStatus());
        2.  

          • int PressAnyKey();

            Returns the first button the player hits on the controller, otherwise returns -1. (0-31)

          Code: Adventure Game Studio
          1. while (gamepad.PressAnyKey()==-1)
          2.     {
          3.       Wait(1);
          4.     }
          5.     player.Say("Button pressed is button %d",gamepad.PressAnyKey());
          6.  
            No more military army stuff. I'm alive and back.

            Jack

            • Think deviant
              • I can help with AGS tutoring
              • I can help with scripting
              • I can help with story design
              • Jack worked on one or more games that was nominated for an AGS Award!
            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #7 on: 06 May 2019, 18:23 »
            Cool, Dualnames!

            I also like that you added the value after "wired" called MAX in case the user gets struck by lightning.

            Dualnames

            • Local Moderator
            • Pretty Badass
              • Dualnames worked on one or more games that won an AGS Award!
              •  
              • Dualnames worked on one or more games that was nominated for an AGS Award!
            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #8 on: 06 May 2019, 21:14 »
            It's what SDL returns, im not sure why they have a 100% and a max, but here it is.
            No more military army stuff. I'm alive and back.

            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #9 on: 14 Oct 2019, 19:13 »
            I'm getting the error there was an error loading plugin 'agscontroller.dll', it may depend on another DLL that is missing.

            I have both the files in the correct locations - any ideas?

            Thanks in advance.


            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #10 on: 14 Oct 2019, 19:35 »
            What are the correct locations? Both must also be added on the AGS Editor directory itself.
            « Last Edit: 14 Oct 2019, 19:43 by eri0o »

            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #11 on: 14 Oct 2019, 19:42 »
            I've placed them into the AGS directory, plus in the specific game's debug and compiled folders.

            I'm getting the error message on startup of AGS.

            Thanks

            Re: [PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1
            « Reply #12 on: 14 Oct 2019, 19:46 »
            Ah, in this case, there's a chance the build isn't including the redistributable VC stuff - use MT flag on Visual Studio C++ compiler.

            Can you try the build here? I am not sure they are correctly configured though. (I briefly read the vcxproj xml file but couldn't find mention of the MT flag... Also the build is suspectly small. It still needs the SDL.dll)

            https://github.com/ericoporto/AGS-Controller/releases/tag/1.1.1

            If this is the case (the above build doesn't work), I can fix later. (I don't have VS right now and managed to expire my azure connection credentials for this repo, so I can't build things right now)
            « Last Edit: 14 Oct 2019, 19:54 by eri0o »