- Home /
Storing multiple integers into one
Hello, this question is very stupid. But I am making a padlock script that is unlocked with 4 different integers and I was wondering if there is any way of making it so that for example. If you have a main int called currentCode. What I want to do is set all of the 4 integers into 1. So it would be like option1, option2, option3 and option4 would be for example (6 4 7 8) as the separate integers. Then I want the currentCode to display the 4 options (6478).
If there isn't any way of doing this that's fine as long as someone can guide me in the direction I need to go towards.
Like if I could do something like currentCode = option1, option2, option3, option4; That would be great but that doesnt seem to be the case
There is no need to consider this as far as the specifics of the coding go. Only for display purposes. You can do something like this, however, using an Array
Thank you for the quick reply
Okay, so is there any way of doing this with arrays then. So like being able to set 4 options of the array to the 4 integers?
Also, was just thinking. Would I be able to do this with strings ins$$anonymous$$d?
You can make an array of anything, more or less. string[]
1d string array string[][]
2d string array.
Answer by fafase · Mar 11, 2018 at 11:02 AM
The solution to your problem is enum with Flag attribute.
[System.Flags]
public enum Option
{
First = 0,
Second = 1,
Third = 2,
Fourth = 4,
Fifth = 8
}
Notice the give value are power of two.
You can then declare an enum reference and add values to it.
Option options = Option.First;
options |= Option.Third;
options | = Option.Fifth;
this contains First, Third and Fifth.
You can figure out if it contains a specific value:
private bool ContainsValue(Option value)
{
return ((options & value) == value);
}
And you can remove a flag with:
options &= ~Option.Third;
Plenty of it over the internet.