Special type value--Veracity's tutorial

ThePowerTool

New member
"While the special-typed variables are declared the same as primitive types (ie: item i), special-typed static values are declared very differently from static values. To specify that a value is of a special type, $typename[value] must be used, with a string in square brackets designating the intended value (ie: $item[cog])."--Veracity's tutorial (Advanced.html).

I have a question regarding this and a sample script:

script "test-item-w-var.ash"

item test_i=$item[bottle of Pete's Sake];
print("test_i="+test_i);

string my_str="bottle of Pete's Sake";
# test_i=$item[my_str];
# print("test_i="+test_i);

Why does the 1st declaration work when simply uncommenting the 2nd declaration yields:
Bad item value: "my_str" (test-item-w-var.ash, line 7, char 14 to char 20)
 
$item[bottle of Pete's Sake] means the item named "bottle of Pete's Sake". $item[my_str] means the item named "my_str", but no such item exists. You can't use variable in the brackets like that. If you want to convert a string into an item, you have to use to_item(my_str) (or, equivalently, my_str.to_item() ).
 
"$typename[value] must be used, with a string in square brackets" is incorrect? There's no disambiguation? If I understand you, I think it should say "$typename[value] must be used, with a static value pointing to a <key expression>" (using Data Structures as a reference).

And does this also affect mall_prices()? The primary reason I created the OP was a desire to only enter static data once and use disambiguation for every subsequent reference. I tried using mall_prices(my_str) where my_str was a comma separated list. It works flawlessly with a static string but fails with errors when I use a string variable with the same comma-separated data.

Can I sneak around this limitation using a function e.g.:
function(string1,string2,string3,string4){}
and then call with
my_str="bottle of Pete's Sake, cog, spring,fortune cookie";
funtion(my_str);

Thank you for your help!
 
The thing in the brackets needs to be a string literal (but without the double-quotes), not a variable of string type.

A comma is just a normal character for a string to contain, and a single string containing a comma doesn't automatically get converted into multiple strings. You'd need something like split_string for that. But also, I don't think mall_prices is what you think it is. If you give it a single string, that's the name of a category, not an individual item. In order to update the price of multiple items, you have to pass it an items map. So you could say:
Code:
boolean[item] my_items = $items[bottle of Pete's Sake, cog, spring,fortune cookie];
mall_prices(my_items);

If a function has multiple parameters, you need to actual call it with multiple arguments. A string containing commas is just a string, so that's just one argument. An array or map would also just be one argument.
 
I was seeing that but not clear on what was happening (calling mall_prices but it appeared to be acting as if it was iterating as mall_price).

That explains so much. Now I have to play with it.

Thank you very much, again!!!
 
Back
Top