Hello, can somebody please point out my mistake?

I just tried writing my first ash script. While it is very similar to C++, I cannot figure out what I have done wrong.

Code:
string[12] StarterEquipment;
StarterEquipment[0] = "seal-skull helmet";
StarterEquipment[1] = "seal-clubbing club";
StarterEquipment[2] = "helmet turtle";
StarterEquipment[3] = "turtle totem";
StarterEquipment[4] = "pasta spoon";
StarterEquipment[5] = "ravioli hat";
StarterEquipment[6] = "saucepan";
StarterEquipment[7] = "Hollandaise helmet";
StarterEquipment[8] = "disco mask";
StarterEquipment[9] = "disco ball";
StarterEquipment[10] = "mariachi pants";
StarterEquipment[11] = "stolen accordion";

int AutosellStarterEquipment(string EquipmentName)
{
	if(item_amount($item[EquipmentName]) > 0)
	{
		print("Autoselling " + item_amount($item[EquipmentName]) + " " + EquipmentName + " for " + (item_amount($item[EquipmentName]) * 10) + " meat.");
		int MeatGained = (item_amount($item[EquipmentName]) * 10);
		cli_execute("autosell " + (item_amount($item[EquipmentName])) + " " + EquipmentName);
		return MeatGained;
	}
	else
	{ return 0; }
}


void main()
{
  ## Sell Starter Items in Inventory
		int TotalAmount = 0;
		foreach EquipmentItem in StarterEquipment
		{
			TotalAmount += AutosellStarterEquipment(EquipmentItem);
		}		
		print("Total starter equipment autosell meat gained: " + TotalAmount);

  ## Auto reprice the mall stuff that is at 999999999.
  cli_execute("undercut"); 
}
 

slyz

Developer
This ASH Errors wiki page can probably help you.

The problem I see in what you posted is this:
PHP:
string EquipmentName;
(...)
$item[EquipmentName]
If you want to make a $item variable from the EquipmentName string, you have to use the to_item() function. Simply replace $item[EquipmentName] by to_item(EquipmentName).
 
Thanks for your help slyz. I've finally got it running the way I had intended. I had to replace the foreach loop because it was somehow passing a number for the argument instead of the string in the array. For anyone interested, here is the working version:
Code:
string[12] StarterEquipment;
StarterEquipment[0] = "seal-skull helmet";
StarterEquipment[1] = "seal-clubbing club";
StarterEquipment[2] = "helmet turtle";
StarterEquipment[3] = "turtle totem";
StarterEquipment[4] = "pasta spoon";
StarterEquipment[5] = "ravioli hat";
StarterEquipment[6] = "saucepan";
StarterEquipment[7] = "Hollandaise helmet";
StarterEquipment[8] = "disco mask";
StarterEquipment[9] = "disco ball";
StarterEquipment[10] = "mariachi pants";
StarterEquipment[11] = "stolen accordion";

int AutosellStarterEquipment(string EquipmentName)
{
	if(item_amount(to_item(EquipmentName)) > 0)
	{
		print("Autoselling " + item_amount(to_item(EquipmentName)) + " " + EquipmentName + " for " + (item_amount(to_item(EquipmentName)) * 10) + " meat.");
		int MeatGained = (item_amount(to_item(EquipmentName)) * 10);
		cli_execute("autosell " + (item_amount(to_item(EquipmentName))) + " " + EquipmentName);
		return MeatGained;
	}
	return 0;
}


void main()
{
  ## Sell Starter Items in Inventory
		int TotalAmount = 0;
		for i from 0 to 11 by 1
		{ TotalAmount += AutosellStarterEquipment(StarterEquipment[i]); }		
		print("Total starter equipment autosell meat gained: " + TotalAmount);

  ## Auto reprice the mall stuff that is at 999999999.
  cli_execute("undercut"); 
}
 

slyz

Developer
I had to replace the foreach loop because it was somehow passing a number for the argument instead of the string in the array.
You have a map:
PHP:
StarterEquipment[ key ] = value
where key is an int, and value is a string.

When you use foreach, you go through the keys:
PHP:
foreach key in StarterEquipment

You can also, however, capture the value:
PHP:
foreach key, value in StarterEquipment
     AutosellStarterEquipment(value);
Or, using your variable names:
PHP:
foreach i, EquipmentItem in StarterEquipment
     AutosellStarterEquipment(EquipmentItem);
 
Last edited:
Excellent debugging slyz. Ok, revamped again:

Code:
string[12] StarterEquipment;
StarterEquipment[0] = "seal-skull helmet";
StarterEquipment[1] = "seal-clubbing club";
StarterEquipment[2] = "helmet turtle";
StarterEquipment[3] = "turtle totem";
StarterEquipment[4] = "pasta spoon";
StarterEquipment[5] = "ravioli hat";
StarterEquipment[6] = "saucepan";
StarterEquipment[7] = "Hollandaise helmet";
StarterEquipment[8] = "disco mask";
StarterEquipment[9] = "disco ball";
StarterEquipment[10] = "mariachi pants";
StarterEquipment[11] = "stolen accordion";

int AutosellStarterEquipment(string EquipmentName)
{
	print(EquipmentName + ": " + item_amount(to_item(EquipmentName)));
	if(item_amount(to_item(EquipmentName)) > 0)
	{
		print("Autoselling " + item_amount(to_item(EquipmentName)) + " " + EquipmentName + " for " + (item_amount(to_item(EquipmentName)) * 10) + " meat.");
		int MeatGained = (item_amount(to_item(EquipmentName)) * 10);
		cli_execute("autosell " + (item_amount(to_item(EquipmentName))) + " " + EquipmentName);
		return MeatGained;
	}
	return 0;
}


void main()
{
  ## Sell Starter Items in Inventory
		int TotalAmount = 0;
		foreach index, EquipItem in StarterEquipment
		{ TotalAmount += AutosellStarterEquipment(EquipItem); }		
		print("Total starter equipment autosell meat gained: " + TotalAmount);

  ## Auto reprice the mall stuff that is at 999999999.
  cli_execute("undercut"); 
}
 

fronobulax

Developer
Staff member
If you want to make a $item variable from the EquipmentName string, you have to use the to_item() function. Simply replace $item[EquipmentName] by to_item(EquipmentName).

This has nailed me more often than I care to admit. $item is essentially a macro expansion that is evaluated at compile time. As such the "argument" must be a constant. to_item is the run time equivalent.
 
Excellent feedback from you as well fronobulax. While my script has proven mostly effective, I have hit a snag. When autoselling a saucepan, it does nothing. Even using KOLMafia's CLI produces the same result. So this leaves me to resolve that it is a KOLMafia bug, and not a bug in my code. Additionally, when importing zlib.ash, it gives me the error:
Code:
Function 'my_path( )' undefined. This script may require a more recent version of KoLmafia and/or its supporting scripts. (zlib.ash, line 271)

Any thoughts?
 
I figured it's probably needing an update, but how do I go about this? Do I download a new .exe and run it from my current installation location? And if I do, will it overwrite my current settings for HP restores, etc...?
 

lostcalpolydude

Developer
Staff member
The exe file there is out of date, which is why I said jar in my post (the .jar should work exactly the same as a .exe). The process is identical to what you did when you got 14.6, which didn't override any of your settings.
 
Top