Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: cipberbloom on Fri 02/07/2021 00:42:30

Title: [SOLVED] Eliminating the Need to Cycle Through for Walk
Post by: cipberbloom on Fri 02/07/2021 00:42:30
Hey everyone  ;-D

I'm using the Sierra template but looking to eliminate the need to cycle through in order to walk. Referencing this thread:

https://www.adventuregamestudio.co.uk/forums/index.php?topic=57050.msg636603983#msg636603983

I did implement some of what was suggested with some (maybe totally wrong) alterations, replacing the original Sierra click-handling section in the global script with the following:

Code (ags) Select

// called when a mouse button is clicked
function on_mouse_click(MouseButton button)
{
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused())
  {
    // game is paused, so do nothing (i.e. don't process mouse clicks)
  }
  else if (button == eMouseLeft)
  {
       if (GetWalkableAreaAt(mouse.x, mouse.y) != 0)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
       }
       if (IsInteractionAvailable(mouse.x, mouse.y) != 0 && (eModeLookat = true)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeLookat);
       }
       if (IsInteractionAvailable(mouse.x, mouse.y) != 0 && (eModeInteract = true)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeInteract);
       }
       if (IsInteractionAvailable(mouse.x, mouse.y) != 0 && (eModeTalkto = true)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeTalkto);
       }
  }
  else if (button == eMouseRight || button == eMouseWheelSouth){
    // right-click or mouse wheel down will cycle the mouse cursor mode forwards
    mouse.SelectNextMode();
  }
  else if (button == eMouseMiddle)
  {
    // middle-click makes the character walk to clicked area, regardless of cursor mode
    Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if (button == eMouseWheelNorth)
  {
    // mouse wheel up will cycle the cursor mode backwards
    mouse.SelectPreviousMode();
  }
}


This produces an 'End of input reached in middle of expression' error. I've checked the curly brackets (from what's been said in the forums this error is commonly the result of missing brackets), but they all seem right.

Any suggestions are most welcome. Thanks in advance!

Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: eri0o on Fri 02/07/2021 01:14:57
It's this line

Code (ags) Select
if (IsInteractionAvailable(mouse.x, mouse.y) != 0 && (eModeTalkto = true)
Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: Crimson Wizard on Fri 02/07/2021 01:35:12
The correct form is:
Code (ags) Select

if (IsInteractionAvailable(mouse.x, mouse.y, eModeLookat))


But you should replace "if"s with "else if", otherwise you may end up having multiple interaction one by one. And probably put "Walkable area" check last, or objects standing on a walkable area won't be interacted with.

So, like:
Code (ags) Select

       if (IsInteractionAvailable(mouse.x, mouse.y, eModeLookat)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeLookat);
       }
       else if (IsInteractionAvailable(mouse.x, mouse.y, eModeInteract)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeInteract);
       }
       else if (IsInteractionAvailable(mouse.x, mouse.y, eModeTalkto)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeTalkto);
       }
       else if (GetWalkableAreaAt(mouse.x, mouse.y) != 0)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
       }
Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: cipberbloom on Fri 02/07/2021 10:00:04
Quote from: Crimson Wizard on Fri 02/07/2021 01:35:12
The correct form is:
Code (ags) Select

if (IsInteractionAvailable(mouse.x, mouse.y, eModeLookat))


But you should replace "if"s with "else if", otherwise you may end up having multiple interaction one by one. And probably put "Walkable area" check last, or objects standing on a walkable area won't be interacted with.


Thank you very, very much, for the correct format, the 'else if' tip, and the bit about putting the walkable area last!

Code (ags) Select

// called when a mouse button is clicked
function on_mouse_click(MouseButton button)
{
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused())
  {
    // game is paused, so do nothing (i.e. don't process mouse clicks)
  }
  else if (button == eMouseLeft)
  {
       if (IsInteractionAvailable(mouse.x, mouse.y, eModeLookat))
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeLookat);
       }
       else if (IsInteractionAvailable(mouse.x, mouse.y, eModeInteract))
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeInteract);
       }
       else if (IsInteractionAvailable(mouse.x, mouse.y, eModeTalkto))
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeTalkto);
       }
       else if (GetWalkableAreaAt(mouse.x, mouse.y) != 0)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
       }
  }
  else if (button == eMouseRight || button == eMouseWheelSouth){
    // right-click or mouse wheel down will cycle the mouse cursor mode forwards
    mouse.SelectNextMode();
  }
  else if (button == eMouseMiddle)
  {
    // middle-click makes the character walk to clicked area, regardless of cursor mode
    Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if (button == eMouseWheelNorth)
  {
    // mouse wheel up will cycle the cursor mode backwards
    mouse.SelectPreviousMode();
  }
}


