x


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

more ▼

asked Jun 01 '10 at 02:02 AM

James Ser gravatar image

James Ser
37 9 9 11

i was thinking about the same thing, it can be really handy really.

Aug 13 '10 at 03:27 PM alexnode
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

you should use bool.Parse to convert a string to it's logical boolean representation.

more ▼

answered Jun 01 '10 at 02:10 AM

Ashkan_gc gravatar image

Ashkan_gc
9k 33 56 117

Jun 01 '10 at 02:34 AM Cyclops

Thanks for your suggestion. But it doesn't work. It says unknown identifier: 'bool'

Jun 01 '10 at 02:51 AM James Ser

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.

Jun 01 '10 at 09:01 AM Ashkan_gc

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

Aug 13 '10 at 03:37 PM alexnode

In UnityScript (javascript) it's boolean.Parse() like Ashkan thought for anyone reading this later.

May 04 '11 at 11:25 PM NewfieJoe
(comments are locked)
10|3000 characters needed characters left

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;

  }
}
more ▼

answered May 04 '11 at 11:38 PM

NewfieJoe gravatar image

NewfieJoe
106 11 12 20

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.

May 05 '11 at 02:14 AM Owen Reynolds

Yeah, that's why I thought they'd be best. You could have hundreds and it will be fine.

May 05 '11 at 05:18 AM NewfieJoe
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x418
x239
x226

asked: Jun 01 '10 at 02:02 AM

Seen: 3219 times

Last Updated: Jun 29 '10 at 04:47 AM