Good morning, everybody! I’ve been having a great time slugging it out with C# in unity so far. I have an awesome little interface that, if you’d told me I was going to put it together a few weeks ago, I would have laughed in your face
That being said, I’ve also learned a few things about keeping code somewhat simple.
I have a list of strings via the new string[ ] code in c#, the last item of which is basically meant to be a custom function. IE, building your own weapon, etc. I may want to change the string listing to add or remove preset items. Right now, my code checks to see if the index of that string[ ] listing is the last item, but it’s via a hard integer. IE, if it is 4 (being 5th in the list), do this. Do that if it isn’t. I’d rather find a way to return the length or count of that listing, but I can’t seem to get that so far. Am I missing something about the attributes listing for this kind of string or group of integers?
private string[] planetGravityValueString = new string[]{"Light Gravity", "Normal Gravity", "Heavy Gravity"};
private int planetGravityValue = 0;
.....
if(planetGravityValue < 2){
//do this
}
else{
//do that
}
In this case, I’d like to replace the hard value of 2 with something that would reference the size of that list. Subtracting 1 to account for the number actually starting at 0 isn’t an issue, but I can’t seem to get the number corresponding to how many values this list holds. I’ve tried Debug.Log(planetGravityValueString.size); as well as .count, .length etc. So far, no dice.
Does anyone have any suggestions?
I look forward to reading your replies!
-Lance
string[ ] is not a list, thats an array and adding / removing stuff on the fly wouldn’t be the best idea there.
for adding and removing stuff, I would recommend List or LinkedList instead
as for your comparision there: use planetGravityValueString.Length - 1 instead of 2
…Length - 1 would be the last item in the array
Awesome, dreamora. That worked like a charm!
In this case, it wouldn’t be adding on the fly. These are all set up at runtime, and won’t be modified. That freaking capital L on length was just what I needed. Darned rookie mistake. Makes sense though, since I am one 
Thanks again!
Sounds like you might want to consider using enumeration types: Enumeration types - C# reference | Microsoft Learn
I could certainly do that. This will eventually lead into exporting this information after it has all been entered and selected. IE, you’ve decided what gravity type you will deal with, etc. Though, that has led to another question regarding exporting/importing of character data and options. In that case, a list could have different datatypes together. Knowing that the first value would be a string for someone’s name, but the next value could be an integer for the class type, a boolean to determine if certain things were true or false, etc. It would be a pre-defined number of attributes, but unlike what I’m using above, it would have a mix of different datatypes. Can enumeration handle a string of different datatypes being passed to it? IE, not all integers or strings, etc.
The point of an enumeration is that there is no “string” representation of it. It is it’s own type. If you need to save, the integer representation:
enum GravityType
{
Light = 0,
Normal = 1,
Heavy = 2
}
private GravityType planetGravityValue = GravityType.Light;
if (planetGravityValue < GravityType.Heavy)
{
}
else
{
}
//saving
int gravityIntValue = (int)planetGravityValue;
PlayerPrefs.SetInt("gravity", gravityIntValue);
//loading
planetGravityValue = (GravityType)PlayerPrefs.GetInt("gravity");
It’s a bit late, so maybe I misunderstood what you’re asking. Likely I have an oversight in my code above, but hopefully you get the idea.
I do, yes. Wow, that makes much more sense than before - and it’ll beat the heck out of trying to remember which index I’m using with all the numbers and such! I won’t be able to fiddle with that until I get home after work, but I will certainly give it a try. Again, thank you for explaining it!