Using a string for an object name

puzzler7
Hello!
I'm new to Quest, and I'm starting to learn it. Right now I'm working on a teleporter. When used, it asks where the player wants to be teleported to. If the player asks for "hallway", how do I convert the string "hallway" into the room "hallway"? Also, how do I check that the room the player asks for is valid i.e. exists?

HegemonKhan
some of these things are a bit advanced... showing an example in code as it's quick and easy for me to do (I'm lazy), lol ...

(if you don't know code, it'll take more time to walk you through using the GUI~Editor)

(I'm not sure if this will work... as I'm not sure about the 'Getobject()' Function/Script... if not, there's the iteration method using ObjectLists)

(another way to check if a room exists as well as a possibly desired additional condition, is the built-in 'visited' Boolean Attribute)

<object name="room">
<attr name="type_of_object" type="string">room</attr>
</object>

<object name="player">
<attr name="type_of_object" type="string">pc</attr>
</object>

<object name="orc">
<attr name="type_of_object" type="string">npc</attr>
</object>

<object name="tree">
<attr name="type_of_object" type="string">tree</attr>
</object>

<object name="teleporter">
<attr name="type_of_object" type="string">teleporter</attr>
<attr name="teleport" type="script">
teleporter_function
</attr>
<attr name="displayverbs" type="simplestringlist">teleport</attr>
</object>

<verb>
<property>teleport</property>
<pattern>teleport</pattern>
<defaultexpression>Sorry, you can't teleport for whatever the reason</defaultexpression>
</verb>

<function name="teleporter_function">
msg ("Enter location")
get input {
if (not GetObject (result) = null and not player.parent = GetObject (result) and GetString (GetObject (result), "type_of_object") = "room") {
player.parent = GetObject (result)
} else if (GetObject (result) = null) {
msg ("The object doesn't exist")
} else if (player.parent = GetObject (result)) {
msg ("You're already at this very location, silly.")
} else {
msg ("The object is not a room.")
}
}
</function>


----------

example using iteration/looping and Object List Attributes:

<object name="room">
<attr name="type_of_object" type="string">room</attr>
</object>

<object name="player">
<attr name="type_of_object" type="string">pc</attr>
</object>

<object name="orc">
<attr name="type_of_object" type="string">npc</attr>
</object>

<object name="tree">
<attr name="type_of_object" type="string">tree</attr>
</object>

<object name="teleporter">
<attr name="type_of_object" type="string">teleporter</attr>
<attr name="teleport" type="script">
teleporter_function
</attr>
<attr name="displayverbs" type="simplestringlist">teleport</attr>
</object>

<verb>
<property>teleport</property>
<pattern>teleport</pattern>
<defaultexpression>Sorry, you can't teleport for whatever the reason</defaultexpression>
</verb>

<function name="teleporter_function">
msg ("Enter location")
get input {
foreach (object_variable, AllObjects()) {
if (object_variable = Getobject (result) and not player.parent = GetObject (result) and GetString (GetObject (result), "type_of_object") = "room") {
player.parent = GetObject (result)
}
}
if (GetObject (result) = null) {
msg ("The object doesn't exist")
} else if (player.parent = GetObject (result)) {
msg ("You're already at this very location, silly.")
} else {
msg ("The object is not a room.")
}
}
</function>

The Pixie
I would recommend iterating through everything; HK's second method. It is not straightforward, but here is how I would do it (again in code).
get input {
result = LCase(result)
handled = false
foreach (o, AllObjects()) {
if (LCase(o.name) = result and not ListCount(ScopeExitsForRoom(o)) = 0) {
handled = true
player.parent = o
}
}
if (not handled) {
msg ("I don't know where " + result + " is")
}
}

I put everything inlower case so it is case insensitive. There is a flag called handled that we can test to see if a message is required to say that it failed. I check the object has at least one exit; this is probably the best way to check it is a room, not an item (there is nothing built-in bizarrely). It checks the object name, so does not allow you to use aliases,

puzzler7
Ok, thank you! I don't know the Quest code (I'm using the GUI currently, but I plan to change that in the immediate future), but I have experience in Java and Python. This helps a lot.

HegemonKhan
Quest's syntax is more similar to c++/java's obviously (all the brackets used), unlike python's more stream-lined code (no brackets and all that stuff).

since you know code, just use quest's documentation (quest's coding, I think, is pretty easy to pick up, if you know coding already):

http://docs.textadventures.co.uk/quest/

explore around the doc for what you need (or ask and we can navigate/link to what you're looking for)

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums