cannot convert 'String*' to 'InventoryItem*'

Started by Rocco, Sat 24/10/2009 23:15:39

Previous topic - Next topic

Rocco

I have no idea how could possibly convert a String containing the script name of an InventoryItem into the actual script object.

I have this function;

Code: ags


  function Func_Roc_Searching(String loot)
 {
   if(String.IsNullOrEmpty(loot))
   { }
   else  
   {
     int value;
     value = loot.AsInt;
       if(value)
       {
         GiveMoney(value);
       } 
       else
       player.AddInventory(loot);  // <- here is the problem
      
     PlaySound(4);  
   
   } 

....
}

Lufia

To the best of my knowledge, that's not possible.  :-\

You can access the Name and ID properties of inventory items, but not the scripting name. Maybe you could make a loop check over the inventory array until you find the name (not the scripting one) of your item, but that doesn't seem practical.

Why not use two arguments for your function instead?

Code: ags
function Func_Roc_Searching(bool type, int number) {
  if (type) { // Let's say that's for money
    GiveMoney(number);
  } 
  else {
    player.AddInventory(inventory[number]);  // And here is for items
    PlaySound(4);
  } 
...
}


I've no idea if that's applicable to what you're doing or not. (And I've no idea if there's a better way of doing this or not. :P)

Rocco

Yes thx this is a workaround that works fine for me.  :)

Code: ags

  function Func_Roc_Searching(int money, int inv)
 {
   if(inv)
   player.AddInventory(inventory[inv]);
   
   if(money)
   GiveMoney(money);
   
   PlaySound(4);
}

RickJ

Quote
Maybe you could make a loop check over the inventory array until you find the name (not the scripting one) of your item, but that doesn't seem practical.
Well it would look something like the following code.  It's untested but should give an idea how to go about searching for the name.  Also, note it's possible to know how many of each inventory item a character has.  So the inventory can be used to keep track of much money the player has instead doing it as a special case.  Also in

Code: ags

*** Script header ***
import function GiveInv(this Character*, String name, int amount);

*** Global Script ***
// Extender function - Gives quantity of inventory to character
function GiveInv(this Character*, String name, int amount)  {
    int i;
    bool found;

   // Find the named inventory item
   i = 0;
   found = false;
   while ((i<Game.InventoryItemCount)&&!found) {
      if (name==inventory[i].Name) {
         found = true;
      }
      else i++;
   }
   // Give it to him
   if (found) {
      this.InventoryQuanity[i] = player.InventoryQuanity[i]+amount;
      PlaySound(4);
   } 
   else {
      Display("*** Error-GiveInv, item not found (%s%)", name);
   }
}

*** Global or Room  Script ***
   // Give 12 coins to Ego
   cEgo.GiveInv("coins", 12);


Perhaps a better solution is to use the inventory objects (pointers to them) themselves instead of their names.  That would look like this and would negate the need for the search loop.
Code: ags

*** Script header ***
import function GiveInv(this Character*, Inventory *name, int amount);

*** Global Script ***
// Extender function - Gives quantity of inventory to character
function GiveInv(this Character*, Inventory *name, int amount)  {
    int i;
   // Give it to him
   if (name!=null) {
      this.InventoryQuanity[name.IDi] = this.InventoryQuanity[name.ID]+amount;
      PlaySound(4);
   } 
   else {
      Display("*** Error-GiveInv, invalid inventory item specified");
   }
}

*** Global or Room  Script ***
   // Give 12 coins to Ego
   cEgo.GiveInv(iCoins, 12);

Lufia

InventoryItem is the correct type, I don't think Inventory actually exists.

Your method is neater but maybe the topic creator has a specific reason not to handle money in the inventory. (Or not.  ;D)

Rocco

Yes, i prefer to have the ammount of money in the rpg-struct, and i also found that approach with player.InventoryQuanity
much more uncomfortable, so decided to lean on this approach from Khris -> http://www.adventuregamestudio.co.uk/yabb/index.php?topic=31554.msg405532#msg405532

and the main purpose of my function is the player search handling, the money and inv transfer is a additional thing and not the main purpose of that function, and fortunatly this case is solved now and works fine, so thx for your help.  ;D

SMF spam blocked by CleanTalk