I have no idea how to ask this question.
I have a variable
public static var MaxDurabilityTestItem:Number = 3;
public static function setItemInSlot(Item:String, Slot:Number, MaxDurability:Number = 0)
{
UI_Taskbar_Inventory.InventoryItems[Slot] = Item;
if(MaxDurability == 0)
{
trace("Before change " + UI_Taskbar_Inventory.InventoryDurability);
UI_Taskbar_Inventory.InventoryDurability[Slot] = "MaxDurability" + Item;
trace("After change " + UI_Taskbar_Inventory.InventoryDurability);
}
else
{
trace("not using default durability");
}
}
UI_Taskbar_Inventory.InventoryDurability[Slot] = "MaxDurability" + Item
Before change 0,0,0,0,0,0,0,0
After change 0,MaxDurabilityTestItem,0,0,0,0,0,0
Before change 0,0,0,0,0,0,0,0
After change 0,3,0,0,0,0,0,0
"MaxDurability" + Item
MaxDurabilityTestItem
MaxDurabilityTestItem
"MaxDurability" + Item
makes a string calledMaxDurabilityTestItem
,
Because you automatically defined a "string"
by using the quotes. I can only assume Item
is also string with text "TestItem"
. So you've simply joined+two+strings together.
(2)
...rather than referring to my variable
MaxDurabilityTestItem
.
Try as:
UI_Taskbar_Inventory.InventoryDurability[Slot] = MaxDurabilityTestItem; //now using your defined variable
Edit :
Just in case you really want to use a string as reference to the variable itself :
Use this[ "name of some var" ]
... where this
will target the current class and ["name"]
will find such specified variable within the current class.
Try:
if(MaxDurability == 0)
{
trace("Before change " + UI_Taskbar_Inventory.InventoryDurability);
UI_Taskbar_Inventory.InventoryDurability[Slot] = this[ "MaxDurability" + Item ];
trace("After change " + UI_Taskbar_Inventory.InventoryDurability);
}
else
{ trace("not using default durability"); }