I’m trying to work with pointers for modularity. For example
var myArrayA = new Array();
var myArrayB = new Array();
var myArrayC = new Array();
myArrayA.push(1);
myArrayA.push(2);
myArrayA.push(3);
myArrayB.push(4);
myArrayB.push(5);
myArrayB.push(6);
myArrayC.push(7);
myArrayC.push(8);
myArrayC.push(9);
PrintArray(myArrayA);
PrintArray(myArrayB);
PrintArray(myArrayC);
function PrintArray(myPointer)
{
var x : int = 0;
for(x = 0; x < myPointer.length; x++)
{
Debug.Log(myPointer[x]);
}
}
This outputs
123
456
789
So it works, but myPointer isn’t a pointer. It’s copying my array each time I call PrintArray. I don’t want this. Does anyone know how I can make myPointer a pointer? Also, how do I pass the address of an object?
there is no such thing as pointers in .NET or any managed environment.
Objects though by default are references and thats what you get there too, no idea how you conclude that it is a copy.
if you want to be able to alter an object reference within a function, you must use C#, then you can specify it as ref which passes in a reference to the object (itself a reference).
Arrays should be passing by reference (at least they do in C#). Can you show us how you concluded that they are passing copied arrays?
EDIT: in C#, you only need to use the “ref” keyword if you want to pass a value type (struct) as a reference. Otherwise all non-struct objects are passed by reference anyway (with a few immutable exceptions, such as String).
Sorry guys. I don’t know what I was thinking. I just re-read my post. Wow.
Of course arrays are passed by reference, that’s Programming 101.
Bad example.
Here’s what I was getting at:
class Color
{
int R;
int G;
int B;
};
var myColor : Color;
function Start()
{
myColor.R = 0;
myColor.G = 255;
myColor.B = 0;
DisplayColor(myColor);
}
function DisplayColor (C : Color)
{
//Some display routine
}
Its my assumption that DisplayColor creates a copy of “myColor” and stores it in “C”. Is this correct?
So If I did:
C.R = 255
Will myColor.R = 255 or will it equal 0?
This is what I was originally trying to get at. I have several class objects with alot of data in them. I don’t want to pass copies of them to other functions, as that’s computationally expensive.
EDIT: I’m under the assumption myColor.R will equal 0.
EDIT - Reply to both posts: I see, passing by ref isn’t handled by JavaScript. That sucks. C# exclusive. Thanks for the replies
In this case, you create a class Color. This should pass by reference (it should not make a copy for the method call). However, I believe if you use the intrinsic “Color” type provided by Unity, it will make a copy. At least in C# it’s a struct (value-type that creates copies) but I don’t know how that translates into the JavaScript/UnityScript world.
EDIT: According to this: http://answers.unity3d.com/questions/16104/struct-in-javascript you cannot create custom structs in JavaScript. You can create a C# struct however and use it in your JavaScript code though. Similarly, intrinsic Unity structs (such as Vector3) still behave as structs in JavaScript, but you just can’t create your own in JavaScript.
EDITx2: To answer your question “Will myColor.R = 255 or will it equal 0?”, I believe if you change its “R” value within your method, it will alter the original myColor reference as well. If you passed in an intrinsic Unity Color object, it would not alter the original instance (as it is a struct).
Yes, you can create custom JavaScript classes in Unity, but they will be object classes that pass by reference; you cannot create structs that pass with copies (like integers and strings)
Since you’re trying to create “pointers”, this would suit you fine. But I don’t think you can directly manage/view addresses or anything, you just have to keep tabs by assigning/passing object references.
Sorry to bump this thread. But I’ve currently got this thing going on with Unity, and I seem to get the feeling it has to do something with pointers too.
Somewhere in my code I do: currentTexture = renderer.material.mainTexture;
Then later on I do currentTexture = otherTexture2D but that doesn’t update my texture.
However, when I do renderer.material.mainTexture = otherTexture2D it works.
That’s just not true. C# has pointers with unsafe code. I don’t know if you can use unsafe code in Unity, and I wouldn’t recommend it if you could, but still, they’re there and they are real pointers.
That’s not true. Objects’ references are passed by value. Strings are treated exactly like any other reference type and immutability has nothing to do with it.
Consider:
// In some function:
Bar obj = new Bar();
Foo(obj);
public void Foo(Bar obj)
{
obj = new Bar(); // only reassigns locally, does not change original obj reference
}
vs.
// In some function:
Bar obj = new Bar();
Foo(obj);
public void Foo(ref Bar obj)
{
obj = new Bar(); // reassigns the original obj reference
}