Public Functions:Assets
game.$()
game.$("name") takes a name or ID and returns a game asset. The
results of MyGame.$() can be chained, meaning that you
can immediately call a function on the returned asset, for example:
MyGame.$("treasure chest").getContents(). If no asset is found,
a special null asset will be returned to prevent Javascript errors. A
chained function called on the null asset will always return false or null.
-
MyGame.$("asset")
{String}
asset a name or id representing an
asset
Object
MyGame.createAsset({ class: "Lantern", name: "brass lantern"}); // is the brass lantern in the crystal cave? if( MyGame.$("brass lantern").getRoom() === "crystal cave" ) { // do something };
TMI game.$() vs global variables
We offer game.$() as an alternative to saving asset
references to global variables. Of course you can save
references if you prefer. Here is an example of saving a reference to an
asset as it is created.
var lantern = MyGame.createAsset({ class: "Lantern", name: "brass lantern"});
// what room is the brass lantern in?
lantern.getRoom();
In this example, the variable lantern becomes globally
available and can be referred to from any other script. By contrast,
here's an example that uses the get asset function.
MyGame.createAsset({ class: "Lantern", name: "brass lantern"});
// what room is the brass lantern in?
MyGame.$("brass lantern").getRoom();
The results are the same. So what's the benefit of using the get asset function vs making your own reference? Well, there are several.
-
game.$()helps maintain consistency. Notice how we used a shortcut name for the reference we created?lanternvs the asset's actual name ofbrass lantern. That's because as humans, we're always tempted to shorten things, and it's a choice, not an error, but now let's say you have several lanterns in your game and you've forgotten which asset you assigned to which variable. Using the asset's actual name will always give you a clean identifier. -
game.$()keeps the global namespace clean. Using the get asset function protects against "polluting the global namespace", as some developers would say. What that means is that your global variables get written to the global namespace, where they float around in one big pond together with all of the web browser's built-in functions, which can get very messy; and because global variables never get garbage collected, they continue to take up memory even if they're not needed. We're not going to delve any more deeply into it here, but we invite you to look it up for further reference, if you're so inclined. -
game.$()protects against Javascript errors. If your asset is ever destroyed, your global reference to it will returnundefined, meaning that if you call a method on it, that will throw a Javascript error. But, thegame.$()will return a specialnullasset that has most of the common asset functions, and will not throw an error.
The bottom line is: if you're new to Javascript development, you're
probably safer using game.$(). If you're an experienced
developer and you feel comfortable managing your own references in the
global namespace, go for it.
TMI game.$() vs game.getAsset()
If you go looking through the AdventureJS code, you'll find frequent
uses of game.getAsset(). The methods
game.getAsset() and
game.() are very similar but have important
distinctions. Chief among them is that when
game.getAsset() fails to find an asset, it returns
null, but when game.$() fails to find
an asset, it returns a special asset of class
Null. The Null asset
exists specifically to catch chained author queries such as
MyGame.$("foo").to("bar") without throwing a Javascript
error.
Properties
These specific properties provide shortcuts to a few key pieces of asset information.
-
MyGame.$("asset").room
This method returns the object of the room that the specified asset is
in. Returns
falseif the asset has no room (which would only be the case if it was in limbo). Note that this method only works for assets in the Entity class tree, and will return null for other asset classes. -
MyGame.$("asset").parent
Returns the object of the asset that the specified asset is
in/on/under/behind(etc). If the asset has no parent but is in a room,
the room will be returned. Returns
falseif the asset has no parent (which would only be the case if it was in limbo). Note that this method only works for assets in the Entity class tree, and will return null for other asset classes. - MyGame.$("asset").contents Returns all the contents of an asset. This is a shallow list that doesn't return nested assets.
$is()
asset.$is() is a broad utility function that houses shortcuts
for accessing various entity asset properties. Note that this method only
works for assets in the
Entity class tree, and
will return null for other asset classes.
-
MyGame.$("asset").$is("in", "asset
name")
{String} a preposition or property
{String} an optional asset name or
id
Boolean
Some of the methods here may be redundant to other shortcut methods.
Feel free to use whichever method makes more sense to you.
- MyGame.$("asset").$is("in", "other asset") accepts any preposition, asking, is this asset in that aspect of that asset?
- MyGame.$("asset").$is("behind") accepts a preposition without an asset, asking, is this asset [preposition] any asset?
- MyGame.$("asset").$is("nested in", "other asset") nested in, specific to character classes, asking, is this asset nested in that asset? For instance, is player on trapdoor?
- MyGame.$("asset").$is("closed") asking, is this asset closed?
- MyGame.$("asset").$is("locked") asking, is this asset locked?
- MyGame.$("asset").$is("plugged") asking, is this asset plugged?
- MyGame.$("asset").$is("sealed") asking, is this asset sealed?
- MyGame.$("asset").$is("zipped") asking, is this asset zipped?
- MyGame.$("asset").$is("open") asking, is this asset open?
- MyGame.$("asset").$is("empty") asking, is this asset empty? This considers entity assets and substances in all aspects.
- MyGame.$("asset").$is("unlocked") asking, is this asset unlocked?
- MyGame.$("asset").$is("unplugged") asking, is this asset unplugged?
- MyGame.$("asset").$is("unsealed") asking, is this asset unsealed?
- MyGame.$("asset").$is("held", "other asset") asking, is this asset held by that asset, as in a bannister held by player?
- MyGame.$("asset").$is("holding", "other asset") asking, is this asset holding that asset, as in player holding a rope?
- MyGame.$("asset").$is("worn") asking, is this asset being worn?
- MyGame.$("asset").$is("wearing", "other asset") asking, is this asset wearing the other asset?
- MyGame.$("asset").$is("arbitrary_property") asking, does this asset have asset.is.arbitrary_property and is that property set to true? Will return false if the asset does not have the property and will not error.
isAspect()
This is an alternate way of asking whether one asset is in or on or under or
behind another aspect. These are functionally equivalent to using
asset.$is(). You're welcome to use whichever version you
prefer. Note that these methods only work for assets in the
Entity class tree, and
will return false for non-entity asset classes.
-
MyGame.$("asset").isAttached("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This is a shortcut method to ask whether one asset is specifically in
the
attachedaspect of another aspect, ignoring nested assets. This is functionally identical to usingMyGame.$("asset").$is("attached", "other asset"). -
MyGame.$("asset").isBehind("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This is a shortcut method to ask whether one asset is specifically in
the
behindaspect of another aspect, ignoring nested assets. This is functionally identical to usingMyGame.$("asset").$is("behind", "other asset"). -
MyGame.$("asset").isIn("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This is a shortcut method to ask whether one asset is specifically in
the
inaspect of another aspect, ignoring nested assets. This is functionally identical to usingMyGame.$("asset").$is("in", "other asset"). -
MyGame.$("asset").isOn("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This is a shortcut method to ask whether one asset is specifically in
the
onaspect of another aspect, ignoring nested assets. This is functionally identical to usingMyGame.$("asset").$is("on", "other asset"). -
MyGame.$("asset").isUnder("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This is a shortcut method to ask whether one asset is specifically in
the
underaspect of another aspect, ignoring nested assets. This is functionally identical to usingMyGame.$("asset").$is("under", "other asset"). - MyGame.$("asset").isWithin("asset name") {String} asset a name or id representing an asset Boolean This is a shortcut method to ask whether one asset is contained anywhere inside of another, inclusive of all of the parent asset's aspects. Which is to say, this will return true if the child asset is in or under or behind or on (or any other preposition) the parent asset, even if it's nested within other assets.
Misc
- MyGame.$("asset").has("asset name") {String} asset a name or id representing an asset Boolean This is the reverse way of asking asset.isWithin("other asset"), a shortcut method to ask whether one asset has another asset inside of it, inclusive of all of the parent asset's aspects. Which is to say, this will return true if the child asset is in or under or behind or on (or any other preposition) the parent asset. Whether you use assetA.isWithin(assetB) or assetB.has(assetA) is purely a matter of preference.
- MyGame.$("asset").doesContain("asset") {String} asset a name or id representing an asset Boolean This is a shortcut method to ask whether one asset contains a substance, ie: does the bucket contain water? (Though it will also return true for entities, ie: does the bucket contain the shovel?) While it's similar to isWithin(), it only does a shallow search that doesn't consider nested items.
Getters
Asset getter functions return useful information about assets of the Entity class.
-
MyGame.$("asset").getRoom()
Object
This method returns the object of the Room that the specified asset is
in. Returns
falseif the asset has no room (which would only be the case if it was in limbo). Note that this method only works for assets in the Entity class tree, and will return null for other asset classes. There is a shortcut to this property:MyGame.$("asset").room -
MyGame.$("asset").getParent()
Object
This method returns the object of the asset that the specified asset is
in/on/under/behind(etc). If the asset has no parent but is in a room,
the room will be returned. Returns
falseif the asset has no parent (which would only be the case if it was in limbo). Note that this method only works for assets in the Entity class tree, and will return null for other asset classes. There is a shortcut to this at:MyGame.$("asset").parent -
MyGame.$("asset").getContents("in")
{String}
aspect a preposition, aka attached /
behind / in / on / under or "all"
Array
This is a method to get the contents of an asset's aspect. Takes a
preposition and returns
asset.aspects[aspect].contents. Can take "all" as
a param and will return the contents of all aspects. Returns
falseif the asset doesn't have the specified aspect. This is a shallow list that doesn't return nested assets.
Placement
Asset placement functions include a set of methods for moving one Entity classed asset into or out of an Aspect of another asset. Aspects are containers representing in, on, under, behind, or attached.
- MyGame.$("asset").to("on","asset name") {String} aspect a preposition representing an aspect {String} asset a name or id representing an asset MyGame.$("asset").to({ on: "asset name" }) {Object} an object with one property where the key is an aspect and the value is an asset name or id Boolean This method sends one asset into the specified aspect of another asset, for instance MyGame.$("sword").to("in", "scabbard"). Note that this method bypasses the usual behavior that calls verb actions, so for instance if sword.doMoveThisToThat("scabbard") were set to a custom function, that function would not be called by to(). If you wanted the verb action to be called, you could instead use MyGame.$("sword").moveTo("scabbard"), which does invoke verb actions.
- MyGame.$("asset").from("asset name") {String} asset a name or id representing an asset Boolean This method moves one asset away from another asset, without specifying a new destination. For instance MyGame.$("sword").from("scabbard") would result in the sword being moved into no place, a limbo outside of the game world. Note that this method bypasses the usual behavior that calls verb actions, so for instance if sword.doMoveThisFromThat("scabbard") were set to a custom function, that function will not be called by from. If you wanted that function to be called, you could instead use MyGame.$("sword").moveFrom("scabbard"), which does invoke verb actions.
.move() alternate
These methods are convenience aliases. They use .move() instead
of .$(), which does exactly the same thing, which is to say
that it gets an asset and lets you chain a second function call. Feel free
to use these if you find them easier to remember.
-
MyGame.move("asset").to("on","asset
name")
{String}
aspect a preposition representing an
aspect
{String}
asset a name or id representing an
asset
MyGame.move("asset").to({ on: "asset"
})
{Object} an object with one property
where the key is an aspect and the value is an asset name or id
Boolean
This method sends one asset into the specified aspect of another asset,
for instance
MyGame.$("sword").to("in", "scabbard"). Note that
this method bypasses the usual behavior that calls
verb actions, so for
instance if
scabbard.doMoveThatToThis("sword") were set to a
custom function, that function would not be called by
to(). If you wanted the verb action to be called,
you could instead use
MyGame.$("sword").moveTo("scabbard"), which does
invoke verb actions.
MyGame.move("asset").to("asset")is functionally identical toMyGame.$("asset").to("asset"). It's offered as an alias. -
MyGame.move("asset").from("asset
name")
{String}
asset a name or id representing an
asset
Boolean
This method moves one asset away from another asset, without specifying
a new destination. For instance
MyGame.move("sword").from("scabbard") would result
in the sword being moved into no place, a limbo outside of the game
world. Note that this method bypasses the usual behavior that calls
verb actions, so for
instance if
scabbard.doMoveThatFromThis("sword") were set to a
custom function, that function will not be called by
from. If you wanted that function to be called,
you could instead use
MyGame.$("sword").moveFrom("scabbard"), which does
invoke verb actions.
MyGame.move("asset").from("asset")is functionally identical toMyGame.$("asset").from("asset"). It's offered as an alias.
Verbs
- MyGame.$("asset").hasQuirk("verb_means_action") Boolean This method returns a boolean indicating whether the asset has the specified verb quirk.
- MyGame.$('asset').getQuirks() Object This method returns a list of the asset's verb quirks.
- MyGame.$("asset").$didDo("verb name") {String} the name of a verb Boolean This method asks whether the specified verb has ever been successfully applied to the specified asset as a direct object. Useful for writing one-time-use logic.
- MyGame.$("asset").$didTry("verb name") {String} the name of a verb Boolean This method asks whether the specified verb has ever been attempted on the specified asset as a direct object. This will return true regardless if the verb attempt failed or succeeded.
- MyGame.$("asset").$doCount("verb name") {String} the name of a verb Int This method asks how many times the specified verb has been successfully applied to the specified asset as a direct object. Useful for writing do-on-nth-time logic.
- MyGame.$("asset").$tryCount("verb name") {String} the name of a verb Int This method asks how many times the specified verb has been attempted on the specified asset as a direct object. Useful for writing do-on-nth-time logic. The count includes both failures and successes.
- MyGame.$("asset").iDidVerb("verb name") {String} the name of a verb Boolean This method asks whether the specified verb has ever successfully used the specified asset as a indirect object. Useful for writing one-time-use logic.
- MyGame.$("asset").$iDidTry("verb name") {String} the name of a verb Boolean This method asks whether the specified verb has ever attempted to use the specified asset as a indirect object. This will return true regardless if the verb attempt failed or succeeded.
- MyGame.$("asset").$iDoCount("verb name") {String} the name of a verb Int This method asks how many times the specified verb has successfully used the specified asset as an indirect object. Useful for writing do-on-nth-time logic.
- MyGame.$("asset").$iTryCount("verb name") {String} the name of a verb Int This method asks how many times the specified verb has attempted to use the specified asset as an indirect object. Useful for writing do-on-nth-time logic. The count includes both failures and successes.