Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Fanomatic on Sun 03/03/2019 17:54:58

Title: SOLVED: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Sun 03/03/2019 17:54:58
Hello there dear forum users, I have a problem related to regions that I hope you can help me with. I've tried for days, but I can't seem to figure this one out. I'm using the 9-verb add-on and this could potentially be related to how that is set up, but I'm a bit new to this so if somebody have any ideas I'd be glad to hear them.

The game I'm working on features three playable characters that you can switch between similar to how Day of the tentacle works. The first room of the game is a jail where the room is divided into different sections of which some can only be reached by certain characters, so each player character is somewhat restricted in where they can go. The region setup looks a bit like this:

(https://i.imgur.com/ZwVNNdr.jpg)

Since I don't want the player characters to be able to give items to other characters that are not in the same ”section” I've divided the room up into regions and walkable areas and are doing a check every time I'm giving something from one player character to another player character to determine if they should be allowed to do that.

The way I've set this up currently is that every time the player clicks inside a region (inside the play area) the region is saved to a global variable (clickedRegion) like this:

Code (ags) Select
function on_mouse_click(MouseButton button)
{
 
  //If the mouse cursor is above the verb-GUI
  if (mouse.y <= 153)
  {
    clickedRegion = Region.GetAtRoomXY(mouse.x + GetViewportX(), mouse.y + GetViewportY());
    lblDebug3.Text = String.Format("Clicked region: %d", clickedRegion.ID);
  }
 
}


Then I have a ”check area” function that compares the region the player just clicked to the region the active player is in:

Code (ags) Select
// Check if what the player clicks are in the same region as the player.
function checkArea()
{
  Region*playerRegion = Region.GetAtRoomXY(player.x, player.y); //get current player region
 
    //If the player clicks the same region the player is in it can always interact.
    if (clickedRegion.ID == playerRegion.ID)
    {
      canWalkToObject = true;
    }
   
    //If player clicks on region 1
    if (clickedRegion == region[1])
    {
      if (playerRegion == region[2])
        {
            sayCantReach();
            canWalkToObject = false;
        }
      else if (playerRegion == region[3])
        {
          if (cellDoorIsOpen == false)
          {
            sayCantReach();
            canWalkToObject = false;
          }
          else
          {
            canWalkToObject = true;
          }
        }
      else if (playerRegion == region[4])
        {
          sayCantReach();
          canWalkToObject = false;
        }
     
    }
    //If player clicks on region 2
    else if (clickedRegion == region[2])
    {
      if (playerRegion == region[1])
        {
            sayCantReach();
            canWalkToObject = false;
        }
      else if (playerRegion == region[3])
        {
          if (cellDoor2IsOpen == false)
            {
              sayCantReach();
              canWalkToObject = false;
            }
          else
            {
              canWalkToObject = true;
            }
        }
      else if (playerRegion == region[4])
      {
        sayCantReach();
        canWalkToObject = false;
      }
    }
   
    //If player clicks on region 3
    else if (clickedRegion == region[3])
    {
      if (playerRegion == region[1])
        {
          if (cellDoorIsOpen == false)
          {
            sayCantReach();
            canWalkToObject = false;
          }
          else
          {
            canWalkToObject = true;
          }
        }
      else if (playerRegion == region[2])
        {
          if (cellDoor2IsOpen == false)
          {
            sayCantReach();
            canWalkToObject = false;
          }
          else
          {
            canWalkToObject = true;
          }
        }
      else if (playerRegion == region[4])
      {
        sayCantReach();
        canWalkToObject = false;
      }
    }
    //If player clicks on region 4
    else if (clickedRegion == region[4])
    {
      if (playerRegion == region[1])
        {
        sayCantReach();
        canWalkToObject = false;
        }
      else if (playerRegion == region[2])
        {
        sayCantReach();
        canWalkToObject = false;
        }
      else if (playerRegion == region[3])
      {
        sayCantReach();
        canWalkToObject = false;
      }     

    }
  }


And lastly, this is an example of code from the "giveto" part of an item. Basically it sees if the player can reach the region and then either gives the item or says that it can't because the character is too far away:

Code (ags) Select
  // GIVE TO (characters only)
  else if(UsedAction(eGA_GiveTo))
  {
      if (player.ActiveInventory == iChili)
      {
        checkArea();
        if (canWalkToObject == true)
        {
            if (player == cFive)
            {
              cFuzzy.Say("Oh, Fuzzy like!");
              cFive.LoseInventory(iChili);
              cFuzzy.AddInventory(iChili);
            }
            else
            {
             
            }
        }
        else
        {
          sayCantReachPlayer();
        }
      }
  }


The mouse region variable normally works and registers correctly, but when I press the ”give” verb and try to hand an inventory item to a character it doesn't for some reason. It's still set to the region I clicked before I clicked the ”give” verb, so the check doesn't work...

Here's some images that explains the scenario:

(https://i.imgur.com/SQ6k7iP.jpg)

I guess this has something to do with how 9-verbs works. Maybe I need to change something in the global "give" function? Unfortunately I don't know where to look.  ???
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Cassiebsg on Sun 03/03/2019 21:05:59
Dejavue  ???  (roll)

https://www.adventuregamestudio.co.uk/forums/index.php?topic=56874.0
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Mon 04/03/2019 19:25:08
QuoteDejavue  ???  (roll)

Yes, this is indeed a followup to the previous issue that Khris and Crimson Wizard helped me fix. I should have included the link to the previous post in this post. Thanks for that Cassiebsg. :)

However, this isn't technically the same issue, so I thought I should post it as a new topic. Also I'm not really sure if this belongs in the "Beginners technical questions" or "Advanced technical forum". If I'm doing this wrong, I apologize.

I know this issue is a headache and I'm trying to solve it on my own, but if someone can help I would appreciate it greatly. I'll include all who help in the game credits of course :)

