When you declare your array, if you put a number within the square brackets it indicates the array size, not its contents:
new Object
So when you call:
new Object[environmentMaker.GetEnvironmentData ()]
GetEnvironmentData() needs to return an integer to indicate the array size. Since it’s returning an object array, the error message is that the system cannot convert an object[ ] array to an integer (naturally).
I’m guessing your intent is that GetEnvironmentData() is supposed to return the object array that you pass into SetEnvironmentData(), so maybe your line should look more like:
Nice try, except that, for future problems avoidance, I need to set an exact COPY of the Object [ ] into EnvironmentData, not a REFERENCE that’s why I am struggling to find a constructor for making a second instance.
As I said, it worked in U2, but it do not work anymore (to the chicken despair) in U3 ^^
For additional details, I have two game object / scripts : EnvironmentMaker, and PlayerEnvironment. Each have a EnvironmentData variable, of type Object [ ]. The EnvironmentMaker build a map, which is retrieved by all existing PlayerEnvironment. The problem is that additional data is written into the EnvironmentData of PlayerEnvironment, forcing me to make an instance, not a reference (else, everyone is writing on the same EnvironmentData !)
Well, you can create a copy of the array, but if its elements are objects then they would be references, not copies. (unless they were value types, like integers, in which case they would be copies)
object[] originalArray = ...
object[] copiedArray = new object[originalArray.Length];
for (var i = 0; i < originalArray.Length; i++)
{
copiedArray[i] = originalArray[i];
}
Is that enough or do you need a solution to copy the elements, something like:
copiedArray = originalArray*.Clone(); //creates a copy of the entry rather than passing the reference*
Now that I think about it, making a clone of EnvironmentData is nice, but if the content is reference (and it is, since the content are Hashtable [ ] … ), I am still in trouble.
An Object [ ], which contains Hashtable [ ], which contains Hashtable. All those object are complicated structure, and passed by reference.
If I need a copy of everything, so everything is unrelated…I need a more heavy use of the new function…
I’ll work on it, but about the first question, how do you make a clone / copy of an Object [ ] ?
I posted sample code of how to copy an array. Essentially create a new one and transfer the elements over.
If you want to make deep copies of all the elements though, that’s purely dependent on what those objects are.
It sounds to me that you may want to reconsider how your environment variables are structured, stored, and referenced; perhaps use a strongly typed custom class (that knows how to clone itself) or key-value-pairs of strings (or other value types).
You can’t be serious, in U2, I just had to write new Object [arrayToCopy], and I had a copy. How could things go more complexe ?_? Is it some kind of foothold trap for chickens ?!
Unless I am AGAIN mistaken about the true effect of new Object [arrayToCopy] ?
Unity3 is much more strictly typed (good thing!) and from what I’ve seen in every other programming language that I’ve used, new Object is the syntax and not to make a copy of the array.
If that’s how it was in UnityScript 2 where you can pass your original array into the square brackets, well that’s just plain confusing.
As for whether or not I’m serious manually copying the array, I am. It’s a very standard practice, though depending on the language there may be faster or simpler ways of doing it.
For example, when duplicating a generic List in C# you can pass in the original to the constructor:
List<object> originalList = ...
List<object> copyList = new List<object>(originalList);
Or some classes even provide the ability to generate an array for you: