- Home /
Ways to optimize this code? iterate through inventory
Ok, so the way I have it set up is that in this game you have to craft your own guns by collecting parts throughout the round and then going to the crafting bench and crafting the weapons. Anyway, trying to keep it as simple as possible because there is no need for a real "inventory" I just made the inventory a list of strings really. Here I want to be able to have one variable part of the craft process which is the power source. In the UI you click the power source, which brings up a list of possible power sources, which you can click one of. This then changes the UI text of the original screen from "no source selected" to whatever you chose. Then if that is changed, the light behind the text lights up. if all the lights have an opacity of 1 then the condition is met to craft. Here's the code, but the problem is that it's so clunky it only works to light up the light behind the power source every dozen times or so due to processing requirements (i think that's what it is, because it does ocassioanlly work). Here's the part of the code that checks if the text has changed which is really not very efficient I think. I left out some of the lines above but all the component references are solid.
function Update ()
{
var sourceText : Text= powerSource.GetComponent.<Text>();
var theText : String = sourceText.text;
if (theText != noSourceText)
{
checkInventoryCores();
}
}
function checkInventoryCores()
{
var sourceText : Text= powerSource.GetComponent.<Text>();
var theText : String = sourceText.text;
var Bag = inventory.itemBag;
var psLightA : float = powerSourceLight.GetComponent.<Image>().color.a;
for (var j = 0; j<Bag.length; j++)
{
if (Bag[j] != null)
{
if (Bag[j] == "Luminescent Crystal" && theText == "Luminescent Crystal")
{
psLightA = 1;
} else if (Bag[j] == "Charged Cobalt" && theText == "Charged Cobalt")
{
psLightA = 1;
} else if (Bag[j] == "Hyperionized Plasma" && theText == "Charged Cobalt")
{
psLightA = 1;
} else if (Bag[j] == "Molten Core" && theText == "Molten Core")
{
psLightA = 1;
} else if (Bag[j] == "Radium Shard" && theText == "Radium Shard")
{
psLightA = 1;
} else if (Bag[j]=="Unidentifiable Spheroid" && theText == "Unidentifiable Spheroid")
{
psLightA = 1;
} else {
psLightA = 0;
}
}
}
}
I'm not really understanding exactly what's going on based on your description. Can you edit your question and add some additional info? $$anonymous$$aybe a couple of screenshots including one where it's working as expected?
That said, this doesn't look like it would have any huge performance impact, unless the inventory is really, really huge or something. Even with the GetComponent calls in Update (those should generally be saved in variables in Start so you don't have to call GetComponent each frame)
Thanks for the feedback, because even though I didn't really have the question phrased correctly, you saying that this it probably wasn't the performance made me go back to check my script, and I realized that the way it iterates, unless the last inventory item is the power source, that last else line will always set the light behind it to off. That's why it was only working every so often because I picked up the items before running to the crafting table in a different order every time. So, indirectly you solved it, so thanks :)