- Home /
Converting string to boolean
Hi guys,
I have a few doors and few keys to open specific doors. what I am trying to do is to create a function so that i can reuse them.
The problem that i encounter is converting a series of string to boolean. Can't use parseBoolean(); Don't even know that it is the correct way to do it or is there a better way? Please assist and thanks in advance!
var doorNum: int; static var accessGranted_1: boolean; var isOpen_1: boolean = false;
checkTileOpenDoor_spaceship(1);
function checkTileOpenDoor_spaceship(doorNum){
var doorIndex: int = doorNum;
var currentTileTypeIndex: String = "TileOpenDoor_" + doorIndex;
var isOpenIndex: boolean = parseBoolean("isOpen_" + doorIndex);
var accessGrantedIndex: boolean = parseBoolean("accessGranted_" + doorIndex);
var TileEndIndex: String = "TileEnd_" + doorIndex;
if (currentTileType == currentTileTypeIndex && !isOpenIndex && accessGranted){
//open door
GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
GameObject.Find(TileEndIndex).animation.Play("open");
isOpenIndex = true;
} else if (currentTileType != currentTileTypeIndex && isOpenIndex){
//close door
GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
GameObject.Find(TileEndIndex).animation.Play("close");
isOpenIndex = false;
}
}
Rdgs, James Ser
i was thinking about the same thing, it can be really handy really.
Answer by Ashkan_gc · Jun 01, 2010 at 02:10 AM
you should use bool.Parse to convert a string to it's logical boolean representation.
This may be a better link for it: http://msdn.microsoft.com/en-us/library/system.boolean.parse.aspx
Thanks for your suggestion. But it doesn't work. It says unknown identifier: 'bool'
in C# the class name is bool. in js you might need to use another name like boolean. i don't know. i can create a C# class that exposes this method for you. tell me if you need and i'll edit the answer.
bool.Parse passes user input of true or false to a bool. What jamesser asks is to have a series of bool , like doorIsOpenFloor01 = false, doorIsOpenFloor02 =false, and a var currentfloor: int ...etc and in your script to call them as "if so and so doorIsOpen0(currentFloor) = false
In UnityScript (javascript) it's boolean.Parse() like Ashkan thought for anyone reading this later.
Answer by NewfieJoe · May 04, 2011 at 11:38 PM
You can create a hashtable and store the values in it then read it from there if you absolutely need to reference them this way. Easier would be to use boolean arrays.
public var doorNum: int; static var accessGranted: boolean[]; public var isOpen: boolean[];
function checkTileOpenDoor_spaceship( door: int ){
var currentTileTypeIndex: String = "TileOpenDoor_" + door; var TileEndIndex: String = "TileEnd_" + door;
if (currentTileType == currentTileTypeIndex && !isOpen[ door ] && accessGranted [ door ]){ //open door
GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
GameObject.Find(TileEndIndex).animation.Play("open");
isOpen[ door ] = true;
} else if (currentTileType != currentTileTypeIndex && isOpen[ door ]){ //close door
GameObject.Find(TileEndIndex).animation.wrapMode = WrapMode.Once;
GameObject.Find(TileEndIndex).animation.Play("close");
isOpen[ door ] = false;
} }
The key is that if you have 8 doors, you need to declare 8 booleans for isOpen and 8 bools for accessGranted. The only good way to do that is with arrays.
Yeah, that's why I thought they'd be best. You could have hundreds and it will be fine.