Dictionaries

HegemonKhan
Mods: Jay's right, this should go into the 'libraries and code samples' forum, if you could move this thread over there, it'd be much appreciated.

--------------------------------

here's an example of using dictionaries for conversion (input -> output):

(if you don't like the popup window when using 'show menu', there's this code of mine, hehe. Though, do note that there's a better way to do this, see Pixie's post below on the details of it. This is an example only for helping people understand how dictionaries work)

(HK edit: code is fixed, thanks Pixie for spotting my mistakes!)

<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="testing game stuff">
<gameid>619916da-ce93-495d-82c3-a69ae10f37d2</gameid>
<version>1.0</version>
<author>HK</author>
<start type="script">
race_string_character_creation_function
</start>
<attr name="race_stringdictionary" type="simplestringdictionary">1=human; 2=dwarf; 3=elf; 4=gnome; 5=halfling</attr>
</game>
<function name="race_string_character_creation_function"><![CDATA[
msg ("What is your race? (Please type in the number of the race you want)")
msg ("(1) human, (2) dwarf, (3) elf, (4) gnome, or (5) halfling")
get input {
if (ToInt (result) > 0 and ToInt (result) < 6) {
player.race_string = StringDictionaryItem (game.race_stringdictionary, result)
msg ("Race: " + player.race_string)
wait {
ClearScreen
}
} else {
ClearScreen
race_string_character_creation_function
}
}
]]></function>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
</object>
</asl>


what the dictionary allows for (via 'StringDictionaryItem' ) is CONCEPTUALLY:

if (input: result = 1) -> (and its conversion is: 1 = human) -> then (output: human)
if (input: result = 2) -> (and its conversion is: 2 = dwarf) -> then (output: dwarf)
if (input: result = 3) -> (and its conversion is: 3 = elf) -> then (output: elf)
if (input: result = 4) -> (and its conversion is: 4 = gnome) -> then (output: gnome)
if (input: result = 5) -> (and its conversion is: 5 = halfling) -> then (output: halfling)

<attr name="race_stringdictionary" type="simplestringdictionary">dragon=The dragon has kidnapped the princess, and it can only be killed by the legendary dragon slaying sword. ; evil wizard=The evil wizard guards the legendary dragon slaying sword.</attr>

if (input: result = "dragon") -> (conversion) -> then (output 'msg' Script: "The dragon has kidnapped the princess, and it can only be killed by the legendary dragon slaying sword.")
if (input: result = "evil wizard") -> (conversion) -> then (output 'msg' Script: "The evil wizard guards the legendary dragon slaying sword.")

-------------

Object Dictionaries are the exact same as String Dictionaries, except their output is an Object (instead of just a string:text)

'ObjectDictionaryItem'

if (input string: "apple"), then (output object: apple)

Script Dictionaries are the exact same as String Dictionaries, except their output can be ANY (and as MANY) Scripts (not just limited to a single msg script like the String Dictionary is at its coding core) that (and as) you want.

'ScriptDictionaryItem'

if (input string: "apple"), then (output scripts: see below)

player.hp = player.hp + 50
if (player.hp > 999) {
player.hp = 999
}
msg ("You eat the apple, regaining 50 hp")

if (input string: "poisoned apple"), then (output scripts: see below)

msg ("You eat the poisoned apple, losing 50 hp")
player.hp = player.hp - 50
if (player.hp <= 0) {
msg ("GAME OVER")
finish

jaynabonne
Perhaps this would go better in the "Libraries and Code Samples" section. ;)

The Pixie
I appreciate this is about dictionaries, but...

If you do not like the pop up menu of "show menu", use "ShowMenu" instead (the player can still type a number in the command bar, but can also click on a link). The former also leaves artifacts in the text story, which the latter does not, which I think is a big improvement.

<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="testing game stuff">
<gameid>619916da-ce93-495d-82c3-a69ae10f37d2</gameid>
<version>1.0</version>
<author>The Pixie</author>
<start type="script">
race_string_character_creation_function
</start>
</game>
<function name="race_string_character_creation_function">
ShowMenu ("What is your race? (Please type in the number of the race you want)", Split("Human|Dwarf|Elf|Gnome|halfing", "|"), false) {
player.race_string = result
msg ("Race: " + player.race_string)
wait {
ClearScreen
}
}
</function>
</asl>


By the way, your code has an errant double quote in the first line of the function script. Also, as your code has a less than symbol in it, it needs to be wrapped as CDATA (<![CDATA[ ... ]]>). I you stay out of full code, but just type in code for a function or script, Quest will put that in automatically as required, so easy not to realise.

Pertex
HegemonKhan wrote:Mods: Jay's right, this should go into the 'libraries and code samples' forum, if you could move this thread over there, it'd be much appreciated.

If you append a complete test file with this example I will move the topic :D

HegemonKhan
I uploaded a file of my code in my opening post, though this is from an apple computer, so hopefully it works okay... laughs. If you wanted a more complete code, that shows examples with object dictionaries and script dictionaries, that I don[t have yet.

I do plan to add to this thread (and I'll update my attached game file too), for it to be more extensive with dictionary's usages.

jaynabonne
I hope I'm not too much of a wet bucket (or whatever the euphemism is), but if you plan on uploading code that is meant to help out people who are trying to come up to speed with something they're not familiar with and possibly struggling with, then you really, Really, REALLY want to make sure it works. I can appreciate your enthusiasm, but posting blindly (without the ability to even do a smoke test beforehand) would scare the crap out of me.

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

Support

Forums