Global Variables

I want to keep a count of the number of items of a particular sort that my player has taken, so that when they have all 5 of these items which make a set, they will receive a message to that effect and the inventory will show they have the set. I guess the way to do this would be to have a variable GotSet which increases by one when one of the items is taken, with a check to see when it reaches 5. I've tried various ways of doing this using Variables and Attributes, but can't make it work. Can anyone help please?


You can store global variables as attributes of either the "player" or "game" object (or whatever object you like, but those two exist by default and seem sensible).

Perhaps if you show what doesn't work (if you've tried something like this), we can work out why.


There are a few ways to go about this. Personally, I would use a turn script to do what you want. If they are five specific items, I would forget about variables and do a simple check to see if the player has all five objects in their inventory. For example:

if (Got(First_Object) and Got(Second_Object) and Got(Third_Object) and Got(Fourth_Object) and Got(Fifth_Object)) {
  msg ("You have collected all five items in the set.")
}

If, however, they don't have to be specific items, just of a certain category, I would give all the collectable items a flag (Boolean attribute, true), and check to see if the player has a total of five. If you gave them a flag of "collectable," then this should work:

Set = 0
foreach (item, ScopeInventory()) {
  if (GetBoolean(item, "collectable")) {
    Set = Set + 1
  }
}
if (Set = 5) {
  msg ("You have the complete set.")
}

If it is possible to have six or more items, you might want to change Set=5 to Set>4 in the above script.


Thank you for the advice. I've added a turns attribute to the player, and entered the script, but the message doesn't appear when all 5 items have been collected


you can take a look at this guide in the meantime while waiting for us to help you more specically (we'll probably need to see your entire game code - it's the easiest way, much easier than trying to describe to us exactly what you've done or want to do):

http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk
http://textadventures.co.uk/forum/quest/topic/5387/i-really-need-help (this is a demo game step by step guide on basic Attribute usage)


You don't need to add any attribute to the player. The turn script goes to the game itself. Click game. Then select the Add menu. Under those options, select Turn Script. Name the script (optional) and paste code. Be sure to check the box to enable the script when the game starts. Then you'll be set.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums