Game Development Community

what is the work of this codes of inventory.cs?

by Ahsan Muzaheed · in Torque 3D Beginner · 07/21/2011 (8:00 am) · 1 replies

function ShapeBase::setInventory
of
inventory.cs contains this codes:

function ShapeBase::setInventory(%this, %data, %value)
{
   if (%value < 0)
      %value = 0;
   else
   {
      %max = %this.maxInventory(%data);
      if (%value > %max)
         %value = %max;
   }

   // Set the value and invoke object callbacks
   %name = %data.getName();
   if (%this.inv[%name] != %value)
   {
      %this.inv[%name] = %value;
      %data.onInventory(%this, %value);
      %this.getDataBlock().onInventory(%data, %value);
   }
   return %value;
}

cannot understand the purpose of this part:

%name = %data.getName();
   if (%this.inv[%name] != %value)
   {
      %this.inv[%name] = %value;
      %data.onInventory(%this, %value);
      %this.getDataBlock().onInventory(%data, %value);
   }

and
what is inv[%name] and where it located?

About the author

Torque 3D enthusiastic since 2010.Have been working in several T3D projects besides of Unreal Engine 4 and Unity 3D. NEED a hand with your project? SHoot me a mail. http://www.garagegames.com/community/forums /viewthread/138437/


#1
07/21/2011 (9:24 am)
Quote:what is inv[%name] and where it located?
It's a dynamic variable set during those functions. If it does not exist, it is created on that line. It does not exist in any datablock.

If I were to add comments to the code, it would look like this:

// Get the name of ItemData datablock
%name = %data.getName();

// If the object's inventory (at %name index) does not
// match the value
if (%this.inv[%name] != %value)
{
   // Set the new value of that inventory slot
   %this.inv[%name] = %value;

   // Execute the onInventory callback for the ItemData
   %data.onInventory(%this, %value);

   // Execute the onInventory callback for the ShapeBase
   // This is most likely the PlayerData
   %this.getDataBlock().onInventory(%data, %value);
}