Expression to Dictionary issues

Hey, guys,

I'm having more trouble. I promise for every problem I drop here, I struggle through and solve 6 -on average- on my own, so I'm really making an effort and not just begging for help the first second I hit a wall.

My problem now is that I have an object that I'm having display lists of items “purchased” in the game. That's easy, and done. Problem is, the player might want to sell those objects back, and thus I need to set a cost for the Strings put into the Stringlist in game. I've decided to do that by creating a StringDictionary (game.purchases), and adding the Strings and associated costs there. However, when I do that:

Error running script: Error adding key '40mm-S, High Explosive [5]' to dictionary: Unable to cast object of type 'System.Int32' to type 'TextAdventures.Quest.Scripts.IScript'. 


The code I'm using is:

  list add (this.purchase, this.listalias + ", " + this.round + " [" + result + "]")
  <b>dictionary add (game.purchases, this.listalias + ", " + this.round + " [" + result + "]", (this.cost*ToInt(result)))</b>
  player.grace = player.grace-(this.cost*ToInt(result))
  msg (result + " rounds of " + this.listalias + ", " + this.round + " ammunition purchased for " + (this.cost*ToInt(result)) + "G.  " + player.grace + "G left.")


So, I want the specific caliber (this.listalias) + the type of ammo (this.round) + the number of rounds purchased (result) added to the dictionary and then associated with the actual item cost (this.cost*ToInt(result)).

All of this works great, except the dictionary part. Then I get the error. Do I have to convert this expression to a String, and if so, how?

Thanks again for the help, guys,

Unadept

Based on the error message, it looks like you have created game.purchases as a ScriptDictionary. But if you want to add the costs as integers, then just use a simple Dictionary. That can take values of any type (and you can even have different types within the same dictionary). The typed dictionaries (e.g. ScriptDictionary, StringDictionary, etc) can *only* take values of the type they hold, which is why you're getting the error.

If you do want to use a StringDictionary, then you'd need to convert the purchase total to a string before adding it to the dictionary.

totalcost = ToString(this.cost*ToInt(result)))

Fantastic, thanks jaynabonne, that makes sense.

Worked perfectly, thanks again.