how to shorten the script

Started by KiraHaraReturns, Thu 11/02/2016 11:19:53

Previous topic - Next topic

KiraHaraReturns

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

Snarky

#1
Several options, but the easiest is to write a helper function:

Code: ags
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
import bool isKey(this InventoryItem*);


And now you can just write:

Code: ags
  if(player.ActiveInventory.isKey())
    Display("say what");

Scavenger

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

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


Regardless of the amount of different keys your game has.

KiraHaraReturns

#3
thank you very much, it works pretty well

SMF spam blocked by CleanTalk