Key Not In Dictionary

cybernetsurfer7
10 Mar 2014, 20:30
Hello, I'm trying to create a switch statement to simulate a conversation. Normally it works just fine because the previous possibilities are removed after choosing one. However, they are not removed on the mobile version. I can fix this by setting a case for when the result of the choice is not in the dictionary. Then it would just say you've already talked about that. But I can't figure out how to set that up. Here's the code:

ShowMenu ("Talk Options", IntroDanyel.talkTopics, false) {
switch (result) {
case (1) {
msg ("Topic 1, OK!<br/>")
dictionary remove (IntroDanyel.talkTopics, "1")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining - 1
}
case (2) {
msg ("Topic 2, OK!<br/>")
dictionary remove (IntroDanyel.talkTopics, "2")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining - 1
dictionary add (IntroDanyel.talkTopics, "4", "Topic 4")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining + 1
}
case (4) {
msg ("Topic 4, OK!<br/>")
dictionary remove (IntroDanyel.talkTopics, "4")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining - 1
dictionary add (IntroDanyel.talkTopics, "5", "Topic 5")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining + 1
}
case (5) {
msg ("Topic 5, OK!<br/>")
dictionary remove (IntroDanyel.talkTopics, "5")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining - 1
}
case (6) {
msg ("Topic 6, OK!<br/>")
dictionary remove (IntroDanyel.talkTopics, "6")
IntroDanyel.talkRemaining = IntroDanyel.talkRemaining - 1
}
case (999) {
msg ("Exit Convo, OK!<br/>")
MoveObject (player, LivingRoom)
IntroDanyel.leaving = true
}
case (null) {
msg ("You've already talked about that")
}
}
if (IntroDanyel.talkRemaining > 0) {
DanyelTalk1
}
else if (IntroDanyel.leaving = true) {
IntroDanyel.leaving = false
}
else {
if (DictionaryContains(IntroDanyel.talkTopics, "999") <> true) {
dictionary add (IntroDanyel.talkTopics, "999", "Exit Convo")
}
DanyelTalk1
}
}


EDIT: I thought I could add an if statement at the beginning to first see if the result was in the dictionary. That works, but then still causes an error. I'll poke at it some more.

EDIT AGAIN: I needed to call the show menu function again. It's not a totally ideal situation, but it works.

HegemonKhan
11 Mar 2014, 02:39
I don't use thus don't know about the mobile version, so I don't know what needs to be done (aka what type of workaround you need) for it to work for you.

------

though if you want to look at some code design methods of doing topics~events and removing~adding them as you select them, you can take a look at some of my works:

(see the ~ last posts of this thread: viewtopic.php?f=10&t=4224 )

my method is to use a List (String~Object) with a Dictionary (String~Object~Script), using "string comparison" (List's item String = Dictionary's item key String), to whether it activates the corresponding Dictionary's Value

thus, I can keep my Dictionary as a "database" for being "called upon" by the List.

alternating the List, for what can be activated in the dictionary, for (conceptual) example:

String List:
(1) dragon
(2) sword
(3) princess

Script dictionary:
dragon = msg ("d for dragon")
sword = msg ("s for sword")
princess = msg ("p for princess")

if (String List's String of "dragon" = Script Dictionary's String of "dragon"), then run the corresponding script: msg ("d for dragon")

well, what if I removed, "dragon", from the string list?

my "database" Script Dictionary is still intact, and also, I've eliminated the ' msg ("d for dragon") ' from running again. I've only altered my string list, which can be easily and quickly altered back to having "dragon" again.

anyways, here's two game codings of mine (though the "topic" game code doesn't have the list item removal code lines in it) that you can play around with (my "explore and travel" game code has all the elements that you want):

"Explore and Travel" Game Code:

