I have a player inventory that is an array of strings. I also have another array of strings that lists the items sold in the store. I am trying to return an 'int' by comparing the player Inventory array against the Store array. For example, if the first index of the player array has a value of "Sword", and sword is located in index 3 of the store array, I want to return 3. I did a web search and found the FindIndex and read about it, but I am not grasping it I guess. https://msdn.microsoft.com/en-us/library/03y7c6xy(v=vs.110).aspx Code (CSharp): for(int i = 0 ; i < rInvArray.Length ; i++){ int num = Array.FindIndex(namesOfInventoryArray, playersData.rArray[i]); print("Value is: " + num); } I am getting this error and I am not sure what the differences are between a string and System.predicate string is? Thanks
I wrote this up to be sure I got the syntax correct Pasted from Rextester: Code (csharp): string [] store = { "Bottle", "Gun", "Sword" }; string [] inv = { "Gun" , "Sword", "Bottle", "Nope!" }; for(int i = 0; i < inv.Length; ++i) { int c = Array.FindIndex(store, w => w.Equals(inv[i])); Console.WriteLine("for " + inv[i] + " c = " + c); } Edit: Sometimes I go to answer a post and miss other options lol You could change this line, for the same result: Code (csharp): int c = Array.IndexOf(store, inv[i]);
Thanks for the help. I started reading about Lambda today but I am having a tough time wrapping my head around it. So in the first response you had. Code (CSharp): int c = Array.FindIndex(store, w => w.Equals(inv[i])); .FindIndex() takes 2 arguments it seems. The first being the Array I want to search, in this case the store. The second argument is the array that I want to iterate through to find the match, if it exists. In this case it in the 'inv' Array. I also get the .Equals because you are comparing the left to the right for a match. But what I am not getting is the 'w => w'. Since it is w.Equals() I am guessing that the w represents the store array in some way? So every time the for loop increments it also increments the players inventory by one. The that gets checked against all of the indexes or the store, some how? Thanks again, trying to wrap my head around the Lambda.
Wait, hang on. Your edited code is pretty much what I have already IndexOf(storeArray, playerArray); up above and that is what was giving me the error string System.string error? Just a hunch, I am reading from a saved file. I serialized to save but on loading the data I deserialze it. Could there be any System string formatting that is causing the problem since it is giving me an error comparing strings?
What you have above, where? Apologies if I'm missing something you wrote...Not sure about the formatting issue; first I'd check/confirm we had the same thing
I believe the 'w' is each entry in the store array. Using that variable, it does the comparison. If you're interested, search for simple lambda examples to get the idea. (I did that a number of times lol).
Kudos to methos for suggesting something as elegant as lambda expressions! But that might be a little complicated. A simpler solution to your original question is just to do a simple if then query in your for loop: Code (csharp): int ReturnMatchingStoreIndex (string stringToMatch) { for (int i = 0; i < rInvArray.Length ; i++) { // if you get a return val of -1, that means the object wasn't found. int returnVal = -1; if (rInvArry[i] == stringToMatch) { returnVal = i; break; } return (returnVal); } } Then just iterate through your player inventory array sending every string in the array to ReturnMatchingStoreIndex. I might add that arrays of strings is sorta cumbersome way to approach inventory. Maybe consider an enum of weapon types that you then put into a master array. Or better yet, write yourself a base weapons class that has a built in indexNumber. Then your array for both the player inventory and the store would be arrays of type weapon. You can then access the inventory number whenever you like giving you a lot more flexibility instead of having to parse strings continuously. Lots of ways to skin the cat, though. Good luck!
It's always good to have options. I do agree that strings aren't the best, but each project is unique.. and sometimes 1 problem at a time heh.
Thanks guys @methos5k & @sebastiansgames I got it working just fine tonight. This was just a small part of a larger thing I was trying to accomplish. I am working on Saving and Loading the game right now. My player inventory aren't strings, they are actually Buttons with images. User clicks the inventory button and the objects are created. I included an image of the purchased player inventory as an example. The problem I had was in saving. In Objective C I could save reference objects in an NSData class and save that to the hard drive and restore NSData to NSButton again. I ran in to trouble trying to save reference types , I think you call it, so I basically got the array of button objects and then got their transform.name. From that point I just added the name strings to an array which was savable to the hard drive. Then I just reversed that process to repopulate the player inventory. I got it working tonight with your guys help. Thanks for the assist!
No offense, but how can you call such a mess that won't even compile "a simpler solution" and this Code (CSharp): int ReturnMatchingStoreIndex(string stringToMatch) { return Array.FindIndex(rInvArray, i => i == stringToMatch); } you call "a little complicated". Even if I would write your code correctly Code (CSharp): int ReturnMatchingStoreIndex(string stringToMatch) { for (var i = 0; i < rInvArray.Length; i++) if (rInvArray[i] == stringToMatch) return i; return -1; } even then is the lambda solution far more readable than for loop.