Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: skooperstooper on Thu 28/07/2022 22:49:14

Title: How to limit character walking to left and right, no up and down?
Post by: skooperstooper on Thu 28/07/2022 22:49:14
Sorry, I've been searching for answers and rarely find what I'm looking for despite browsing the manual, the forum, etc., probably because I'm using the wrong keywords or phrasing. Also, the forum search specifically gives me an error and says to try again later.

I just started with AGS and went through the tutorial using the Sierra template. It was a breeze, very useful. Now I'm moving onto tweaking the template to fit my needs and I've been figuring everything out bit by bit except movement.

The default behavior is that the player can walk left, right, up and down. My game will have no room depth. I only want the player to be able to walk left or right, with up and down controls causing the player to face up or down. Not actually move.

I can't find anything about how to turn moving up and down off. If it's a character setting, a room's walkable area setting, if it can even be done without writing my own script. Every time I search for character movement, walking, directions, controls, I get info about animations, not movement within the room itself.

Can you point me in the right direction? Thanks!
Title: Re: How to limit character walking to left and right, no up and down?
Post by: fernewelten on Fri 29/07/2022 02:09:43
Make the walking area of the room a small band (wide but only a few pixels high) â€" would this work for you? Ego will be forced to go either left or right because there isn't the vertical space in the band to allow other moves.

I don't know with how little vertical space you can get away with. A band only one pixel high might be too risky: If Ego gets off the band, e.g., while turning, they will freeze in place and not be able to move at all.
Title: Re: How to limit character walking to left and right, no up and down?
Post by: newwaveburritos on Fri 29/07/2022 03:28:15
I've tried to do this before and a very narrow walkable area works but it can get weird with the pathfinding since I was finding that EGO would get sort of caught at places.  I think the way I solved it was by coding something super sloppy where a mouse click on the right of EGO moved EGO right a little bit and vice versa for left with no walkable area at all.
Title: Re: How to limit character walking to left and right, no up and down?
Post by: Crimson Wizard on Fri 29/07/2022 04:00:35
Quote from: skooperstooper on Thu 28/07/2022 22:49:14
The default behavior is that the player can walk left, right, up and down. My game will have no room depth. I only want the player to be able to walk left or right, with up and down controls causing the player to face up or down. Not actually move.

If you are speaking of keyboard controls, and using default template where these are implemented as KeyboardMovement script, then the solution is to find where the script makes character walk (calling "WalkStraight") and replace that with "FaceDirection" command for up/down presses.

For instance it has this:
Code (ags) Select

  if (old.x != move.x || old.y != move.y)
  {
    player.WalkStraight(player.x + move.x * distance, player.y + move.y * distance, eNoBlock);
  }

This may be replaced with
Code (ags) Select

  if (old.x != move.x)
  {
    player.WalkStraight(player.x + move.x * distance, player.y, eNoBlock);
  }
  else if (old.y < move.y)
  {
    player.FaceDirection(eDirectionDown);
  }
  else if (old.y > move.y)
  {
    player.FaceDirection(eDirectionUp);
  }






For mouse controls, supposing you have something like this, originally:
Code (ags) Select

  if (button == eMouseLeft)
  {
    // left-click, so try using the current mouse cursor mode at this position
    Room.ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }


Then you need to check if this is a walk cursor mode (may vary depending on which cursor modes you use in your game), and add a separate case for walking where you clamp Y coordinate to the current player's position; or maybe using WalkStraight to ensure it walks precisely along the axis.
Code (ags) Select

  if (button == eMouseLeft)
  {
    // left-click, so try using the current mouse cursor mode at this position
    if (mouse.Mode == eModeWalkto)
    {
       // here we are essentially forcing Y coord to always stay the same
       // NOTE: (mouse.x + Game.Camera.X) converts from screen to room coordinates
       player.WalkStraight(mouse.x + Game.Camera.X, player.y, eNoBlock);
    }
    else
    {
       Room.ProcessClick(mouse.x, mouse.y, mouse.Mode );
    }
  }
Title: Re: How to limit character walking to left and right, no up and down?
Post by: skooperstooper on Fri 29/07/2022 16:19:39
Okay, thank you everyone! I'll take a stab at this and report back if things work or if I need more guidance!
Title: Re: How to limit character walking to left and right, no up and down?
Post by: skooperstooper on Fri 29/07/2022 19:31:08
With the default Sierra template I'm using, only the arrow keys work for keyboard movement. Not WASD. So my focus was on how arrows control the player. I'm not using a mouse since I'm learning and testing on my laptop, not on my desktop.

I found the code crimson mentioned in the keyboard movement script and copy pasted the replacement as provided. Movement behavior didn't change. I deleted that block of code entirely and ran the game again. Movement behavior still didn't change.

That made me think movement is being controlled by something else, so I searched for more code in the movement script that included walkstraight and found this on its own line:

Code (ags) Select
player.WalkStraight(player.x   move.x * distance, player.y   move.y * distance, eNoBlock);

When I deletedthat, the player wouldn't move anymore, so I guess I'll focus on that line to tweak movement. In the meantime, I tried creating a really thin walkable area like others suggested and that does limit movement.

But mentions of it maybe getting the character stuck makes me not really want to use that method. I also don't see how to set exact values for it since I'm drawing it. How would I "set" it to 1 pixel in height when there's nothing in properties except design and scaling? Or do I just zoom in as much as I can to draw it as thin as I can?

I'm abandoning this for now because it's already taken up two days of research and I want to get the interface and dialogue stuff figured out first, but I'll bookmark this so I can update it whenever I have my solution. Thanks again!
Title: Re: How to limit character walking to left and right, no up and down?
Post by: Crimson Wizard on Fri 29/07/2022 20:34:52
Quote from: skooperstooper on Fri 29/07/2022 19:31:08
I found the code crimson mentioned in the keyboard movement script and copy pasted the replacement as provided. Movement behavior didn't change. I deleted that block of code entirely and ran the game again. Movement behavior still didn't change.

That made me think movement is being controlled by something else, so I searched for more code in the movement script that included walkstraight and found this on its own line:

This module works in two modes: tapping and holding (set by KeyboardMovement.Mode). Each is implemented by a separate script block. That's true that I showed how to change only one of them; the second may be edited similarily, with some differences.