I'll see if I can dig deeper in the 9-verb global code and see if I can pinpoint what code is causing the problem.
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Snarky on Mon 04/03/2019 19:38:18
It's hard to tell just from the fragments you're showing here, but in cases like this the problem is often that things aren't happening in the order you expect. For example, maybe clickedRegion only gets set after the function with the else if(UsedAction(eGA_GiveTo)) block. I would probably place some Display() calls in that function and in on_mouse_click() to see what actually happens when I click.

Otherwise, this is a typical case for debugging. When you run the game through the IDE with F5, you can place a breakpoint in the code by clicking in the margin (you can do this while the game is running): when it hits that point, it will pause and allow you to step through the code line-by-line to trace what's happening.
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Mon 04/03/2019 20:37:44
Thanks! Okay, I placed some display() calls in the code.

What seems to happen is that the else if(UsedAction(eGA_GiveTo)) block is triggered 'before' the onMouseClick, so the check is done before clickedRegion is set.

Does this mean I have to find the eGA_GiveTo function and somehow set clickedRegion inside the function to make sure it gets set before checkArea()?

I found this function inside guiScript.asc, but I'm unsure if it's the relevant one.

Code (ags) Select
      // Giveto
      else if ((GSagsusedmode == eModeUseinv) && GSloctype==eLocationCharacter && isAction(eGA_GiveTo))
      {
        ActionLine.TextColor=ActionLabelColorHighlighted;
        ItemGiven=player.ActiveInventory;
       
        if (approachCharInteract == false)
        {
          if (IsInteractionAvailable (mrx - GetViewportX (), mry - GetViewportY (), eModeUseinv) == 1)
          {
            character[GSlocid].RunInteraction(eModeUseinv);
          }
        }
        else {
          if (GoToCharacter(character[GSlocid], 0, NPC_facing_player, 2))
          {
            if (IsInteractionAvailable (mrx - GetViewportX (), mry - GetViewportY (), eModeUseinv) == 1) {
              character[GSlocid].RunInteraction(eModeUseinv);       
            }
          }
        }
        SetAction (eMA_Default);
      }     
      else {
        UpdateActionBar();
        ActionLine.TextColor=ActionLabelColorHighlighted;
        #ifnver 3.4
        ProcessClick(x, y, GSagsusedmode);
        #endif
        #ifver 3.4
        Room.ProcessClick(x, y, GSagsusedmode);
        #endif
        SetAction(eMA_Default);
        ItemGiven=null;
      }
    }
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Khris on Mon 04/03/2019 22:51:29
AGS calls all on_mouse_click functions in order, from top to bottom. The GlobalScript is last.
Just copy the line that sets clickedRegion to the start of your checkArea function.
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Tue 05/03/2019 22:47:20
QuoteAGS calls all on_mouse_click functions in order, from top to bottom. The GlobalScript is last.
Just copy the line that sets clickedRegion to the start of your checkArea function.

Thanks, but the problem is that if I get clickedRegion from checkArea I will get the region the mouse cursor is currently overlapping when the function runs.

However, since the PC automatically walks towards the character it's gonna give the item to, I can move the mouse cursor to another region while the player is walking and checkArea gets that region instead as it triggers when the PC reaches the character (or when it's as close as it can possibly get). So if I happen to hover the mouse cursor over the same region my active PC is standing in, it reads it as both characters being in the same area and allows the "give to" command.
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Khris on Tue 05/03/2019 23:27:56
One thing I'm realizing right now is that getting the region at the mouse coordinates doesn't make sense anyway when you're interacting with a character because you need to check the region the character is standing in, right?
Not sure why you're getting region 1 though; I'd expect 0 (no region).
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Wed 06/03/2019 22:11:27
Hmmm, yes, you're absolutely right. It's probably better to just get the region of the interacted PC directly rather than trying to get the (arbitary) mouse position.

Thanks! I'll write a new function for that tomorrow.  ;-D
Title: Re: Problem getting mouse region (9-verb add-on)
Post by: Fanomatic on Thu 07/03/2019 22:36:00
Okay, I set it up so that I check for the character region on 'give to' instead of the mouse cursor region and now it works! Thanks for the help!