Change Value of Dictionary Key? [SOLVED]

Me again. How do you change the value of a dictionary key (in this case, the first key), and without changing the order of the dictionary keys?

Because the order must be preserved, I can't use "remove value from dictionary" and then "add value to dictionary", as the key will then be placed at the end. I also can't add the same key but with a new value, because you can't add a key that already exists. I tried changing the value of the key with "set variable", but I don't think Quest recognizes a dictionary key as a variable. I've run out of ideas...


I don't think you can alter the ordering of a dictionaries items other than copying and rebuilding it every time it changes. Alternatively you could sort the results before doing anything else with them.

Can you explain why you specifically need both a dictionary and a set order to the items it contains?


chuckles... Dcoder wants/needs tree data management :D (or beyond: dictionaries, maps, etc...)

https://en.wikipedia.org/wiki/Binary_search_tree
https://en.wikipedia.org/wiki/2–3_tree
https://en.wikipedia.org/wiki/2–3–4_tree

and/or maybe Dcoder, should create a heap, which he/she can then have functions for handling it (keeping the correct order/placement of items after adding/removing), which then would that correct item be used as the input value for the dictionary to get the resultant output.


I failed this class though, so I can't be of much help... my mind/brain hasn't yet learned to think in this structural and abstract way... sighs.


I need a way to update the player.statusattributes dictionary. The player can suffer from multiple conditions at any given time (Poisoned, Woozy, Cleansed, etc.). So I am using a different dictionary key and value for each condition (Poisoned key and Poisoned string value, Woozy key and Woozy string value, etc.). I just add and remove keys as needed.

I DON'T want a long list of text that says this:

Condition: Poisoned
Condition: Woozy
Condition: Cleansed

I DO want "Condition: " to be displayed just once, with each following condition displayed in a vertical list, and DIRECTLY BENEATH the first condition:

Condition: Poisoned
-------------> Woozy // the arrows should not be there (Forum won't post multiple blank spaces)
-------------> Cleansed

So far, I can do all of this by using Pixie's "WhiteSpaces" function to print a bunch of blank spaces followed by the word "Woozy" (the Woozy value code is this expression: BlankSpaces(17) + "!").

The problem arises when the first condition (Poisoned) goes away, and the next condition (Woozy) goes up the list and is listed first. I need to change the Woozy value to "Condition: !" instead of BlankSpaces(17) + "!". Thus, how do I change a dictionary key value without upsetting the order? Actually, the order doesn't matter, so long as "Condition: " is displayed in the first key.

Aren't you glad you asked, haha?


One option, as you are already using Quest 5.7, would be a custom status pane (on the Interface tab). You would have to reset it manually when any attribute changes. Example code that puts the conditions in a table to align them:

JS.setCustomStatus("<table><tr><td width=\"50%\">Condition:</td><td>Poisoned</td></tr><tr><td></td><td>Woozy</td></tr></table>")

A knowledge of HTML and (to some degree) CSS would be useful here!

ETA: See here:
http://docs.textadventures.co.uk/quest/custom_panes


for just having the formating you desire, you can set your first condition to be a separate String Attribute:

<attr name="first_condition_string_attribute" type="string">unknown</attr>

<attr name="condition_stringlist_attribute" type="simplestringlist">unknown</attr>

<attr name="changedfirst_condition_string_attribute" type="script">
  this.first_condition_string_attribute = "Condition: " + this.first_condition_string_attribute
</attr>

<attr name="statusattributes" type="simplestringdictionary">first_condition_string_attribute = !; /* however you do the tab-margin formatting for all of your other conditions: poisoned_string_attribute = (spacing) poisoned; confused_string_attribute = (spacing) confused*/</attr>

hopefully, you get the idea... as I'm not sure how you're handling the statusattributes and the margin/tab formatting... beyond my understanding/ability in how you/pixie are doing it... lol


Perhaps a custom statusAttribute with a turn script to update it every turn?
Assuming that all the conditions that can affect the user are in a player attribute called "conditions" which is a dictionary, where the value is a string representation of an integer of the severity of the condition:

Try this:
Create a player attribute called "condstatus". Make it a status attribute whose value is just "!".

Create a function called "DisplayCondition" and fill it with

newtext = "Condition: "
if (DictionaryCount(player.conditions) = 0) {
  newtext = newtext + "Fine"
}
else {
  slist = NewStringList()
  foreach (entry, player.conditions) {
    list add (slist, entry)
  }
  item = ListItem(slist,0)
  newtext = newtext + " " + GetCondition( item ) + "<br/>"
  list remove (slist, item)
  while (ListCount(slist) > 0) {
    item = ListItem(slist,0)
    newtext = newtext + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + GetCondition(item ) + "<br/>"
    list remove (slist, item)
  }
}
player.condstatus = newtext
UpdateStatusAttributes

Then create a function called GetCondition and fill it with

l = player.conditions
if (not DictionaryContains(l,condname)) {
  return ("")
}
else {
  i = StringDictionaryItem(l,condname)
  return (condname + "(" + ToString(i) + ")")
}

Lastly, create a TurnScript called "CondStatusPrint" which just calls:-

CondStatusText

every turn. Now the turn script will pre-sort the list of conditions, generate the status attribute, and print it automatically.

Maybe that might help?


I'm in the process of getting my head around these suggestions.....


Ok, I came up with a simpler variation of DarkBlueMonkey's code that seems to work. This is a global turn script:

if (ListCount(player.MultiConditionList) = 0) {
  player.Condition = "Condition: Normal"
}
else {
  count = 0
  player.Condition = ""
  for (ConditionLoop, 1, ListCount(player.MultiConditionList), 1) {
    if (ConditionLoop = 1) {
      player.Condition = player.Condition + "Condition: " + ListItem(player.MultiConditionList, 0) + "<br/>"
    }
    else {
      player.Condition = player.Condition + BlankSpaces(17) + ListItem(player.MultiConditionList, count) + "<br/>"
    }
    count = count+1
  }
}
UpdateStatusAttributes

"player.Condition" is a key within "player.statusattributes", and displays the player's conditions. It is a string "built up" from "player.MultiConditionList", which is a string list hidden from the player that "holds" all of the current conditions (besides "Normal"), and is "fed" those conditions by actions within the game. BlankSpaces is Pixie's handy function for returning any # of blank spaces (found that somewhere in the forum under "WhiteSpaces").

Looks simple, but that took me all day to come up with (as well as editing everything else that got affected in my game). Thanks everyone, especially DarkBlueMonkey, for all the help. Back to bug-fixing and playtesting Q5.7!


unfortunately, that's how it always is... the code itself looks so simple (when it's done and you look at it, and it makes sense and is easy to understand)... but coming up with it and/or understanding it's needed workings (as you try to craft/create/come up with it) is another matter... sighs.


it's not much fun when you're a coding/programming noob and it takes a full day or worse (a week or a month or even a few months!) to come up with something (and often it's just a single simple piece/part of coding/functionality/feature, argh!) that works and/or understanding it... sighs... but this is the process of learning to program... getting experience...

compared to being a professional coder/programmer, and you can create full complex working programs/stuff with ease and quickness, sighs.


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

Support

Forums