Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KiraHaraReturns on Thu 11/02/2016 11:19:53

Title: how to shorten the script
Post by: KiraHaraReturns on Thu 11/02/2016 11:19:53
hallo,
my charakter has several keys, everytime using any key on a certain door a text should be displayed, how to avoid repetitions like

if (player.ActiveInventory == inventory[2] || player.ActiveInventory == inventory[7],....) Display("say what");

what I'm looking for is a list of numbers appropriate to apply in the square brackets of inventory[]

I would be grateful for any clues
Title: Re: how to shorten the script
Post by: Snarky on Thu 11/02/2016 12:00:51
Several options, but the easiest is to write a helper function:

Code (ags) Select
bool isKey(this InventoryItem*)
{
  if(this == inventory[2] || this == inventory[7] || ...)
    return true;
  else
    return false;
}


You can put this in the global script or its own script module, importing it in the corresponding header file:

Code (ags) Select
import bool isKey(this InventoryItem*);

And now you can just write:

Code (ags) Select
  if(player.ActiveInventory.isKey())
    Display("say what");
Title: Re: how to shorten the script
Post by: Scavenger on Thu 11/02/2016 12:04:00
Alternatively, if you want to do it from the editor, make a new boolean Property for InventoryItems called "IsKey", and set it to true for each key in your game. Then, you can go:

Code (AGS) Select

if (player.ActiveInventory.GetProperty ("IsKey")) Display ("Say what");


Regardless of the amount of different keys your game has.
Title: Re: how to shorten the script
Post by: KiraHaraReturns on Thu 11/02/2016 12:19:58
thank you very much, it works pretty well