- Home /
Getting a result of an if statement from a list.
So, i have a list of items like so
var itemList = new Item[40];
class Item {
var itemName : String;
var PrereqItem1 : String;
var PrereqItem2 : String;
var sellPrice : int;
var weight : float;
var quantityInInventory : int;
}
And in a seperate file i have a function which, on fired, i would like to return the value of an item based off the two items the player has selected. So far the function looks like this:
function SearchList(){
for (var i : int = 0; i < Inv.itemList.length; i++) {
if (item1 == Inv.itemList.PrereqItem1 && item2 == Inv.itemList.PrereqItem2){
Result = Inv.itemList.itemName;
}
}
}
So, as an example, the player might select a fish (item 1) and a pizza (item 2), and click the button. The code will then search the list for an item that has both the prerequisites of pizza and fish, then return the name of "fish pizza".
My issue is that i cannot work out how to word the if statement so it...well...works. I suspect the answer is simple, but i'm currently staring at the wood and wondering where the trees are.
Any help you guys can give will be much appreciated.
Answer by robertbu · Nov 14, 2014 at 09:30 PM
The typical solution would be to return null if something is not found:
function SearchList() : String {
for (var i : int = 0; i < Inv.itemList.length; i++) {
if (item1 == Inv.itemList[i].PrereqItem1 && item2 == Inv.itemList[i].PrereqItem2){
return Inv.itemList[i].itemName;
}
}
return null;
}
You can then check for null as the return value to see if the function succeeded or failed.
Ok,so the errors i'm getting now is this: Assets/Assets/Scripts/Player Systems/Crafting.js(80,27): BCE0019: 'PrereqItem1' is not a member of 'Item[]'. One for item name and prereq2 as well...
You cannot access non-static variables in another file like you are attempting unless they are static. Research static variables, GetComponent(), and Singletons:
https://docs.unity3d.com/352/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://wiki.unity3d.com/index.php?title=Singleton
Also you may want to reconsider how you are structuring things. It seems to be that the SearchList() function belongs with the Item class (though you would still have to figure out how to access this method from another file).
Gah, ok, ill have a plot and see what i can come up with. Thanks for the help though!
Wait...so if i combined the two files, theoreticlly it should work fine?
If you combine the two files, then yes you can access the list. You will be accessing 'itemList', not 'Inv.itemList'.
@darkironphoenix: You missed the index on your list when you access an element inside the list. I've fixed the answer. He just copied your faulty code and hasn't spotted that one ;)
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do you know if a value is a list? 0 Answers
Increment based 'if' conditions not being met for the later conditions. Any solutions ? 0 Answers
If String is in List Then... 2 Answers
C# ArrayList match to string? 1 Answer