[SOLVED] Overwriting a dictionary entry?

TinFoilMkIV
As far as I understand, it seems dictionaries function very similarly to lists but use a key instead of an integer as their index values. My question is how would one go about overwriting a dictionary entry with a variable function, as in not looking at one specific entry but being able to look up any entry to modify it.

The Pixie
Here is a complete game for you. There is a new function, DictionaryAdd, that can be used to add new entries and overwrite old, and a start script that shows it in use.
<!--Saved by Quest 5.6.5508.33899-->
<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="test">
<gameid>5d46a2e6-530e-40a3-b929-1f4f2ecbdb97</gameid>
<version>1.0</version>
<firstpublished>2015</firstpublished>
<start type="script">
d = NewStringDictionary()
dictionary add (d, "key1", "value1")
dictionary add (d, "key2", "value2")
dictionary add (d, "key3", "value3")
msg (StringDictionaryItem(d, "key1"))
dictionary remove (d, "key1")
dictionary add (d, "key1", "new value1")
msg (StringDictionaryItem(d, "key1"))
DictionaryAdd (d, "key2", "new value2")
msg (StringDictionaryItem(d, "key2"))
DictionaryAdd (d, "key4", "new value4")
msg (StringDictionaryItem(d, "key4"))
</start>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
</object>
<function name="DictionaryAdd" parameters="dict, key, value">
if (DictionaryContains(dict, key)) {
dictionary remove (dict, key)
}
dictionary add (dict, key, value)
</function>
</asl>

TinFoilMkIV
So I need to remove the existing entry and create a new one with the same key to update it then? That is if I'm reading this correctly.

HegemonKhan
You could test it yourself, see if it works without removing the dictionary entry (if you can over-write it directly), and if it doesn't, then you know you need to thus remove the dictionary entry first (as you can't over-write it directly).

Most Attributes can be directly over-writen (the 'name' String Attribute is the 'ID' Attribute with quest), but I myself am not sure with the Dictionary+List attributes, if they can be directly over-written or not (needing to remove the entry, and then re-add the entry with its new Value).

direct over-writing with the other types of attributes:

player.condition_string = "normal"
~ to (you got poisoned) ~
player.condition_string = "poisoned"

orc.dead_boolean = false
~ to (you killed the orc) ~
orc.dead_boolean = true

player.current_life_integer = 100
player.maximum_life_integer = 999
~ to (you drink a full life recovery potion) ~
player.current_life_integer = player.maximum_life_integer
// player.current_life_integer = 999

The Pixie
TinFoilMkIV wrote:So I need to remove the existing entry and create a new one with the same key to update it then? That is if I'm reading this correctly.

Yes.

TinFoilMkIV
Yep, you get the error "Error adding key 'test' to dictionary: An item with the same key has already been added." if you try to add without removing the entry before replacing it.

So question answered.

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

Support

Forums