- Home /
How to convert String array to int array
Hi friends, I'm asking this question after searching this forum for similar problems but they are not helpful for me. I am creating a card game. I've created string array of cards player has >
var playerCards : Array = ["playerCard1", "playerCard2", "playerCard3", "playerCard4", "playerCard5", "playerCard6", "playerCard7", "playerCard8", "playerCard9", "playerCard10"];
While playing game, the player will select one of these cards. I want that card to give/ display its index in array, so that I can compare it with another card and see whether they match or not like this >
if (playerCardID == enemyCardID)
{
Debug.Log ("CARDS MATCHED!!!");
}
I need indexes of playerCardID only, those of enemyCardID will be generated randomly. SO what is the solution for this one. can you help plz. Thanx
Answer by Berenger · May 19, 2012 at 02:21 PM
You can create a function that find the index of a string in the array by comparing it with each members. Might not be very optimized, not to the point to slow down your game but still. You could use a dictionary as well, but I suppose it would be the same.
You could create a class containing the name and the index, then do an array of that instead.
as a beginner I'm looking to complete this game. I will see about optimization later. BTW Didn't get the answer (I mean I am too noob to understand this stuff) but appreciate your effort. thanks
Well, what he's telling you, is to create a class, like a custom type of variable, wich can hold, both, the name of the card (in a string) and the index of the card (you can add more data as well).
Let's say you create the card class, and that class has the properties ID and name, ID being an integer, and name being a string, so then, you could create your array of "card type" ins$$anonymous$$d of strings, so then you could easily access to both the name or the index of any particular card just doing this:
playerCards[0].ID -> this holds the ID from the player's first card
playerCards[0].name -> this holds the string containing the player's first card name
Your answer
Follow this Question
Related Questions
Array - Convert Object into Int 5 Answers
Converting a string to an int 2 Answers
Convert Array to String 3 Answers
Extracting Numbers from String Place JS 1 Answer
Why Won't This Work? 1 Answer