Find_Property: Implement searching for specific value (#2021)

master
Lukas Werling 2018-04-08 15:00:27 +02:00
parent 5dc4282b56
commit d9f9a2a9a3
4 changed files with 32 additions and 9 deletions

View File

@ -7,26 +7,38 @@
<title>Find_Property</title>
<category>Objects</category>
<subcat>Search</subcat>
<version>7.0 OC</version>
<version>7.0 OC<extversion>9.0 OC</extversion></version>
<syntax>
<rtype>array</rtype>
<params>
<param>
<type>string</type>
<name>function</name>
<desc>Function to call</desc>
<name>property</name>
<desc>Name of the property to check</desc>
</param>
<param>
<type>any</type>
<name>value</name>
<desc>Value the property should match</desc>
<optional />
</param>
</params>
</syntax>
<desc>Search criterion: Finds all objects which have the property set to a value that converts to boolean true.</desc>
<desc>Search criterion: Finds all objects which have the property set to a value that converts to boolean true or equals the given value.</desc>
<remark>Due to C4Script limitations, it is not possible to search for value nil with this function.</remark>
<remark>For additional information on the use of this function see <funclink>FindObjects</funclink>.</remark>
<examples>
<example>
<code><funclink>FindObjects</funclink>(<funclink>Find_Distance</funclink>(20), <funclink>Find_Property</funclink>(&quot;Collectible&quot;))</code>
<code><funclink>FindObjects</funclink>(<funclink>Find_Distance</funclink>(20), Find_Property(&quot;Collectible&quot;))</code>
<text>Returns all nearby collectible objects.</text>
</example>
<example>
<code><funclink>FindObjects</funclink>(Find_Property(&quot;Visibility&quot;, VIS_None))</code>
<text>Returns all invisible objects. Note that <emlink href="definition/visibility.html">Visibility</emlink> is a bitmap, but Find_Property compares the whole value.</text>
</example>
</examples>
<related><funclink>FindObjects</funclink></related>
</func>
<author>Sven2</author><date>2015-08</date>
<author>Luchs</author><date>2018-04</date>
</funcs>

View File

@ -214,9 +214,14 @@ global func Find_InArray(array a)
}
// documented in /docs/sdk/script/fn
global func Find_Property(string s)
global func Find_Property(string s, value)
{
return [C4FO_Property, s];
// Ideally, we'd check the parameter count here, but this information is
// not available from the script.
if (value == nil)
return [C4FO_Property, s];
else
return [C4FO_Property, s, value];
}
// documented in /docs/sdk/script/fn

View File

@ -241,7 +241,9 @@ C4FindObject *C4FindObject::CreateByValue(const C4Value &DataVal, C4SortObject *
C4String *pStr = Data[1].getStr();
if (!pStr) return nullptr;
// Construct
C4FindObjectProperty *pFO = new C4FindObjectProperty(pStr);
C4FindObjectProperty *pFO = Data.GetSize() >= 3
? new C4FindObjectProperty(pStr, Data[2])
: new C4FindObjectProperty(pStr);
// Done
return pFO;
}
@ -837,7 +839,7 @@ bool C4FindObjectProperty::Check(C4Object *pObj)
{
assert(Name); // checked in constructor
C4Value value;
return pObj->GetPropertyByS(Name, &value) && value.getBool();
return pObj->GetPropertyByS(Name, &value) && (have_value ? value == Value : value.getBool());
}
bool C4FindObjectProperty::IsImpossible()

View File

@ -382,8 +382,12 @@ class C4FindObjectProperty : public C4FindObject
{
public:
C4FindObjectProperty(C4String * Name) : Name(Name) { }
C4FindObjectProperty(C4String * Name, const C4Value &Value)
: Name(Name), have_value(true), Value(Value) { }
private:
C4String * Name;
bool have_value = false;
C4Value Value;
protected:
bool Check(C4Object *pObj) override;
bool IsImpossible() override;