Assigning Keyboard Module's function (or AscII) to a Joypad (AGS Controller)

Started by Olleh19, Thu 29/10/2020 17:20:07

Previous topic - Next topic

Olleh19

Quote from: Crimson Wizard on Thu 29/10/2020 18:51:39
Ok, so, have you tried adding the code I suggested above, into the keyboardmovement module?

I'm not sure at which point you are currently.

Code: ags
// Main script for module 'KeyboardMovement'

//****************************************************************************************************
// DEFINITIONS
//****************************************************************************************************

#define DISTANCE 10000// distance player walks in Tapping mode before he stops

enum KeyboardMovement_Directions {
	eKeyboardMovement_Stop, 
	eKeyboardMovement_DownLeft, 
	eKeyboardMovement_Down, 
	eKeyboardMovement_DownRight, 
	eKeyboardMovement_Left, 
	eKeyboardMovement_Right, 
	eKeyboardMovement_UpLeft, 
	eKeyboardMovement_Up, 
	eKeyboardMovement_UpRight 
  //eKeyboardMovement_Punch, 
};

//****************************************************************************************************
// VARIABLES
//****************************************************************************************************

// keycodes as variables for future key customization functions (static variables?):
int KeyboardMovement_KeyDown = 380; // down arrow
int KeyboardMovement_KeyLeft = 375; // left arrow
int KeyboardMovement_KeyRight = 377; // right arrow
int KeyboardMovement_KeyUp = 372; // up arrow
int KeyboardMovement_KeyDownRight = 381; // PgDn (numpad)
int KeyboardMovement_KeyUpRight = 373; // PgUp (numpad)
int KeyboardMovement_KeyDownLeft = 379; // End (numpad)
int KeyboardMovement_KeyUpLeft = 371; // Home (numpad)
int KeyboardMovement_KeyStop = 376; // 5 (numpad)


KeyboardMovement_Modes KeyboardMovement_Mode = eKeyboardMovement_None; // stores current keyboard control mode (disabled by default)

KeyboardMovement_Directions KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop; // stores current walking direction of player character

//****************************************************************************************************
// USER FUNCTIONS
//****************************************************************************************************

//====================================================================================================

static function KeyboardMovement::SetMode(KeyboardMovement_Modes mode) {
	KeyboardMovement_Mode = mode;
}

//====================================================================================================

// key customization functions here

//====================================================================================================

//****************************************************************************************************
// EVENT HANDLER FUNCTIONS
//****************************************************************************************************

Controller*gamepad;
//====================================================================================================

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

    int _dx, _dy;
    int left_frames, right_frames, up_frames, down_frames;
     
    function repeatedly_execute() {
     
            //--------------------------------------------------
            // Pressing mode
            //--------------------------------------------------
     
            if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Pressing) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
              // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function
        
      bool left, up, right, down;
        
      if (IsKeyPressed(KeyboardMovement_KeyUpLeft)) { up = true; left = true; }
      if (IsKeyPressed(KeyboardMovement_KeyUp)) up = true;    
      if (IsKeyPressed(KeyboardMovement_KeyUpRight)) { up = true; right = true; }
      if (IsKeyPressed(KeyboardMovement_KeyRight)) right = true;    
      if (IsKeyPressed(KeyboardMovement_KeyDownRight)) { down = true; right = true; }  //I'll add the rest later if this works ofc!
      if (IsKeyPressed(KeyboardMovement_KeyDown)) down = true;    
      if (IsKeyPressed(KeyboardMovement_KeyDownLeft)) { down = true; left = true; }
      if (IsKeyPressed(KeyboardMovement_KeyLeft) || gamepad.GetAxis(0) > 20) left = true; 
      
      if (IsKeyPressed(KeyboardMovement_KeyStop)) { up = false; left = false; down = false; right = false; }
      
      if (left) left_frames++; else left_frames = 0;
      if (right) right_frames++; else right_frames = 0;
      if (left && right) {
        left = left_frames < right_frames;
        right = !left;
      }
      if (up) up_frames++; else up_frames = 0;
      if (down) down_frames++; else down_frames = 0;
      if (up && down) {
        up = up_frames < down_frames;
        down = !up;
      }
      
      int dx = right * DISTANCE - left * DISTANCE;
      int dy = down * DISTANCE - up * DISTANCE;
      
      if (dx != _dx || dy != _dy) player.WalkStraight(player.x + dx, player.y + dy, eNoBlock); // walk player character to the new coordinates
      
      _dx = dx; _dy = dy;
    }
//====================================================================================================

function on_key_press(int keycode) {

	//--------------------------------------------------
	// Tapping mode
	//--------------------------------------------------

	if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Tapping) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
	  // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function

	KeyboardMovement_Directions newdirection; // declare variable storing new direction

	// get new direction:
	if (keycode == KeyboardMovement_KeyDownRight) newdirection = eKeyboardMovement_DownRight; // if down-right key pressed, set new direction to Down-Right
	else if (keycode == KeyboardMovement_KeyUpRight) newdirection = eKeyboardMovement_UpRight;
	else if (keycode == KeyboardMovement_KeyDownLeft) newdirection = eKeyboardMovement_DownLeft;
	else if (keycode == KeyboardMovement_KeyUpLeft) newdirection = eKeyboardMovement_UpLeft;
	else if (keycode == KeyboardMovement_KeyDown) newdirection = eKeyboardMovement_Down;
	else if (keycode == KeyboardMovement_KeyLeft) newdirection = eKeyboardMovement_Left;
	else if (keycode == KeyboardMovement_KeyRight) newdirection = eKeyboardMovement_Right;
	else if (keycode == KeyboardMovement_KeyUp) newdirection = eKeyboardMovement_Up;
	else if (keycode == KeyboardMovement_KeyStop) newdirection = eKeyboardMovement_Stop; // if stop key pressed, set to stop player character

	if (newdirection != KeyboardMovement_CurrentDirection) { // if new direction is different from current direction

		if (newdirection == eKeyboardMovement_Stop) player.StopMoving(); // if new direction is the Stop command, stop movement of player character
		else { // if new direction is NOT the Stop command

			int dx, dy; // declare variables storing new walk coordinates
			if (newdirection == eKeyboardMovement_DownRight) {
				dx = DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpRight) {
				dx = DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_DownLeft) {
				dx = -DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpLeft) {
				dx = -DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Down) {
				dx = 0;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Left) {
				dx = -DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Right) {
				dx = DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Up) {
				dx = 0;
				dy = -DISTANCE;
			}

			player.WalkStraight(player.x + dx, player.y + dy, eNoBlock); // walk player character to the new coordinates
		}
		KeyboardMovement_CurrentDirection = newdirection; // update current direction to new direction

	}
	else { // if new direction is same as current direction
		player.StopMoving(); // stop player character
		KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop; // update current direction
	}

}

//====================================================================================================

function on_event(EventType event, int data) {

	if (event == eEventLeaveRoom) KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop;

}

//====================================================================================================

Olleh19

SUCCESS! It actually works.

I'm surprised. I thought i had to do syntax in the repeatedly???

Now with the Action Button on the keyboard Z, still tho.
Thats its own "function" i guess, its not part of the keyboard module. alltho i ofc would like it to be.


Crimson Wizard

Quote from: Olleh19 on Thu 29/10/2020 18:56:32
I'm surprised. I thought i had to do syntax in the repeatedly???

You probably should check for controller axes in repeatedly_execute. What "syntax" are you refering to?

Quote from: Olleh19 on Thu 29/10/2020 18:56:32
Now with the Action Button on the keyboard Z, still tho.
Thats its own "function" i guess, its not part of the keyboard module. alltho i ofc would like it to be.

I don't understand what you mean here though.

Olleh19



Code: ags
if (gamepad.GetAxis(0) < -20) \


{
do stuff
}



That type of "function" where you clearly should add forexample i guess the left button, but its not being used now, its been solved another way, which interests me.

So what i mean is here is that version of the button im going to use for the Z assigned keyboard.

As the example from the ags controller thread on here:

Code: ags
    if (gamepad.IsButtonDownOnce(11))
        {
           //click on Z button to get a punch.."guess working again"
        }
     


my Z button is in the Actions. This type of code not posting the whole code, no point in doing that.. Then i "get it". I hope....

Code: ags



Actions.Asc

   if (IsKeyPressed(eKeyZ))                  //PUNCH BUTTON HERE COMES THE MESS; Här börjar RÖRAN :DDDDDDDDD! 
  
   
{
    
    
    
    
    

    if (cAxl.Loop==2 || cAxl.Loop==3 || cAxl.Loop==4 || cAxl.Loop==5) //RIGHT PUNCH HIT, All loops are RIGHT/positioned player v_Axl_WalkAction 

{
    


Khris

Here's my take: https://pastebin.com/raw/8CKW9FAY

Works fine for me with the template I found the KM 1.02 module in, the 3.43 Sierra one.

I also chose a smaller angle for diagonal walking, this a) fits the perspective better and b) causes AGS to use the sideviews.

Olleh19

Quote from: Khris on Thu 29/10/2020 19:06:00
Here's my take: https://pastebin.com/raw/8CKW9FAY

