What use is IsDefined?

The Pixie
12 Nov 2013, 07:57
I am curious when you might use the IsDefined function. It seems to only apply to variables, not to objects, attributes, types, commands or functions. Variables exist only within the context of a script, so it would seem to be testing when you have used a variable already in this script. Why not just look further up the script?

What am I missing here?

Liam315
12 Nov 2013, 08:48
I believe it's used for commands to handle the possibility of one parameter not existing.

e.g. command pattern: throw <object1> at <object2>

if (not IsDefined("object2")) {
blah
}
else{
blah
}

Thus "throw object" and "throw object at object2" can both be handled by the parser. Simply using if object2=null will cause quest to throw up an error.

The Pixie
13 Nov 2013, 09:24
I do not think that that makes sense. Your command pattern will only be a match if both object1 and object2 are identified. If object2 is absent, Quest says "I don't understand your command." because the pattern matching fails. If object2 is not recognised it says "I can't see that. (fjghjhj)" (or whatever). It only runs your script if both object1 and object2 are defined, so there is no need to test if that is the case.

I have looked at the core libraries, and I am more confused than ever. It is in OnEnterRoom:

  <function name="OnEnterRoom" parameters="oldRoom">
<![CDATA[
game.displayroomdescriptiononstart = false
if (IsDefined("oldRoom")) {
if (oldRoom <> null) {
if (HasScript(oldRoom, "onexit")) {
do (oldRoom, "onexit")
}
}
}
// etc


So here it is apparently checking if the parameter was sent. But Quest throws an error if a function is expecting a parameter, but is not sent one (like this: "Error running script: No parameters passed to TestFunction function - expected 1 parameters"), so again it seems redundant.

The other two places it is used is in a couple of scripts. This is from Coreparser, and is related to what you said:

    <script type="script">
<![CDATA[
if (not IsDefined("object2")) {
object2 = null
}
//etc.


And in CoreTypes:

    <changedparent type="script">
if (game.pov = this) {
if (IsDefined("oldvalue")) {
OnEnterRoom(oldvalue)
}
else {
OnEnterRoom(null)
}
}
// etc.


How can oldvalue or object2 be defined in these places? Where do they exist? What is their scope? I understood variables to be defined only within the scope of the script itself.

Pertex
13 Nov 2013, 10:20
There seems to be internal objects like oldroom which are created and handled by Quest

The Pixie
13 Nov 2013, 11:03
It is for objects?

That makes sense, but is not at all what the Wiki says:

IsDefined (string variable name)

Returns a boolean indicating whether the specified variable name is defined in the current scope.


Pertex
13 Nov 2013, 14:08
The Pixie wrote:It is for objects?


Think so, but nonetheless I would use GetObject instead of IsDefined

Liam315
14 Nov 2013, 00:52
The Pixie wrote:I do not think that that makes sense. Your command pattern will only be a match if both object1 and object2 are identified. If object2 is absent, Quest says "I don't understand your command." because the pattern matching fails. If object2 is not recognised it says "I can't see that. (fjghjhj)" (or whatever). It only runs your script if both object1 and object2 are defined, so there is no need to test if that is the case.


In this case I meant if your command pattern has multiple cases where some omit a variable present in others.

e.g. throw #object1# at #object2#; throw #object1#

It allows the same command to account for multi object verbs and single object verbs in the same script. Although GetObject may work for this particular example, other variables like #text# wouldn't be handled.