[PLUGIN] AGS Controller Support (OPEN SOURCE) - v1.1

Started by Dualnames, Tue 30/04/2019 03:20:35

Previous topic - Next topic

Dualnames

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: AGS

Controller*gamepad;
function game_start()
{  
  gamepad = Controller.Open(0);
}



  • 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: AGS

if (gamepad.GetAxis(0) < -200)
{
//LEFT
}
else if (gamepad.GetAxis(0) > 200)
{
//RIGHT
}


  • int GetPOV();

    Returns POV value. (0-8)

Code: AGS

if (gamepad.GetPOV() == ePOVUp)
{
    cEgo.y--;
}


  • bool IsButtonDown (int button);

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

Code: AGS

int i=0;
String app="";
while (i < gamepad.ButtonCount+2)
{
  app = app.Append(String.Format("button %d is %d[",i, gamepad.IsButtonDown(i)));
  i++;
}
cEgo.SayBackground(app);
//this will parse all the buttons' states and print them on the screen.


  • bool IsButtonDownOnce(int button);

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

Code: AGS

if (gamepad.IsButtonDownOnce(11))
    {
       //click on UI
    }


  • 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: AGS

if (gamepad.IsButtonDownOnce(11))
    {
      gamepad.Rumble(65535, 65535, 40);     
    }


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: AGS

lblstatus.Text=String.Format("BATTERY STATUS: %d",gamepad.BatteryStatus());


  • int PressAnyKey();

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

Code: AGS

while (gamepad.PressAnyKey()==-1)
    {
      Wait(1);
    }
    player.Say("Button pressed is button %d",gamepad.PressAnyKey());





3. Download links

AGSController.dll
SDL2.dll

*ZIP file containing both*

4. Source

The source files can be found on GITHUB
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Vincent

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

#2
Pretty sure, I can specifically run this test for you!

EDIT: Yep, no crashes!
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Vincent

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

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)
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Vincent

Alright thanks for the further clarification, I look forward to attempt this plugin on a next time.

Dualnames

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: AGS

lblstatus.Text=String.Format("BATTERY STATUS: %d",gamepad.BatteryStatus());


  • int PressAnyKey();

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

Code: AGS

while (gamepad.PressAnyKey()==-1)
    {
      Wait(1);
    }
    player.Say("Button pressed is button %d",gamepad.PressAnyKey());

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Jack

Cool, Dualnames!

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

Dualnames

It's what SDL returns, im not sure why they have a 100% and a max, but here it is.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Aaron Best

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.


eri0o

What are the correct locations? Both must also be added on the AGS Editor directory itself.

Aaron Best

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

eri0o

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)

Blackthorne

Yep, I'm getting the same error as Aaron.  "error loading plugin 'agscontroller.dll', it may depend on another DLL that is missing."
-----------------------------------
"Enjoy Every Sandwich" - Warren Zevon

http://www.infamous-quests.com

JanetC

Quote from: Blackthorne on Sun 05/04/2020 16:44:30
Yep, I'm getting the same error as Aaron.  "error loading plugin 'agscontroller.dll', it may depend on another DLL that is missing."

Was this ever resolved? I'm getting the same error. I'm using AGS 3.4.1 Patch 3.


JanetC

OK the solution is to not use the SDL2.dll provided.

Use the 32-bit version from here:

https://www.libsdl.org/download-2.0.php

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

morganw

Quote from: Dualnames on Thu 17/09/2020 21:59:43
Am I supplying a 64bit version of SDL2.dll?

Yes.

Code: ags
$ file SDL2.dll 
SDL2.dll: PE32+ executable (DLL) (console) x86-64 (stripped to external PDB), for MS Windows

Stranga

Hello there! I've managed to get the plugin to work and it works fine with my controller. I am having a little trouble setting it up though. Is there any way to use these in the on_keypress function? A lot of my menu navigation code is in there and I sorta don't want to re do everything in an rep_ex function to control gamepad input(If possible that is!). Any help would be greatly appreciated.

P.S I forgot to mention, the reason I want to use it in the on_keypress function is if I hold a button down in the rep_ex function it loops repeatedly and I'm not sure if there's a way to check if the POV/dpad has only been pressed once?

Dualnames

I believe there is a function called IsButtonDownOnce, does that not work with pov,dpad?

If not u can set a simple check

Code: AGS

bool LEFT=false;
bool RIGHT=false;
bool UP=false;
bool DOWN=false;

function HitNoDirection()
{
LEFT=false;
RIGHT=false;
UP=false;
DOWN=false;
}

function repeatedly_execute()
{

if (gamepad.GetAxis(0) < -200 && !LEFT)
{
 HitNoDirection();
LEFT=true;
//LEFT
}
else if (gamepad.GetAxis(0) > 200 && !RIGHT)
{
 HitNoDirection();
RIGHT=true;
//RIGHT
}
else if (gamepad.GetAxis(1) > 200 && !DOWN)
{
 HitNoDirection();
DOWN=true;
//DOWN
}
else if (gamepad.GetAxis(1) < -200 && !UP)
{
 HitNoDirection();
UP=true;
//UP
}
else 
{
 HitNoDirection();
}
}


Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SMF spam blocked by CleanTalk