Works fine for me with the template I found the KM 1.02 module in, the 3.43 Sierra one.

I also chose a smaller angle for diagonal walking, this a) fits the perspective better and b) causes AGS to use the sideviews.

I replaced the entire script (guessing that was your idea). I get a error Parse error unexpect joystick line 60:

Edit: I think perhaps your script is based on the old joystick plugin???
I've tried editing it, but i still get errors..

Khris

Yeah, it's the one by WyZ from August 2010. Didn't know there's a newer version?

If you link me to the newer version, I'll fix the script.

Olleh19

Quote from: Khris on Thu 29/10/2020 19:25:00
Yeah, it's the one by WyZ from August 2010. Didn't know there's a newer version?

If you link me to the newer version, I'll fix the script.

https://www.adventuregamestudio.co.uk/forums/index.php?topic=57129.0

Edit: I'm getting a scary vibe now, using Crimsons version my Left button completely flips out. It just moves constantly to the left, when the right button works perfectly fine.
I hope my controller isn't broken. Have to try it on my laptop..

Edit Again: tried an emulator, my controller is fine. Something is not doing it in the code..Whatever that is!

Khris

Right, here's the version for the AGSController.dll plugin: https://pastebin.com/raw/G8RwyhBA

Regarding the character moving on its own, my left stick is pretty severely off the center if I don't touch it, at about -3000, -7000. That's why I set the dead zone to +/- 10000 in my script. The maximum value is +/- 32768 though, so that's not a problem.

Olleh19

Quote from: Khris on Thu 29/10/2020 19:59:30
Right, here's the version for the AGSController.dll plugin: https://pastebin.com/raw/G8RwyhBA

Regarding the character moving on its own, my left stick is pretty severely off the center if I don't touch it, at about -3000, -7000. That's why I set the dead zone to +/- 10000 in my script. The maximum value is +/- 32768 though, so that's not a problem.

Ahh, that's great to hear.
Undefined token 'iblGameName' i get tho.

edit: i just deleted, works perfectly, amazing job Khris!

Khris

Right, I was debugging the axis values handling  :-D
Glad it's working now!

Olleh19

SOLVED: I've just put it in the wrong place. I was not thinking correctly i believe. I tried to run ekeyZ || gamepad but outside the function in repeately, it should ofc have been INSIDE the function Player_Actions()
Thank god!


The original problem was: The IsKeyPressed Z will NOT assign to the gamecontroller, but the solution is now added.


Problem 2 Solved: How to get Gamepad to work for a Custom scripts functions:

To get it to work in your own Custom scripts. This i did not get to work first either, it's just trial & error, but it turns out you need to pretty much "clone" the globalscript first lines. You need to add this to get the gamepad working inside the custom script:

Code: ags


//top of your custom script.asc before all other functions are introduced, just like in your globalscript...

Controller*gamepad;



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







Code: ags

//Global script.asc      


Controller*gamepad;
function game_start()
{  


//This is NOT for the custom script solution, this is a a separate solution. If you want the gamepad to work in Globalscript only and dont care about doing your Custom functions in other scripts.
                                         

  gamepad = Controller.Open(0);

cAxl.SetIdleView(2, -30);
cEnemy1.SetIdleView(3, -30); 

 Mouse.ControlEnabled=false;
 Mouse.Visible=false;
 
KeyboardMovement.SetMode(eKeyboardMovement_Pressing);


  
   // set KeyboardMovement movement mode
  

}


function Player_Actions()  //here is the function..It's declared (if that is what it's called in Globalscript.ash
{
  

  
{
 
 player.Say "(This would work if i push the Square button on my gamecontroller");
   
    cAxl.Baseline=1000;                 // This makes him hit in the face, or else the punches get's behind the enemy's Body. 
    cEnemy1.Baseline=1;                 //Try to make this based on Enemy's Y positions instead, so it calculates where it can hit and not..
 
   
   if (IsKeyPressed(eKeyZ) || gamepad.IsButtonDownOnce(3))            //PUNCH BUTTON    - Here is the solution. gamepad needed to be in the actual function NOT OUTSIDE IT in the globalscript; DOH!!! 
  
   
{
    


    if (cAxl.Loop==2 || cAxl.Loop==3 || cAxl.Loop==4 || cAxl.Loop==5) //RIGHT PUNCH HIT, All loops are RIGHT/positioned player v_Axl_WalkAction 

{
    


      cAxl.Animate (10, 0, eOnce, eNoBlock); 

      cAxl.SetIdleView(5, 0);                                                       

      cAxl.Baseline=0;
}
  



SMF spam blocked by CleanTalk