Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: eminigalaxy on Thu 20/06/2024 17:55:24

Title: Adding any word someone types into the parser into inventory.
Post by: eminigalaxy on Thu 20/06/2024 17:55:24
I need help trying to figure out how to code a part in my game where the player can type the name of any ingredient into the parser and it will be added to the inventory (the inventory is just a simple list of words). I've tried two different ways but have gotten two errors (cannot convert 'Const string' to 'inventory item*' & cannot convert 'bool' to 'string*') with it and can't figure out any other ways. Or if it can even be done.
Title: Re: Adding any word someone types into the parser into inventory.
Post by: Crimson Wizard on Thu 20/06/2024 18:23:02
Please post your scripts that did not work, it may be easier to tell how to fix the script.

Also, you say that "the inventory is just a simple list of words", but then mention that something cannot be converted to  'inventory item*'. Are you trying to also use Inventory Items somewhere in this?
Title: Re: Adding any word someone types into the parser into inventory.
Post by: Khris on Fri 21/06/2024 12:00:18
You cannot use built-in inventory items for this. You need an array of strings instead:

String inv_item[100]; // array
int inv_count;  // count size

// add new string to array
function AddItem(String name) {
  if (inv_count == 100) { Display("Inv item limit reached!"); return; }
  inv_item[inv_count] = name;
  inv_count++;
}
Title: Re: Adding any word someone types into the parser into inventory.
Post by: Snarky on Fri 21/06/2024 13:53:44
You say "add any word someone types," but do you mean any word, or just any word that is actually the name of an inventory item in the game?

If what you want is the first option, that's what Khris's code will let you do. If it's the second, you can do:

Code (ags) Select
function AddItem(String name)
{
  for(int i=0; i<Game.InventoryItemCount;i++)
  {
    if(inventory[i].Name == name)
    {
      player.AddInventory(inventory[i]);
      break;
    }
  }
}