Various questions about inventory scripts

Started by iamlowlikeyou, Sat 13/11/2010 14:33:42

Previous topic - Next topic

iamlowlikeyou

These are the things, I'd like to make:

*Left click on inventory items both in walk mode and interact mode --> interact with that item (i.e. selects it as active inventory).

*Right click on inventory items --> Look at item.

*Left click while mouse cursor = active inv, that performs no action (clicks on non-object, character and hotspot) --> deselect active inventory.

*Right click while mouse cursor = active inc --> deselect active inventory.


I'm not sure whether these functions require "handle inv clicks in scripts" or not...
Sorry for posting so many questions in one post, but I thought it was better than making 4 seperate posts after all.
Thanks for your help!

Khris

There are countless threads about this already, they can be hard to find though.

Quote*Left click on inventory items both in walk mode and interact mode --> interact with that item (i.e. selects it as active inventory).

*Right click on inventory items --> Look at item.

In General settings, set the option that inventory clicks are handled in script.

In Global.asc/on_mouse_click add behavior for inv clicks:

Code: ags
// at the very top
  InventoryItem*ia = inventory[game.inv_activated];

// this after eMouseLeft/Right
  else if (button == eMouseLeftInv) {
    if (mouse.Mode == eModeUseinv) ia.RunInteraction(eModeUseinv);
    else ia.RunInteraction(eModeInteract);
  }
  else if (button == eMouseRightInv) {
    if (mouse.Mode == eModeUseinv) {
      player.ActiveInventory = null;
      mouse.Mode = eModeWalk;
    }
    else ia.RunInteraction(eModeLook);
  }


Quote*Left click while mouse cursor = active inv, that performs no action (clicks on non-object, character and hotspot) --> deselect active inventory.

*Right click while mouse cursor = active inc --> deselect active inventory.

Inside the if (button == eMouseLeft) block and the eMouseRight one, add

Code: ags
  if (GetLocationType(mouse.x, mouse.y) == eLocationNothing) {
    player.ActiveInventory = null;
    mouse.Mode = eModeWalk;
  }

monkey0506

Actually this is essentially one complex question, so it's good that you put it all in one post. :=

You will need to set "Handle inventory clicks in script" to True.

Code: ags
// GlobalScript.asc
bool walkMode = false; // used to treat eModeWalkto as eModeInteract when over an inventory item

function repeatedly_execute() {
  // update walkMode
  InventoryItem *iat = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
  if (iat == null) {
    if (walkMode) {
      mouse.Mode = eModeWalkto;
      walkMode = false;
    }
    return;
  }
  if ((walkMode) && (mouse.Mode != eModeInteract)) {
    walkMode = false;
    return;
  }
  if (mouse.Mode == eModeWalkto) {
    mouse.Mode = eModeInteract;
    walkMode = true;
  }
}

function on_mouse_click(MouseButton button) {
  if (button == eMouseLeftInv) {
    InventoryItem *iat = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
    if (iat.IsInteractionAvailable(mouse.Mode)) iat.RunInteraction(mouse.Mode);
    else if (mouse.Mode == eModeInteract) player.ActiveInventory = iat;
  }
  else if (button == eMouseRightInv) {
    InventoryItem *iat = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
    iat.RunInteraction(eModeLookat);
  }
  else if (button == eMouseLeft) {
    if (player.ActiveInventory != null) {
      if (!IsInteractionAvailable(mouse.x, mouse.y, mouse.Mode)) player.ActiveInventory = null;
      else ProcessClick(mouse.x, mouse.y, mouse.Mode);
    }
    // else..put your normal left-mouse click stuff here, like ProcessClick
  }
  else if (button == eMouseRight) {
    if (player.ActiveInventory != null) player.ActiveInventory = null;
    // else..anything else you might want to do on a right mouse click
  }
}


Khris beat me but I ain't bovvered. :D

iamlowlikeyou

@Khris:
I know there's a lot of posts about these topics, but most of them imply the knowledge of some functions, that I'm familiar with yet, so I thought the best solution was to ask for the entire code.

Thanks both!  ;D
I'll maybe try and make a nice mixture of your codes.

iamlowlikeyou

I have now tried adding both the codes, but I can't get it to work...

When adding code supplied by monkey_05_06 it messed up some GUI elements for some reason...

Then I tried the one provided by Khris, but when I try to compile the game it says about the line "  InventoryItem*ia = inventory[game.inv_activated];" --> Error: cannot assign initial value value to global pointer.

Khris

The line is supposed to go at the top of on_mouse_click, not Global.asc, i.e. inside the function.

monkey0506

Well my code shouldn't be affecting GUIs at all..

I'd say just pick one person to listen to and follow them. :P Combining the code from two different programmers can be frustrating even for a seasoned coder.

iamlowlikeyou

@ monkey_05_06:
Yeah, I was mostly kidding about the mix ;)
I chose your code first because you have helped me with success in thread about GUI background animation.
If you say that it shouldn't affect the guis, I will try and check it through - maybe I made a flaw somewhere...

@Khris:
I misunderstood that - when I put the line in the right place it's solved of course.
However it seems that the inventory clicks do not function properly. This might very well be my mistake somewhere. I will try to detect the cause and return here again.

I think I will try to understand both your codes, and then decide which suits me best...

monkey0506

Well it shouldn't affect GUIs coz on_mouse_click isn't called when you click on any type of GUI or GUIControl (with the exception of InvWindows when you click on an actual item therein).

Khris is like me that he tries to help people out wherever he can, and he did indeed post in your other thread as well. He just kinda stepped back since it was my module so he figured I could probably handle it from there.

One thing I'd recommend you try and do is take these code snippets and basically rip them apart. That's how I learned how to program, is by taking tutorial code and breaking it down line by line, word by word. You just have to find a way that works for you to learn what each piece of the AGS scripting language does.

SMF spam blocked by CleanTalk