Handle Inventoy clics in scripts

Started by Fedx, Sun 05/04/2009 03:09:36

Previous topic - Next topic

Fedx

So, this is the thing. I have this piece of code in the on_mouse_click function (With a Lucas-Arts style gui, Label6 is the label on the status line):

Code: ags

function on_mouse_click(MouseButton button) {
   // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }
  else if (button == eMouseMiddle) { 
    // Middle-button-click, default make character walk to clicked area (a little shortcut)
    // Could have been just "player.Walk(mouse.x,mouse.y)", but it's best to
    // leave our options open - what if you have a special script triggered
    // on "walking" mode?
    ProcessClick(mouse.x, mouse.y, eModeWalkto); 
  }
    if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModeLookat) == 0){
    mouse.Mode = eModeWalkto;
    Label6.Text = ("Walk to @OVERHOTSPOT");
  }
    else if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModeInteract) == 0){
    mouse.Mode = eModeWalkto;
    Label6.Text = ("Walk to @OVERHOTSPOT");
  }
    else if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModeUseinv) == 0){
    mouse.Mode = eModeWalkto;
     Label6.Text = ("Walk to @OVERHOTSPOT");
  }
    else if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModePickup) == 0){
    mouse.Mode = eModeWalkto;
     Label6.Text = ("Walk to @OVERHOTSPOT");
  }
    else if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModeTalkto) == 0){
    mouse.Mode = eModeWalkto;
     Label6.Text = ("Walk to @OVERHOTSPOT");
  }
  else if (button == eMouseLeftInv && mouse.Mode == eModeInteract){
  player.ActiveInventory = inventory[game.inv_activated];
  Label6.Text = String.Format("Use %s with @OVERHOTSPOT@", player.ActiveInventory.Name);
  }
}


Till now, everything was just fine, until I tried to make the character look at one inventory item. What shall I do? I tryed to do it by myself but I couldn't.
- The gamer doesn't dies... he respawns -

OneDollar

Apologies for the lengthy answer, I'm trying to work through your code and see what's actually happening...

I'm a little confused by what the code is supposed to be doing. What you have is one if function with conditionals for the game being paused, the left mouse being clicked and the middle mouse being clicked.

Then you have a completely separate if function dealing with combinations of left mouse button and available interactions.

So if the player clicks the left mouse button on something, it will call ProcessClick on that, then if there is a missing interaction for any mode on that object it sets the cursor to eModeWalkto and sets the status label to "Walk to @OVERHOTSPOT@", but if all interactions are available and the mouse mode is currently eModeInteract and the player left clicked on the inventory it makes the clicked item the active inventory item.

I think you've missed an "else" off the front of "if (button == eMouseLeft && IsInteractionAvailable(mouse.x,mouse.y, eModeLookat) == 0){", and if that's the case you need to stick those conditions higher than the "else if (button == eMouseLeft)" otherwise they'll never be called.

I'm not really following the logic here, but (assuming you've got HandleInventoryClicks in script set to true) the reason you're not getting a lookat interaction for clicking on an inventory item is because no conditional matches it.

If you set the mouse mode to eModeLookat and left click an inventory item it calls the on_mouse_click function setting the button value to eMouseLeftInv. Your only condition for when button == eMouseLeftInv is when you also have mouse.Mode == eModeInteract, but its not because we set it to eModeLookat. So nothing is called.

Personally I'd look at doing a more top down method of coding, i.e. first figure out what mouse button is being clicked, then figure out what the mouse mode is, then figure out if there's an interaction available. Something like this, maybe...
Code: ags

function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }else if (button == eMouseLeft) {
    //Player left clicks on something that is not the inventory
    if (IsInteractionAvailable(mouse.x, mouse.y, mouse.Mode) == 0) {
      //No interaction available for the current mouse mode, so cancel the interaction
      //and reset cursor to walk to.
      mouse.Mode = eModeWalkto;
      Lable6.Text = ("Walk to @OVERHOTSPOT@");
    }else{
      //There is an interaction available, so do it!
      ProcessClick(mouse.x, mouse.y, mouse.Mode );
    }
  }else if (button == eMouseMiddle) { 
    // Middle-button-click, default make character walk to clicked area (a little shortcut)
    // Could have been just "player.Walk(mouse.x,mouse.y)", but it's best to
    // leave our options open - what if you have a special script triggered
    // on "walking" mode?
    ProcessClick(mouse.x, mouse.y, eModeWalkto); 
  }else if (button == eMouseLeftInv) {
    //Left clicked on an inventory item
    if (mouse.Mode == eModeInteract) {
      //Interact with inventory item
      player.ActiveInventory = inventory[game.inv_activated];
      Label6.Text = String.Format("Use %s with @OVERHOTSPOT@", player.ActiveInventory.Name);
    }else{
      //Other click on inventory item (including look at)
      inventory[game.inv_activated].RunInteraction(mouse.Mode);
    }
  }
}

Pumaman

Well, you have this existing code to handle selecting an inventory item:

Code: ags

  else if (button == eMouseLeftInv && mouse.Mode == eModeInteract){
  player.ActiveInventory = inventory[game.inv_activated];
  Label6.Text = String.Format("Use %s with @OVERHOTSPOT@", player.ActiveInventory.Name);
  }


so to look at it, just add something like:

Code: ags

  else if (button == eMouseLeftInv && mouse.Mode == eModeLookat){
    inventory[game.inv_activated].RunInteraction(eModeLookat);
  }


Fedx

Thanks both!! It worked out correctly!! And saved a lot of space :D
- The gamer doesn't dies... he respawns -

SMF spam blocked by CleanTalk