<asl version="540">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="Testing Game Stuff">
<gameid>eef801a1-4e6b-4b0a-bdbf-8f3ecfa8389c</gameid>
<version>1.0</version>
<firstpublished>2013</firstpublished>
<turns type="int">0</turns>
<statusattributes type="simplestringdictionary">turns=</statusattributes>
<start type="script">
msg ("Important Note:")
msg ("Type in: help")
</start>
</game>
<object name="homeland">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
</object>
<object name="grassland">
<inherit name="editor_room" />
</object>
<object name="plains">
<inherit name="editor_room" />
</object>
<object name="desert">
<inherit name="editor_room" />
</object>
<object name="tundra">
<inherit name="editor_room" />
</object>
<object name="swampland">
<inherit name="editor_room" />
</object>
<object name="mountains">
<inherit name="editor_room" />
</object>
<object name="forest">
<inherit name="editor_room" />
</object>
<object name="wasteland">
<inherit name="editor_room" />
</object>
<object name="coastland">
<inherit name="editor_room" />
</object>
<object name="hills">
<inherit name="editor_room" />
</object>
<command name="help_command">
<pattern>help</pattern>
<script>
help_function
</script>
</command>
<command name="explore_command">
<pattern>explore</pattern>
<script>
explore_function
</script>
</command>
<command name="travel_command">
<pattern>travel</pattern>
<script>
travel_function
</script>
</command>
<object name="data_object">
<inherit name="editor_object" />
<travel_string_list type="simplestringlist">homeland</travel_string_list>
<homeland_events_string_list type="simplestringlist">grassland_discovery;plains_discovery;desert_discovery;tundra_discovery;swampland_discovery;forest_discovery;mountains_discovery;hills_discovery;wasteland_discovery;coastland_discovery</homeland_events_string_list>
<homeland_events_script_dictionary type="scriptdictionary">
<item key="grassland_discovery">
list add (data_object.travel_string_list, "grassland")
msg ("You've discovered the grassland! Now, you can travel to the grassland and explore it!")
</item>
<item key="plains_discovery">
list add (data_object.travel_string_list, "plains")
msg ("You've discovered the plains! Now, you can travel to the plains and explore it!")
</item>
<item key="desert_discovery">
list add (data_object.travel_string_list, "desert")
msg ("You've discovered the desert! Now, you can travel to the desert and explore it!")
</item>
<item key="tundra_discovery">
list add (data_object.travel_string_list, "tundra")
msg ("You've discovered the tundra! Now, you can travel to the tundra and explore it!")
</item>
<item key="swampland_discovery">
list add (data_object.travel_string_list, "swampland")
msg ("You've discovered the swampland! Now, you can travel to the swampland and explore it!")
</item>
<item key="forest_discovery">
list add (data_object.travel_string_list, "forest")
msg ("You've discovered the forest! Now, you can travel to the forest and explore it!")
</item>
<item key="mountains_discovery">
list add (data_object.travel_string_list, "mountains")
msg ("You've discovered the mountains! Now, you can travel to the mountains and explore it!")
</item>
<item key="hills_discovery">
list add (data_object.travel_string_list, "hills")
msg ("You've discovered the hills! Now, you can travel to the hills and explore it!")
</item>
<item key="wasteland_discovery">
list add (data_object.travel_string_list, "wasteland")
msg ("You've discovered the wasteland! Now, you can travel to the wasteland and explore it!")
</item>
<item key="coastland_discovery">
list add (data_object.travel_string_list, "coastland")
msg ("You've discovered the coastland! Now, you can travel to the coastland and explore it!")
</item>
</homeland_events_script_dictionary>
</object>
<turnscript name="global_turnscript">
<enabled />
<script>
game.turns = game.turns + 1
</script>
</turnscript>
<function name="help_function">
msg ("Type 'explore' to explore your area.")
msg ("Type 'travel' to travel to different areas.")
</function>
<function name="explore_function"><![CDATA[
switch (game.pov.parent) {
case (homeland) {
result_1 = ListCount (data_object.homeland_events_string_list) - 1
if (result_1 >= 0) {
result_2 = StringListItem (data_object.homeland_events_string_list,GetRandomInt(0,result_1))
invoke (ScriptDictionaryItem (data_object.homeland_events_script_dictionary,result_2))
on ready {
foreach (item_x, split ("grassland_discovery;plains_discovery;desert_discovery;tundra_discovery;swampland_discovery;forest_discovery;mountains_discovery;hills_discovery;wasteland_discovery;coastland_discovery",";")) {
if (result_2 = item_x) {
list remove (data_object.homeland_events_string_list, result_2)
}
}
}
} else {
msg ("There seemingly is nothing left to explore in this area.")
}
}
}
]]></function>
<function name="travel_function">
show menu ("Where do you wish to travel?",data_object.travel_string_list,false) {
if (not game.pov.parent = GetObject (result)) {
game.pov.parent = GetObject (result)
} else {
msg ("You are already at this area.")
ask ("Try again?") {
if (result=true) {
travel_function
} else {
msg ("You realize that you need to discover a new area to travel to first, before you can travel to that place.")
}
}
}
}
</function>
</asl>


"Topics" Game Code:

(missing the code lines for removing a topic choice, as that's not what I wanted to occur for this test of code design)

<asl version="540">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="Testing Game Stuff">
<gameid>d67ec73f-f879-4911-9d88-c02ea527c534</gameid>
<version>1.0</version>
<firstpublished>2013</firstpublished>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
<object name="wise_owl_npc">
<inherit name="editor_object" />
<alias>Wise Owl</alias>
<displayverbs type="stringlist">
<value>Inquire</value>
</displayverbs>
<inquire type="script">
show menu ("What do you wish to inquire about?", global_data_object.inquire_topics_string_list, false) {
invoke (ScriptDictionaryItem (global_data_object.inquire_topics_script_dictionary, result))
}
</inquire>
</object>
</object>
<object name="global_data_object">
<inherit name="editor_object" />
<inquire_topics_string_list type="stringlist">
<value>princess</value>
<value>sword</value>
<value>dragon</value>
</inquire_topics_string_list>
<dragon_topics_string_list type="stringlist">
<value>weakness</value>
<value>location</value>
</dragon_topics_string_list>
<inquire_topics_script_dictionary type="scriptdictionary">
<item key="princess">
msg ("The princess has been captured by the dragon. Rescue her, and the King will wed thee to her, as his heir to his throne and kingdom! You'll become the new king of this land!")
</item>
<item key="sword">
msg ("Ah the legendary dragon slayer sword, magically forged... and guarded by a powerful sorcerer...")
</item>
<item key="dragon">
show menu ("What about the dragon do you wish to know?", global_data_object.dragon_topics_string_list, false) {
invoke (ScriptDictionaryItem (global_data_object.inquire_topics_script_dictionary, result))
}
</item>
<item key="weakness">
msg ("Only the legendary dragon slayer sword can pierce its adamantium scales and thus kill it")
</item>
<item key="location">
msg ("within the volcano in the lava scorched wasteland far to the eastern edge of this world")
</item>
</inquire_topics_script_dictionary>
</object>
<verb name="inquire">
<property>inquire</property>
<pattern>Inquire</pattern>
</verb>
</asl>