dictionarys of integers?
shadowphile
25 Jun 2013, 07:48Can I create a dictionary that returns an integer?
I'm trying to use a key value and get an integer back.
Instead, for now, I created a script dictionary with each script just 'return (n)' where n varies for each key.
I tried ScriptDictionaryItem(game.mydictionary, "tomato") but "tomato" or tomato without the quotes causes an error, even though it's one of the keys I've defined.
And if it did work, what command do I use to run a script?
Such a simple task in other languages...
I'm trying to use a key value and get an integer back.
Instead, for now, I created a script dictionary with each script just 'return (n)' where n varies for each key.
I tried ScriptDictionaryItem(game.mydictionary, "tomato") but "tomato" or tomato without the quotes causes an error, even though it's one of the keys I've defined.
And if it did work, what command do I use to run a script?
Such a simple task in other languages...

Alex
25 Jun 2013, 09:02Yes, the dictionary type lets you store any kind of value: http://quest5.net/wiki/Dictionary
Before Quest 5.4 there were only stringdictionary and scriptdictionary types, but you can use the new dictionary type to store integers.
There's no attribute editor for it yet, so you have to either create it using the code view, or use a script to populate it.
Before Quest 5.4 there were only stringdictionary and scriptdictionary types, but you can use the new dictionary type to store integers.
There's no attribute editor for it yet, so you have to either create it using the code view, or use a script to populate it.
Liam315
25 Jun 2013, 20:47Another workaround is the ToInt function which would allow you to store the numbers in a string dictionary, then convert them to integers when you need to call on them.
http://quest5.net/wiki/ToInt
e.g. ToInt(StringDictionaryItem(dictionaryName,Key))
http://quest5.net/wiki/ToInt
e.g. ToInt(StringDictionaryItem(dictionaryName,Key))
HegemonKhan
26 Jun 2013, 01:40and to call it, I think it's "invoke", so:
invoke (ToInt(StringDictionaryItem(dictionaryName,Key)))
err... maybe invoke only works for a scriptdictionary, you can figure it out though, as I'm too lazy to do so myself, lol.
invoke (ToInt(StringDictionaryItem(dictionaryName,Key)))
err... maybe invoke only works for a scriptdictionary, you can figure it out though, as I'm too lazy to do so myself, lol.
shadowphile
26 Jun 2013, 04:04ToInt requires an object and attribute and wont' take a string! Is there a basic string to number function? Can't find one listed in the wiki, might be unlisted function? (ie IsDefined)

Pertex
26 Jun 2013, 06:42shadowphile wrote:ToInt requires an object and attribute and wont' take a string!
Thats not true. ToInt is the right function
shadowphile
26 Jun 2013, 23:46Sorry, got GetInt and ToInt confused.
ToInt still doesn't work.
Here's the script and output.
thanks
ToInt still doesn't work.
Here's the script and output.
result = ToInt("three")
msg (result)
You are in a room.
Error running script: Error evaluating expression 'ToInt("three")': Input string was not in a correct format.
thanks

