Type-neutral array?

One part of my program desperately needs to use an array which can mix floats, ints, and unsigned ints. This is easy to do in assembly language, but how is it done in Unity’s scripting languages?
I suppose there’s little hope that Unity will ever allow the use of assembly outside of C++ plugins?

In JS, you use Array or ArrayList. In C#, you use ArrayList. You can also use a built-in array of Object.

No chance whatsoever.

–Eric

unfortunatly though all those types ( int, uint, float ) are all 32 bit, its really kind of painful in .net to actually treat them the same. You can use object data type, which will also handle keeping the type but with boxing it will use more memory than you’d think.

But in reality if memory is not a concern then yea use ArrayList if you need it to be able to grow or shrink, otherwise just object[ ] builtin.

you could try using BitConverter, but that then requires use of arrays of bytes.

Maybe theres some namespace with more type support that could help you, but the reality is at least straight up the type strength of .net impeds things that would be logical on assembly or any non strongly typed language where your just dealing with memory.

Though with the use of unsafe keyword theres things like pointers and better type conversion support ( better as in more options ). But unfortunatly the unsafe keyword/functionality its removed from unity afaik

You may want to look into just using a double to store all those values. I’m pretty sure that it can hold a range of any 32 bit type. It will take up twice as much memory but it wont need to box which should actually be alot more performant and wont leave a dirty memory footprint or trigger alot of garbage collection.

But again its not a win win because if you need the array to keep track of what element is what type you’ll need to then store that as well, and casting can become slow when you’re dealing with a huge amount of data.

Thank you both for the quick responses.
I don’t need to store the type, since it’ll be based on context and each element will be cast to the proper type of variable when being fetched from the array. I suppose I could always just store floats as a uint by first multiplying them by a large number to reduce precision loss and then divide them by the same number before using them; but that will slow things down a bit.
Doubles might be an option as well.

You could try using a union, technically c# doesn’t offer this however it does offer structlayout and fieldoffset : http://msdn.microsoft.com/en-us/library/acxa5b99(v=vs.80).aspx

However I’ve not tested this and it probably requires UNSAFE in which case you’re screwed as sadly there’s no way of setting unsafe with Unity :confused: (I wish they’d leave that up to the developer though, if we screw up it’s our fault there, and anyway even with the safety nets and sandboxing they’ve implemented it’s still quite easy to crash unity via script anyway so they may as well allow some more powerful features of the language even if it means getting to know the in’s and outs of memory management a little better).