Difference between revisions of "CInventory::hasRequirements"

From King Arthur's Gold Wiki
Jump to: navigation, search
(Created page with "<onlyinclude> Checks 1 or 2 inventories to see if they have the given requirements, and writes any missing requirements into a given CBitStream. Returns true if the requirem...")
 
Line 1: Line 1:
 
<onlyinclude>
 
<onlyinclude>
Checks 1 or 2 inventories to see if they have the given requirements, and writes any missing requirements into a given [[CBitStream]].
+
Checks if this inventory contains the given requirements, and writes any missing requirements into a given [[CBitStream]].
  
 
Returns true if the requirements are met, otherwise false.
 
Returns true if the requirements are met, otherwise false.
 
</onlyinclude>
 
</onlyinclude>
  
This is primarily for use in conjunction with other requirement-handling methods.
+
This essentially calls [[hasRequirements]] just for this inventory (passing null for the other inventory argument).
You'll likely more often use the [[CInventory]]::[[CInventory::hasRequirements|hasRequirements]] method instead, but this version of the function allows two inventories to be checked at once. However, if you pass a NULL reference for one of the inventory arguments it will only check one inventory.
+
 
+
The inventory1 & inventory2 parameters are both [[CInventory]] objects that will be checked to see if they contain the given requirements.
+
 
The requirements parameter is the [[CBitStream]] you're reading the requirements from.
 
The requirements parameter is the [[CBitStream]] you're reading the requirements from.
 
The missing parameter is a [[CBitStream]] that any missing requirements will be written into.
 
The missing parameter is a [[CBitStream]] that any missing requirements will be written into.
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
bool hasRequirements( CInventory@ inventory1, CInventory@ inventory2, CBitStream@ requirements, CBitStream@ missing )
+
bool hasRequirements(CBitStream@ requirements, CBitStream@ missing)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Example from Entities/Workshops/Scripts/WorkshopTasks.as:
+
Example from Entities/Characters/Scripts/BuilderInventory.as:
<syntaxhighlight lang="cpp" highlight="12">
+
<syntaxhighlight lang="cpp" highlight="8">
void AddWorkshopTasksButtons( CBlob@ this, CBlob@ caller, CGridMenu@ menu )
+
void onCreateInventoryMenu( CInventory@ this, CBlob@ forBlob, CGridMenu @gridmenu )
 
{
 
{
   int tasksCount = this.getProperties().getScriptObjectGroupSize( "tasks" );
+
   CBitStream reqs, missing;
   if (menu !is null)
+
  AddRequirement( reqs, "blob", "mat_stone", "Stones", 10 );
 +
  CGridButton @button = gridmenu.AddButton( "$EmptyShop$", "Make Workshop", Builder::make_workshop );
 +
   if (button !is null)
 
   {
 
   {
     for (int i = 0; i < tasksCount; i++)
+
     if (this.hasRequirements( reqs, missing ))
    {
+
       SetButtonRequirementsText( button, reqs, false );
      WorkshopTask @wt = cast<WorkshopTask@>(this.getProperties().getScriptObject( "tasks", i));
+
    else
 
+
       SetButtonRequirementsText( button, missing, true );
      CBitStream bsMissing;
+
      CBitStream combinedReqs;
+
      if (hasRequirements( this.getInventory(), caller.getInventory(), wt.requirements, bsMissing ))
+
       {
+
        CBitStream params;
+
        params.write_u16(caller.getNetworkID());
+
        CGridButton@ button = menu.AddButton( wt.iconName, wt.name, WshpTasks::make_task + i, params );
+
        SetButtonRequirementsText( button, wt.requirements, false );
+
      }
+
      else
+
       {
+
        CGridButton@ button = menu.AddButton( "$NONE$", wt.name, WshpTasks::nil );
+
        SetButtonRequirementsText( button, bsMissing, true );
+
      }
+
    }
+
 
   }
 
   }
 
}
 
}

Revision as of 21:11, 19 August 2012

Checks if this inventory contains the given requirements, and writes any missing requirements into a given CBitStream.

Returns true if the requirements are met, otherwise false.


This essentially calls hasRequirements just for this inventory (passing null for the other inventory argument). The requirements parameter is the CBitStream you're reading the requirements from. The missing parameter is a CBitStream that any missing requirements will be written into.

bool hasRequirements(CBitStream@ requirements, CBitStream@ missing)

Example from Entities/Characters/Scripts/BuilderInventory.as:

void onCreateInventoryMenu( CInventory@ this, CBlob@ forBlob, CGridMenu @gridmenu )
{
  CBitStream reqs, missing;
  AddRequirement( reqs, "blob", "mat_stone", "Stones", 10 );
  CGridButton @button = gridmenu.AddButton( "$EmptyShop$", "Make Workshop", Builder::make_workshop );
  if (button !is null)
  {
    if (this.hasRequirements( reqs, missing ))	      SetButtonRequirementsText( button, reqs, false );
    else
      SetButtonRequirementsText( button, missing, true );	
  }
}


Object method of: CInventory