[SOLVED] Restore objects' visibility when inventory items deselected

Started by Laura Hunt, Wed 29/05/2019 14:27:44

Previous topic - Next topic

Laura Hunt

Not sure if this would qualify as a "beginner question", given that other issues I've had turned out to be in fact quite advanced, but let's give this a try!

Rather than having an inventory for the game I'm working on, I would like to implement a "pick up object - use it on something else" system.

For example, say there's an empty glass on a table. If the player clicks on the object, the character will walk up to it (still not scripted in the example below) and the cursor will turn into the sprite of the glass while at the same time the object/sprite on the table is disabled:

Code: ags
function oGlass_Interact()
{
  player.AddInventory(iGlass);
  player.ActiveInventory = iGlass;
  oGlass.Visible = false;
  mouse.Mode = eModeUseinv;
}



Now with the glass as active inventory item / cursor, the player can use it on a hotspot on the wall and overhear a conversation.

So far, so good. Everything works as it should, except that when I hit right-click in order to deselect/release the active inventory item, I would like for the object to reappear in its original position.

The only way I have come up to do this would be to do a check for every single inventory item in the game to see which one the player is carrying and make the corresponding object visible. This code does the job nicely:

Code: ags
if (mouse.Mode == eModeUseinv && button == eMouseRight) {
  if (player.ActiveInventory == iGlass) object[0].Visible = true;
  // etc etc etc for every single inventory item!!
  player.ActiveInventory = null;
  }


However, this only seems like a good idea because my game will have a very small number of items, but it doesn't seem like the most elegant or resource-optimized way to do this. Any suggestions for alternatives?

Khris

You can add a custom int property to the InventoryItem, default value -1:

Code: ags
  if (mouse.Mode == eModeUseinv && button == eMouseRight) {
    int objIndex = player.ActiveInventory.getProperty("objindex");
    if (objIndex > -1) {
      object[objIndex].Visible = true;
      player.ActiveInventory = null;
    }
  }


Two issues remaining:
1) the player should ideally approach the table before putting back the glass
2) what if they leave the room with the glass in hand?

In general there are two ways to tie data objects together; either a simple array:  objIndex[iGlass.ID] = 0;
or a struct where one could store additional info such as the room number, coordinates, etc.

Laura Hunt

Quote from: Khris on Wed 29/05/2019 14:34:31
You can add a custom int property to the InventoryItem, default value -1:

Code: ags
  if (mouse.Mode == eModeUseinv && button == eMouseRight) {
    int objIndex = player.ActiveInventory.getProperty("objindex");
    if (objIndex > -1) {
      object[objIndex].Visible = true;
      player.ActiveInventory = null;
    }
  }


Hi Khris, and thanks once more for your help! I did think about creating an array, but this would mean that I would have to be extremely careful to create each object and its equivalent inventory item with the same index number, right? So oGlass and iGlass would both have ID 0, oLamp and iLamp would both need to have ID 1, etc etc?

In any case, the world of custom properties baffles me to no end. So I create a custom property called "objindex" (which is different from the integer objIndex?) with default value -1, ok. But how does that -1 take different values depending on what inventory item you're holding?  :confused:

Quote from: Khris
Two issues remaining:
1) the player should ideally approach the table before putting back the glass
2) what if they leave the room with the glass in hand?

1) The player will approach the table to pick up the glass and not move from there while eModeUseinv is active, so this should not be an issue. The character will only move away from the table if the glass is used on the correct hotspot, otherwise I'll just display a "hm, that doesn't work" message or something without having to make the character walk all the way to the wrong hotspot. I know it feels a bit less realistic, but I'd like to cut down on unnecessary walking, which is an aspect that always annoys me about point and click adventures. That, and two-click "look/use" interfaces. But I digress :-D

2) They can't! All the exits are doors and they can't click on doors/hotspots while the cursor is in eModeUseinv, so that's not an issue either. I think!

Laura Hunt

omg nevermind, something like a giant gear just went CLICK inside my head and I get it now!

Thanks so much, Khris. Not just for the code but for helping me understand *how* to do stuff!

Khris

You're welcome, but just in case somebody else stumbles over this thread:

The object index is stored in the custom property of the inventory item, which one can set in the editor.
If one uses an array instead, the whole point of the array is to assign an arbitrary objectID to each inventory item; if both had the same ID one wouldn't need an array in the first place :)

SMF spam blocked by CleanTalk