jaynabonne
27 Jun 2013, 00:35This is how it's used:
The string must represent the numeric form of a number, but as a string. You would need to parse the string into a number form using language-specific rules if you wanted to handle things like "three", "twenty-five" or "one hundred."
result = ToInt("3")
msg (result)
The string must represent the numeric form of a number, but as a string. You would need to parse the string into a number form using language-specific rules if you wanted to handle things like "three", "twenty-five" or "one hundred."
HegemonKhan
27 Jun 2013, 05:03I wasn't sure myself if the " ToInt " could convert the textual string VALUE of "three" into the numerical " 3 ", so Jaynebonne already beat me to it.
now that I know myself, thanks to Jaynebonne's post, let me just echo what he said:
unfortunately, the converting command functions (or whatever they're actually called, meh), such as the " ToInt ", only convert the TYPE of the attribute, NOT the abc~word characters VALUE itself (like "three") of a string attribute into a numerical VALUE (like "3").
if (STRING type attribute = INTEGER type attribute) { script } -> error, as a STRING is not an INTEGER
if (STRING type attribute = STRING type attribute) { script } -> no error
if (INTEGER type attribute = INTEGER type attribute) { script } -> no error
if (ToInt (STRING type attribute = INTEGER type attribute)) { script } -> no error
if (INTEGER type attribute = INTEGER type attribute) { script } -> no error
An Attribute creation and its own attributes:
Name:
Type:
Value:
some examples:
Name: strength
Type: int (integer)
Value: 50
Name: attack
Type: script (making a verb; or a command though its not via an attribute creation)
Value: (your direct script block or a call function to a function's script block, I don't want to write out a script block for attack)
Name: health_status
Type: string
Value: wounded
Name: physical_damage
Type: double
Value: 0.0
Name: dead
Type: boolean
Value: false
-------
the problem lies in~with the scripting, often when you use an attribute, the script (such as the "if (object.attribute = ...) " returns it as a string type, while on the other side of the equals is an integer type or return integer type, so often you need to add the " ToInt " to get the left side of the equals into an INTEGER type too.
also, the problem lies in that:
the character value of a numeric, such as "0" or "100", (is ~ can be) both a string and an integer, so quest needs to know whether, that "5" VALUE is the STRING TYPE or the INTEGER TYPE, as it needs to know this, as that's how its logic works, by comparing an attribute type to another attribute type, if they're not the same type such as when they're suppose to be, like in the case with an equal sign, then quest is unable to do it, displaying an error, as to its logic, STRING ("Y") TYPE NOT EQUAL TO INT ("X") TYPE, when you specified for it that it needs to be "X" TYPE = "X" TYPE or "Y" TYPE = "Y" TYPE.
quest's engine's logic basically works like this:
your scripting:
quest then checks your scripting like this (it automatically does the additional check of the attributes' TYPES for you):
so, this is why you may have to manually add in the "converters" (such as ToInt), to your own script, to get quest to be able to pass its own automatic check of the TYPES of your attributes.
----------------------------------
Attributes
Name: strength
Type: string (sorry for doing this as my stupid example, as I don't know of nor can find an actual example of where you need to plug in the ToInt, grr)
Value: 100
Name: endurance
Type: int
Value: 100
so, basically:
if (HK.strength = 99) { HK.physical_damage=49.5 } -> NO error, as quest can recognize the " 100 " as an INT type to make up with the " strength " INTEGER type's value (ie the characters: "100")
however, if you got:
if (HK.strength = orc.endurance) { msg ("HK does zero damage to the orc") } -> error, as STRING TYPE is not equal to INTEGER TYPE (as can be seen, quest doesn't care that both of the VALUES are numerical)
if (ToInt (HK.strength) = orc.endurance) { msg ("HK does zero damage to the orc") } -> no error
now that I know myself, thanks to Jaynebonne's post, let me just echo what he said:
unfortunately, the converting command functions (or whatever they're actually called, meh), such as the " ToInt ", only convert the TYPE of the attribute, NOT the abc~word characters VALUE itself (like "three") of a string attribute into a numerical VALUE (like "3").
if (STRING type attribute = INTEGER type attribute) { script } -> error, as a STRING is not an INTEGER
if (STRING type attribute = STRING type attribute) { script } -> no error
if (INTEGER type attribute = INTEGER type attribute) { script } -> no error
if (ToInt (STRING type attribute = INTEGER type attribute)) { script } -> no error
if (INTEGER type attribute = INTEGER type attribute) { script } -> no error
An Attribute creation and its own attributes:
Name:
Type:
Value:
some examples:
Name: strength
Type: int (integer)
Value: 50
Name: attack
Type: script (making a verb; or a command though its not via an attribute creation)
Value: (your direct script block or a call function to a function's script block, I don't want to write out a script block for attack)
Name: health_status
Type: string
Value: wounded
Name: physical_damage
Type: double
Value: 0.0
Name: dead
Type: boolean
Value: false
-------
the problem lies in~with the scripting, often when you use an attribute, the script (such as the "if (object.attribute = ...) " returns it as a string type, while on the other side of the equals is an integer type or return integer type, so often you need to add the " ToInt " to get the left side of the equals into an INTEGER type too.
also, the problem lies in that:
the character value of a numeric, such as "0" or "100", (is ~ can be) both a string and an integer, so quest needs to know whether, that "5" VALUE is the STRING TYPE or the INTEGER TYPE, as it needs to know this, as that's how its logic works, by comparing an attribute type to another attribute type, if they're not the same type such as when they're suppose to be, like in the case with an equal sign, then quest is unable to do it, displaying an error, as to its logic, STRING ("Y") TYPE NOT EQUAL TO INT ("X") TYPE, when you specified for it that it needs to be "X" TYPE = "X" TYPE or "Y" TYPE = "Y" TYPE.
quest's engine's logic basically works like this:
your scripting:
if (object_1.attribute = object_2.attribute) {
do_this_script_1
} else {
do_this_script_2
}
quest then checks your scripting like this (it automatically does the additional check of the attributes' TYPES for you):
if (object_1.attribute.type = X and object_2.attribute.type = X) {
if (object_2.attribute.value = Y and object_2.attribute.value = Y) {
(then, do_this_script_1)
} else {
(then, do_this_script_2)
}
} else {
ERROR, ~ "quest doesn't recognize function: expression type STRING_32 and expression type INT_32"
}
so, this is why you may have to manually add in the "converters" (such as ToInt), to your own script, to get quest to be able to pass its own automatic check of the TYPES of your attributes.
----------------------------------
Attributes
Name: strength
Type: string (sorry for doing this as my stupid example, as I don't know of nor can find an actual example of where you need to plug in the ToInt, grr)
Value: 100
Name: endurance
Type: int
Value: 100
so, basically:
if (HK.strength = 99) { HK.physical_damage=49.5 } -> NO error, as quest can recognize the " 100 " as an INT type to make up with the " strength " INTEGER type's value (ie the characters: "100")
however, if you got:
if (HK.strength = orc.endurance) { msg ("HK does zero damage to the orc") } -> error, as STRING TYPE is not equal to INTEGER TYPE (as can be seen, quest doesn't care that both of the VALUES are numerical)
if (ToInt (HK.strength) = orc.endurance) { msg ("HK does zero damage to the orc") } -> no error