Skip to content
Permalink
Browse files

Find_Property: Implement searching for specific value (#2021)

  • Loading branch information
lluchs committed Apr 8, 2018
1 parent 5dc4282 commit d9f9a2a9a348b77cd3b1c9a1eb2d33f693bc62aa
Showing with 33 additions and 10 deletions.
  1. +17 −5 docs/sdk/script/fn/Find_Property.xml
  2. +8 −3 planet/System.ocg/FindObject.c
  3. +4 −2 src/object/C4FindObject.cpp
  4. +4 −0 src/object/C4FindObject.h
@@ -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>
@@ -214,9 +214,14 @@ global func Find_InArray(array a)
}

// documented in /docs/sdk/script/fn
global func Find_Property(string s)
{
return [C4FO_Property, s];
global func Find_Property(string s, value)
{
// 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
@@ -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()
@@ -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;

0 comments on commit d9f9a2a

Please sign in to comment.
You can’t perform that action at this time.