Check if a boolean is true by use of a string
Im trying to figure out a way to check if a boolean is true, but through a string.
Something like this:
bool Boolean = true;
String TempStr = "Boolean"
if(UseAsBool(TempStr) == true)
Basically it would be doing this:
if(Boolean == true)
Any help with this would be greatly appreciated :)
Answer by ClearRoseOfWar · Dec 17, 2015 at 12:13 AM
(DEC 19 2015)
The answer is simply using the name of the sprite to do the check, and then also check if the boolean is true.
I wanted it to be a bit less code, but its not taking up that many lines of code, so its fine.
Answer:
if(tempstr == "Nameofimage" && Nameofimage == true) //Be Happy
(DEC 17 2015)
Thank you very kindly :) I am pretty sure that this doesn't happen often. This solution would be perfect (Everything works great) if I already knew ahead of time what boolean I'd replace myBool with. However, I am doing something a little bit more strange. I`ll try to explain this the best I can :P
Inside my assets folder, I have the sprites, which are named:
%|-781188308_1|% %|-284706502_1|% %|-90134012_3|% %|1940178792_2|%
I also have 50+ booleans named after their corresponding sprites names`.
ImageType0 = true;
ImageType1 = false;
ImageType2 = true;
ImageType3 = false;
The sprites are images, that can be viewed if they`ve been unlocked. If the image is not locked, then its corresponding boolean is true. (meaning its been unlocked)
Now when I set it up to select a random sprite, It needs to be able to check if its been unlocked or not yet, so I tried this:
stringBool = System.Convert.ToString(GameController.Temp_Sprite.name);
if(stringBool== "True") {
print("True");
}
else {
print("False");
}
I came to the conclusion that its always going to be false, since the stringBool is just the name of sprite.
How can i get the value of ImageType0 (the bool), simply by using the image's name that is randomly selected
Thank you for you help with this @Knnthra. As you can see, its not entirely trivial...
Cheers. DLively
wow, your doing some strange things here. first of all: posting an reply as answer to your question :P
Lets see: You could make a class ImageBooleanPair holding an image asset and an boolean, is serializable and so on (maybe as scriptable object or so). So you could use a 50+ elements array for this.
but to answer your question
How can i get the value of ImageType0 (the bool), simply by using the image's name that is randomly selected
reflections bool b = (bool) typeof(YourClassWhichHoldsAllTheBooleans).GetField("FieldName").GetValue(null);
Hi @Soraphis, Thanks for your reply, time and idea!
Terribly sorry for the latish reply. I am a hobbyist who works every day.
Indeed this may seem strange -> But I am making a puzzle game which has many images to select from.. I guess I should have said that from the start :)
Only two images per category are available from the start of the game. Once you complete any one of the two first available puzzles, the third becomes available.
This process is done with booleans.
there are 50+ puzzles, which means 50+ booleans.
When "Random" Is selected, a random puzzle is chosen by a random puzzle selection algorithm.
Before it can load that puzzle, its needs to check if it can or not.
This is where the "IF" statement comes in. It needs to check if that randomly selected puzzle's boolean is true or false.
so I get the name of the selected puzzle stored into a string, where I'd then like it to check if its boolean equivalent is true or false to deter$$anonymous$$e if its needs to select another random puzzle or not.
I see the route you have suggested, and I think I understand what you're saying. I now have a plan B :)
What I'd really like to know first, is if I can take the string contents of stringbool to enable / disable its boolean equivalent. (Can this be done?)
if(UsingAsBoolEquivalent(stringbool) == true) //Be happy
If it can't be done then I can move forward with plan B, however I don't want to scrap it just yet until I know for sure.
Again, Thanks for your help guys! Cheers!
DLively
In the FieldInfo class of the reflection framework is beneath the method GetValue(), a method SetValue.
but i have a addition to my comment above: The last parameter: if your boolean fields are not static, replace null with the reference to your class-who-holds-the-boolean-values object.
with GetValue, you'll get the value not the reference to the field (afaik), to change this value you'd need to use the SetValue method
edit: but using reflections in this kind of way may not be the intention behind reflections :P i'd really recommend checking first if plan B would'nt be a cleaner-programmed solution.
Answer by Knnthra · Dec 16, 2015 at 07:33 AM
I don't know why you would ever do this, but you could try something like this :)
bool myBool = true;
string stringBool;
stringBool = Convert.ToString(myBool);
if (stringBool == "true")
{
}
Your answer
Follow this Question
Related Questions
Populate Custom List with Csv items 1 Answer
how can i change game to false when the conditions are met in the winner() function? 0 Answers
Animation problem? 0 Answers
My player doesn't take fall damage ... please help 0 Answers
How to copy and paste animation events from one clip to another if those clips are included in FBXs? 1 Answer