HasDictionary??

jdpjdpjdp
29 Dec 2015, 01:25
It's often very useful to be able to write commands that check not only for the existence of an attribute, but also what type an attribute is before going further: "HasString" and "HasScript" being the prime examples. I know that there is no "HasDictionary", but I was wondering if there were some equivalent way of checking if a given attribute is a script dictionary before proceeding? I've looked through the documentation and I can't find it, assuming it even exists. I'm not talking about checking the contents of a dictionary (I know how to do that), I want to simply check if the attribute exists and is of the type "script dictionary".

jaynabonne
29 Dec 2015, 10:27
Here's a function that should do it:

  <function name="HasDictionary" parameters="object,attribute" type="boolean">
if (not HasAttribute(object, attribute)) {
return (false)
}
value = GetAttribute(object, attribute)
return(EndsWith(TypeOf(value), "dictionary"))
</function>


It should return true for an existing attribute whose type ends with "dictionary" (e.g. "dictionary", "stringdictionary", "scriptdictionary").

Here's a full example:

<!--Saved by Quest 5.6.5783.24153-->
<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="hasdict">
<gameid>4264d739-8b26-423f-ac93-6e241c32d3d7</gameid>
<version>1.0</version>
<firstpublished>2015</firstpublished>
<start type="script">
msg ("HasDictionary(foo)=" + HasDictionary(player, "foo"))
msg ("HasDictionary(dict)=" + HasDictionary(player, "dict"))
msg ("HasDictionary(name)=" + HasDictionary(player, "name"))
msg ("HasDictionary(stringdict)=" + HasDictionary(player, "stringdict"))
</start>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
<stringdict type="stringdictionary" />
<dict type="dictionary" />
<scriptdict type="scriptdictionary" />
</object>
</object>
<function name="HasDictionary" parameters="object,attribute" type="boolean">
if (not HasAttribute(object, attribute)) {
return (false)
}
value = GetAttribute(object, attribute)
return(EndsWith(TypeOf(value), "dictionary"))
</function>
</asl>

jdpjdpjdp
29 Dec 2015, 19:27
That looks to be exactly what I was looking for. Thank you! :)