Hi,
I’m trying to clean up some code which is repeated 4 times but uses different variable and references names. Its all nice organised so that it should be easy enough to feed a variable name into a class and let it insert the variable into the relevant parts of the code.
However I can’t get round this:
Code which works:
var storageC = resourceHolder.h2oStorageCurrent;
Code which DOESN’T work:
var name = "h2o"; //this would actually be a variable defined during the call to the class, I just put it here to simplify the example
var storageC = resourceHolder[name + "StorageCurrent"];
Obviously there is more to my class than that, but as I can make it work with the 1st bit of code, it must be my understanding of javascript and it’s coding practices.
Hopefully you can see what I’m trying to do and suggest a solution.
Thanks
Rob
Extra Information
Apologies for not providing all the info you need to help. I am trying to pull a variable from another GO to use with the drawing of UI elements on this GO. My code works, but I have 4 blocks of very similar code I’m trying to clean up into a reusable class.
The error I get is:
“MissingFieldException: Field ‘HUBResources.’ not found.”
Here is my full class:
`
class statusPanel{
var name : String;
var offsetX : int;
var offsetY : int;
var resources : GameObject; //resources is passed a GO which has a script called HUBResources attached to it. This contains some int and float variables which affect the whole game, but in this instance are just being read to display them on a pda style system.
function statusPanel(name, offsetX, offsetY, resources, textHolder){
var filename = "pda_icon_"+name+"_green.png";
var resourceHolder = resources.GetComponent("HUBResources"); //Here I reference the script directly
var storageC = resourceHolder.h2oStorageCurrent; //This is the working non-dynamic version
var storageC2 = resourceHolder[name"+StorageCurrent]; //This is my attempt to make the same line dynamic so I can reuse the code for my other variables sets, oxygen, energy and food.
//The rest of the code uses UIToolkit to draw out various UI elements, all of which work fine.
var icon = UI.firstToolkit.addSprite(filename, offsetX + 80, offsetY + 120);
var bar = UIProgressBar.create("pda_misc_bar.png", "pda_misc_barHolder.png", 3, 3, 0, 0);
pos = bar.position;
pos.x = UIRelative.xPixelsFrom( UIxAnchor.Left, offsetX + 130 );
pos.y = -UIRelative.yPixelsFrom( UIyAnchor.Top, offsetY + 120 );
bar.position = pos;
bar.resizeTextureOnChange = true;
bar.value = storageC / resourceHolder.h2oStorageMax;
}
};`
I’m pretty sure the problem is just contained within the storageC2 line.
Thanks again.
Rob