Should I use booleans over Has Inventory

Started by barefoot, Mon 17/01/2011 16:09:36

Previous topic - Next topic

barefoot

Hi

I am trying to determine the best way of doing the following:

Player gets various inventory items from various places within the game and in no certain order.

You can go to see a fence at any time who will offer you money for the items you have, whatever they may be. Naturally you lose the inventory items and gain the cash..

You do this until you have all the money you need.

Obviously this can be too much for HasInventory I thinks..

Items include:

credit card
jewellery
designer clothes
TV
CD Player
Antiques


----------------------------------------------------------------

I'm wondering if Booleans are a way to go rather than HasInventory etc etc

Could someone assist me in going about it the right way and possible give an example.

Cheers guys

barefoot





I May Not Be Perfect but I Have A Big Heart ..

Khris

Determining the best way is done by asking yourself: does not using a boolean result in a bug?

Except when the player can take e.g. a pebble off a beach again and again, most inventory items are discreet and can only be picked up exactly once at their original location.
Thus the event that adds the item to the player's inventory must perform a check that determines without failure whether the item has already been picked up before.

If the player is never going to lose the item in the game, ever, HasInventory is a sufficient check. I'd still discourage that method though, because I think it's best to write code as modular as possible, i.e. a small change in one part of the game should never make it necessary to rewrite other parts.

In the situation you describe, using HasInventory is obviously not going to work; as soon as the player sells the item, it can be picked up again; rinse, repeat.

For simple "the first time, do this, afterwards, do something else" interactions, there's Game.DoOnceOnly:

Code: ags
function hDesk_Interact() {

  if (Game.DoOnceOnly("search desk in hotel room")) {
    Display("You search the desk for anything valuable.");
    Display("In one of the drawers on the back you find some expensive jewelry.");
    player.AddInventory(iJewelry);
  }
  else Display("You have already search the desk.");
}

barefoot

Thanks for a reply Khris...

Once an object is added to your inventory the object disappears and it can NEVER be picked up again.. Ever... hence the IF, ELSE statement etc etc.

You keep it in your inventory until you sell it to the fence in exchange for money, again it is gone FOREVER from your Inv once the fence buys it from you..

What I need is for the fence (NPC) to tell you what you have in your inventory and give you some money... simples..

IE (After script check for what you have in you inv)

Code: ags

cfence.Say("You have a credit card, TV, CD player. Ok, I'll give you $300");


Something along those lines.

As i said earlier, what items you have when you meet the fence depends where you've been..

You can visit the fence anytime during the game..

UPDATE.. I suppose an alternative way would be to use inv on fence with the activeInventory and have it go on from there.. Of course you would need to use every inv you have one at a time...as an alternative way

cheers
barefoot


I May Not Be Perfect but I Have A Big Heart ..

Khris

#3
I think I misunderstood you; if the inventory items in questions are acquired by picking up objects which disappear afterwards, you won't need any check at all for that.

The core of the problem is composing the message and calculating the offer, right?

Here's what I'd do:

Code: ags
String message, last_item;
int sum;

function AddToMessage(String item, int value) {
  if (String.IsNullOrEmpty(message)) {
    if (!String.IsNullOrEmpty(last_item)) message = last_item;
  }
  else message = String.Format("%s, %s", message, last_item);
  last_item = item;
  sum += value;
}

String FinalMessage() {

  if (message == "") {
    if (last_item == "") {
      return ""; 
    }
    else {
      return String.Format("You have %s. Ok, I'll give you $%d.", last_item, sum);
    }
  }
  else return = String.Format("You have %s and %s. Ok, I'll give you $%d.", message, last_item, sum);
}

function cFence_Talk() {
  
  message = "";
  last_item = "";
  
  if (player.HasInventory(iCreditCard)) AddToMessage("a credit card", 150);
  if (player.HasInventory(iTV)) AddToMessage("a TV", 50);
  if (player.HasInventory(iCDPlayer)) AddToMessage("a CD player", 30);
  
  String offer = FinalMessage();

  if (String.IsNullOrEmpty(offer)) {
    cFence.Say("You don't have anything I'm interested in.");
  }
  else {
    cFence.Say(offer);
    dFenceDialog1.Start();
  }
}


This auto-generates the fence's offer message, using commas and "and", if appropriate. The next thing to do is to use a dialog to accept or deny that offer, I assume.

barefoot

Cheers Khris.. I'll give that a bash..    :=

barefoot
I May Not Be Perfect but I Have A Big Heart ..

barefoot

#5
Hi Khris

I'm a  little stuck with your code.. I do get the gist of it... have you got an easier way of explaining it?

I do have an alternative way, a bit long winded though.

cheers

barefoot
I May Not Be Perfect but I Have A Big Heart ..

Khris

I made it easier to handle; just look at the final parts of the cFence_Talk function.

FinalMessage() composes the fence's offer or returns "" if you don't have anything he's interested in.

SMF spam blocked by CleanTalk