The above is working wonderfully, I can walk round and look at stuff, touch/pick things up, and talk to them, but not if the items have multiple actions. Here's my resulting (sort of) pseudocode of what's going on:

if the game is paused, don't do anything
otherwise, when the left button is clicked
check to see if you are on anything you can interact with
if it is something you can look at, look at it
otherwise, interact with it
<-- it's the otherwise, there, it needs something additional. And even though you were totally right about 'else if' preventing the unwanted multiple, one by one interactions, I tested it without the 'elses' just because I'm weird that way!  (laugh) And you know, it's fun to see what happens.

So then I tried combining all of the interactions into a handful of lines, crossing my fingers that AGS wouldn't care that I was missing out a parameter (it did, of course), but then even if had worked it I'm guessing it would've just done them all, just as though I'd taken out all of the 'else ifs' or done them all at once or something:

Code (ags) Select

  else if (button == eMouseLeft)
  {
       if (IsInteractionAvailable(mouse.x, mouse.y, eModeLookat || eModeInteract || eModeTalkto))
       {
             Room.ProcessClick(mouse.x, mouse.y);
       }
       else if (GetWalkableAreaAt(mouse.x, mouse.y) != 0)
       {
             Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
       }
  }


I really appreciate the way help is provided on the forum, like a really good hint system rather than a walkthrough. I could copy and paste all day but then I wouldn't learn anything, wouldn't know why things worked, which would drive me crazy. Anyway, thanks again! It's pretty late here... I'm going to unwind with someone else's game and prod at the inner-workings of my own again tomorrow!  :-D

All the best to you!
Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: eri0o on Fri 02/07/2021 12:21:01
This last line

Code (ags) Select
else if (GetWalkableAreaAt(mouse.x, mouse.y) != 0)

If you are not planning to add any additional clauses below, I think you can remove the if clause, and the resulting behavior would be, if you click a wall that doesn't have any interactions, the character would walk in direction of that wall. With the if clause, it would instead do nothing.
Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: Cassiebsg on Fri 02/07/2021 17:06:25
Yes, like eri0o said.

Think of it like this:

Code (ags) Select

if // You always need this one in a IF condition.  ;)

else if // You only need this if there are other conditions that can happen and you want a specific thing to happen. You can have as many as needed.

else // You only need this one if you want a default action when all of the above are false (in this case, you want the character to walk there).


Hope it helps.  :)
Title: Re: Eliminating the Need to Cycle Through for Walk
Post by: cipberbloom on Wed 07/07/2021 02:25:34
A thousand thanks, Crimson Wizard, eri0o, and CassieBSG (the if, else if, else translation was invaluable! You're helping me crush the wall in my brain between human languages and programming!  :-D)!!!

It works!
Code (ags) Select

// called when a mouse button is clicked
function on_mouse_click(MouseButton button)
{
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused())
  {
    // game is paused, so do nothing (i.e. don't process mouse clicks)
  }
 
  // START: THE 'WALK ANYWHERE EVEN IF WALK IS NOT THE ACTIVE MODE'
  else if ((GetWalkableAreaAtScreen(mouse.x, mouse.y) != 0 || (GetLocationType(mouse.x, mouse.y) == eLocationNothing)) && (button == eMouseLeft))
  {
    Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  // STOP: THE 'WALK ANYWHERE EVEN IF WALK IS NOT THE ACTIVE MODE'
 
  else if (button == eMouseLeft)
  {
    // left-click, so try using the current mouse cursor mode at this position
    Room.ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }
  else if (button == eMouseRight || button == eMouseWheelSouth){
    // right-click or mouse wheel down will cycle the mouse cursor mode forwards
    mouse.SelectNextMode();
  }
  else if (button == eMouseMiddle)
  {
    // middle-click makes the character walk to clicked area, regardless of cursor mode
    Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if (button == eMouseWheelNorth)
  {
    // mouse wheel up will cycle the cursor mode backwards
    mouse.SelectPreviousMode();
  }
}


All the best, everyone.  ;-D