0

So I'm beginning to use JavaScript with a new gaming platform I'm trying out and LOVE the interface. It's making everything so easy compared to the in the garage type setup I'm used to. Learning JavaScript has been pretty easy so far but I've hit a snag with typeof.

private var Playing : boolean = false;

function PlayKeys(keys,times)
{
	if(!Playing)
	{
	Playing = true;
	Debug.Log(typeof(times)); //priming typeof unsure why but it makes the code work
	Debug.Log(typeof(float));
	Debug.Log(typeof(float[]));
	if(typeof(times) == typeof(float[]))
	{
	AnimateKeysAndTimes(keys,times);
	print("Keys & Time"); //Debug Code
	}
	else if(typeof(times) == typeof(float))
	{
	AnimateKeysConstantTime(keys,times);
	print("Constant Time");
	}
	else
	{
		print("Failure");
	       Playing = false;
		return;
	
	}
    }
}

Animation Code works like a dream and uses coroutines so I can put them in branch statements without making everything a convoluted mess. The issue is that I want one function that is "overloaded" for different types of animations. However, for javascript to do this I have to typecheck the variables myself since javascript allows dynamic typing. When I do typeof(timer) if it's a float I get a string "System.Single" if it's an Array I get "System.Single[]". However, typeof(timer) == "System.Single" when timer is a float won't evaluate to true ... kk .... However, I found if I primed the typeof declarations typeof(timer), typeof(float), typeof(float[]) I can than compare them and get true ..... this confuses me to no end. I think i'm misunderstanding how to use typeof and need someone with a bit more experience to tell me why this works.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by ChaseRLewis
0

Off topic first, even though JavaScript is very loose in type checking, your design should not use typeof() too much. It could get tricky in the future.

On topic, how about convert the typeof() to a string and compare each string with it instead of using Debug.log()?

var typeOfTime = typeof(times)
var typeOfFloat = typeof(float)
var typeOfFloatArr = typeof(float[])
if (typeOfTime==typeOfFloat) {
  ...
}
0

You're right. Something strange is happening. Post again with a lot more specifics on what you are using.

In JavaScript 'typeof' is a unary operator, like the hyphen used to negate a number or the exclamation point used as a logical "not" operator. Saying "typeof(foo)" is identical to "typeof foo" ( like "-(1) === -1" ). The "typeof" call can't be priming anything as it doesn't even call a function.

0

Think it might have been an instability tried it again and the exact code that didn't work yesterday works today. No idea why. Unity does make mention I need to update some stuff as the current build is unstable, just chock it up to that.

Edited by ChaseRLewis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.