UnAlpha
UnAlpha

Reputation: 124

How to refer to existing variable(s) without creating a new one?

I have no idea how to ask this question.

I have a variable

public static var MaxDurabilityTestItem:Number = 3;

I have a function

    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");
        }
    }

The only part of this function that is causing me headaches is this line

UI_Taskbar_Inventory.InventoryDurability[Slot] = "MaxDurability" + Item

It outputs

Before change 0,0,0,0,0,0,0,0

After change 0,MaxDurabilityTestItem,0,0,0,0,0,0

While I want it to output

Before change 0,0,0,0,0,0,0,0

After change 0,3,0,0,0,0,0,0

I know the issue, however, I don't know how to fix it. "MaxDurability" + Item makes a string called MaxDurabilityTestItem, rather than referring to my variable MaxDurabilityTestItem.

How can I change this so that it refers to my variable MaxDurabilityTestItem, rather than this string it creates?

Upvotes: 0

Views: 115

Answers (2)

Organis
Organis

Reputation: 7316

The first thing I need to say here, is, despite all described below works just fine, that resorting to such techniques might indicate some deep problems with the project's architecture. Well, then...

The beauty and ugliness of ActionScript 3 is that literally anything is an object in it, and you can approach any instance as such.

Any class instance is an object. You can address any DisplayObject's x and y as ['x'] and ['y'] respectively. You can approach methods in the same way:

function gotoAnd(frame:*, thenPlay:Boolean):void
{
    // Forms 'gotoAndPlay' or 'gotoAndStop' string.
    var methodName = 'gotoAnd' + (thenPlay? 'Play': 'Stop');

    // Gets method reference to either gotoAndPlay or to gotoAndStop.
    var methodItself:Function = this[methodName];

    // Calls method by the reference.
    methodItself(frame);

    // In one line:
    // this['gotoAnd' + (thenPlay? 'Play': 'Stop')](frame);
}

Any class is an object too. The subtle difference is that the members of a class as an object are static class methods and fields. For example:

import flash.system.System;

// Accepts "free" or "private" or "total" as an argument.
function getMemory(value:String = "total"):Number
{
    var propertyName:String;

    switch (value)
    {
        case "private":
            propertyName = "privateMemory";
            break;

        case "free":
            propertyName = "freeMemory";
            break;

        case "total":
            propertyName = "totalMemoryNumber";
            break;

        // Returns -1 for an invalid argument.
        default:
            return -1;
            break;
    }

    // Returns either System.privateMemory
    // or System.freeMemory or System.totalFreeMemory.
    return System[propertyName];
}

Then again, static class methods are Function members of a class as an object:

// Direct method access.
System.gc();

// Property-based method access.
System['gc']();

Keep in mind, that [] access still respects the private, internal and protected namespaces, so if you cannot access SomeObject.someMethod() because the method is marked private, you won't be able to access it via SomeObject['someMethod'] either. The former will give you a compile-time error, the latter will let you compile your app then will make you deal with runtime exception.

Upvotes: 2

VC.One
VC.One

Reputation: 15881

"MaxDurability" + Item makes a string called MaxDurabilityTestItem,

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"); }

Upvotes: 1

Related Questions