Code Lockable Cabinet...Is this the best way?
Shiv379
13 Jun 2015, 13:17Hi all!
Quest newbie so please don't judge me too harshly
I've created a cabinet with a "keypad" for unlocking. I was wondering if anyone could take a look at what I've done and let me know if this is the best way? Ideally I'd like a way to reference to cabinet object linked from the keypad so I don't have to hardcode the object names (I have four cabinets so far and will probably need more - it would be much easier not having to correct references in the code for every one!).
I'm wondering if it might actually be better to create a seperate function to handle the mechanics but I've never done this before in Quest so I'm not really sure how to do that.
Thanks!
~Shiv
P.S. My code works, I'm just not sure it's the best approach.
Quest newbie so please don't judge me too harshly

I've created a cabinet with a "keypad" for unlocking. I was wondering if anyone could take a look at what I've done and let me know if this is the best way? Ideally I'd like a way to reference to cabinet object linked from the keypad so I don't have to hardcode the object names (I have four cabinets so far and will probably need more - it would be much easier not having to correct references in the code for every one!).
I'm wondering if it might actually be better to create a seperate function to handle the mechanics but I've never done this before in Quest so I'm not really sure how to do that.
<object name="b2b4">
<inherit name="editor_room" />
<alias>ward bay</alias>
<description type="script">
OutputTextNoBr ("Next to an empty {object:b2b4_bed} stands a small {object:b2b4_cabinet} which you assume is for storing patient belongings and medication. ")
</description>
<usedefaultprefix />
<object name="b2b4_cabinet">
<inherit name="editor_object" />
<inherit name="container_open" />
<inherit name="container_lockable" />
<alias>cabinet</alias>
<attr name="feature_usegive" type="boolean">false</attr>
<feature_container />
<isopen type="boolean">false</isopen>
<hidechildren type="boolean">false</hidechildren>
<open />
<autounlock type="boolean">false</autounlock>
<look type="script">
OutputTextNoBr ("A {object:b2b4_cabinet_keypad} is mounted on the cabinet. ")
if (this.locked = True) {
msg ("The cabinet is locked.")
}
else if (this.isopen = False) {
msg ("The cabinet is closed.")
}
else {
msg ("The cabinet is open.")
}
</look>
<displayverbs type="stringlist">
<value>Look at</value>
<value>Open</value>
<value>Close</value>
</displayverbs>
<listchildren />
<object name="mobile_phone">
<inherit name="editor_object" />
<alias>mobile phone</alias>
<alt type="stringlist">
<value>phone</value>
</alt>
<take />
</object>
</object>
<object name="b2b4_cabinet_keypad">
<inherit name="editor_object" />
<alias>keypad</alias>
<feature_usegive />
<look>The keypad can be used to enter a number sequence to unlock the cabinet.</look>
<scenery />
<displayverbs type="stringlist">
<value>Look at</value>
<value>Use</value>
</displayverbs>
<use type="script">
msg ("What code should you enter?")
get input {
if (result="1234") {
set (b2b4_cabinet, "locked", false)
msg ("You enter the code and turn the handle. With a click the cabinet opens.")
HelperOpenObject (b2b4_cabinet)
ListObjectContents (b2b4_cabinet)
}
else {
msg ("Nothing happens.")
}
}
</use>
</object>
</object>
Thanks!
~Shiv
P.S. My code works, I'm just not sure it's the best approach.
HegemonKhan
13 Jun 2015, 16:16if you know some coding already, then definately look into using Functions (able to use Parameters and returning Values), Object Types (Classes), and~or Delegates to reduce code redundancy.
http://docs.textadventures.co.uk/quest/
http://docs.textadventures.co.uk/quest/guides/
http://docs.textadventures.co.uk/quest/elements/ (the OBJECTS of quest's Object-Oriented Programming, not to be confused with one of those OBJECTS~Elements: the 'Object' Element~OBJECT, within quest. OBJECTS: Elements: Objects, Exits, Functions, Commands, Verbs, Turnscripts, Timers, Object Types, etc)
http://docs.textadventures.co.uk/quest/types/ (Attributes)
http://docs.textadventures.co.uk/quest/ ... ction.html
http://docs.textadventures.co.uk/quest/ ... /type.html (Object Types)
http://docs.textadventures.co.uk/quest/ ... gates.html
Functions and Objects Types are very simple to use (I'm still trying to learn Delegates ~ being a noob to coding), examples:
-------
I'm still a code noob, so others can help with what is the best method to do what you want.
http://docs.textadventures.co.uk/quest/
http://docs.textadventures.co.uk/quest/guides/
http://docs.textadventures.co.uk/quest/elements/ (the OBJECTS of quest's Object-Oriented Programming, not to be confused with one of those OBJECTS~Elements: the 'Object' Element~OBJECT, within quest. OBJECTS: Elements: Objects, Exits, Functions, Commands, Verbs, Turnscripts, Timers, Object Types, etc)
http://docs.textadventures.co.uk/quest/types/ (Attributes)
http://docs.textadventures.co.uk/quest/ ... ction.html
http://docs.textadventures.co.uk/quest/ ... /type.html (Object Types)
http://docs.textadventures.co.uk/quest/ ... gates.html
Functions and Objects Types are very simple to use (I'm still trying to learn Delegates ~ being a noob to coding), examples:
<function name="xxx" parameters="x1x,x2x,xetcx" type="xxx">
// in the above tag code line, the 'parameters' and 'type' are optional.
// scripting~scripts
</function>
<type name="xxx">
// attributes tag lines
</type>
<!-- example below -->
<object name="player">
<inherit name="character_object_type" />
<alias>knight</alias>
<attr name="luck_integer_attribute" type="int">75</attr>
<attr name="current_life_integer_attribute" type="int">999</attr>
<attr name="maximum_life_integer_attribute" type="int">999</attr>
</object>
<object name="orc">
<inherit name="character_object_type" />
<inherit name="non_playable_character_object_type" />
<attr name="luck_integer_attribute" type="int">50</attr>
</object>
<verb>
<property>fight</property>
<pattern>fight</pattern>
<defaultexpression>"You can't fight that."</defaultexpression>
</verb>
<type name="character_object_type">
<attr name="strength_integer_attribute" type="int">25</attr>
<attr name="endurance_integer_attribute" type="int">25</attr>
<attr name="dexterity_integer_attribute" type="int">25</attr>
<attr name="agility_integer_attribute" type="int">25</attr>
<attr name="speed_integer_attribute" type="int">25</attr>
<attr name="luck_integer_attribute" type="int">25</attr>
<attr name="current_life_integer_attribute" type="int">500</attr>
<attr name="maximum_life_integer_attribute" type="int">500</attr>
<attr name="condition_string_attribute" type="string">normal</attr>
</type>
<type name="non_playable_character_object_type">
<fight type="script"><![CDATA[
if (not this.condition_string_attribute = "dead" and not player.condition_string_attribute = "dead") {
this.current_life_integer_attribute = this.current_life_integer_attribute - (player.strength_integer_attribute * critical_hit_function (player))
if (this.current_life_integer_attribute <= 0) {
this.condition_string_attribute = "dead"
msg ("You killed the orc.")
} else {
player.current_life_integer_attribute = player.current_life_integer_attribute - (this.strength_integer_attribute * critical_hit_function (this))
if (player.current_life_integer_attribute <= 0) {
player.condition_string_attribute = "dead"
msg (player.alias + " has been killed.")
}
}
} else {
msg ("The orc, player, and~or both orc and player, are dead.")
}
]]></fight>
</type>
<function name="critical_hit_function" type="int" parameters="object_parameter">
boolean_variable = RandomChance(character_parameter.luck_integer_attribute))
if (boolean_variable) {
value = 2
} else {
value = 1
}
return (value)
</function>
-------
I'm still a code noob, so others can help with what is the best method to do what you want.
The Pixie
13 Jun 2015, 16:57What you could do is set an object attribute on the keypad that points to the cabinet it is on, and a string attribute that holds the keycode. Your use script could gen look like this:
And this would be the function:
However, it is probably just as easy to do it the way you already have, just copy-and-paste the code you already have to make three more cabinets (but ensure each object has a unique name; Quest will complain otherwise).
CodeKeypad(this)
And this would be the function:
<function name="CodeKeypad" parameters="keypad">
msg ("What code should you enter?")
get input {
if (result=keypad.code) {
set (keypad.cabinet, "locked", false)
msg ("You enter the code and turn the handle. With a click the cabinet opens.")
HelperOpenObject (keypad.cabinet)
ListObjectContents (keypad.cabinet)
}
else {
msg ("Nothing happens.")
}
}
</function>
However, it is probably just as easy to do it the way you already have, just copy-and-paste the code you already have to make three more cabinets (but ensure each object has a unique name; Quest will complain otherwise).