How do I use ToBuiltin?

Hi all.

According to the docs one can convert an Array object to a [ ] array object using the ToBuiltin function. Well this has always worked perfectly well and if I use it without pragma strict it still works just dandy but as soon as I enable the pragma it complains that:

So how do I convert an Array into an int[ ] while using pragma strict?

	var s : String  = "1;2;3;4;5;7;12;34";
	var args : Array = s.Split(";"[0]);
	var values : int[] = args.ToBuiltin(int);

The round about way:

	var s : String  = "1;2;3;4;5;7;12;34";
	var args : String[] = s.Split(";"[0]);
	var array : Array = new Array();
	for(var i : int = 0; i < args.length; i++){
		array.Add(parseInt(args[i]));
	}
	
	var intArray : int[] = array.ToBuiltin(int);

Obviously you could forgo the dynamic array altogether here and just initialize intArray to the same length as args and fill it as you go. You can’t cast a string to an int without parsing (or implementing your own cast - i’m not sure JS supports it)

ended up doing this:

	var s : String  = fields[i];
	var args : String[] = s.Split(";"[0]);
	var values : int[];
	var cnt : int = 0;
	for(var str :String in args)
		values[cnt++] = parseInt(args[cnt - 1]);

This works, but having to go and change every single dynamic array in every one of my project’s 40 files is a daunting task already so I don’t want to go and do it all the long, hard way only for someone to them turn around and say ‘Why didn’t you just do this?’ so I figured I’d ask first.

Question: Did you actually test that code of yours? From what I see you are only taking a longer way to fill up the Array() and then still calling ToBuilting exactly the same way I did. With pragma strict that will throw the same error I get…

I’ve tried both ways…

args1 : Array() = s.Split(";"[0]);
args2 : String[] = s.Split(";"[0]);
var intArray1 = args1.ToBuiltin(int);
var intArray2 = args2.ToBuiltin(int);

Both complain about not being able to convert to int…

Edit:
Honestly, I think this got broken in Unity 3.4 and cannot be used any more. I just wanted to ask those smarter than me first, just in case…

If you copy and paste the example from the docs:

…even the example code throws a compilation error…

I’ll look into that, thanks
I’ve only ever worked with Lists once when I was converting a script from JS to C#. If Lists contains an equivalent of:

var l: List = s.Split(";"[0]);

…then that was exactly the reply I was hoping I’d find when I posted the question!!!

Thanks for that :slight_smile: