C# alternative to pointers for integral types?

Started by
5 comments, last by DvDmanDT 12 years, 2 months ago
I'm trying to move from C++ to C# because Unity3D uses C# fairly extensively. So far so [s]horrific[/s] good. Unfortunately, I have hit a problem - Unity does not allow unsafe compiling if you want to publish using the Unity web player. This means no pointers. I feel naked and alone.

In particualr, one set of methods I used extensively in past C++ projects is a generic single-variable animation system, which would let me make a call like:

animateVar(&someVar, startValue, endValue, animTime);

The function would add the reference to a list, and another function would regularly update the values, setting the variable's new values using the stored pointers.

I tried this in C# using ref, but there seems to be no way to store the reference to an integral type, meaning there's no way to update the variable.

How could I achieve this in C# without pointers?
------------------------------------------[New Delta Games] | [Sliders]
Advertisement
One way would be to store a delegate instead of a pointer. Call the delegate to update the variable rather than assign to the pointed to value.
I've not read much about delegates, but from what I have read, would I have to make a new delegate every time I want to animate a variable?

so instead of writing:
animateVar(someVar, start, end, time);

I would have to write:
animateVar(void delegateName(parm1) {instance.someVar = parm1; }, start, end, time);

Is that along the right lines? Seems like an overly complicated way of updating a variable.
------------------------------------------[New Delta Games] | [Sliders]
You shouldn't do that even in unsafe mode in C# since the GC can move data and therefore invalidate your pointers. You can use pointers for single operations if you combine it with the fixed(..){..} keyword/structure, but you shouldn't store them.

For the delegate, you'd write this when using it:

AnimateVar( a => someVar = a, start, end, time);

and something like

void AnimateVar(Action<float> func, float start, float end, float time)
{
float newValue = start + end / time; // Your calculation here

func(newValue);
}
You could also just jury rig a boxed reference container.
class RefValue<T>
{
public T value;
}

Something like that.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

[font=arial,helvetica,sans-serif]Thanks DvDmanDT, [/font]it's working great now. Not as nice and clean as passing a ref parameter, but at least it's not as long winded as I thought it was going to be.

------------------------------------------[New Delta Games] | [Sliders]

You could also just jury rig a boxed reference container.
class RefValue<T>
{
public T value;
}

Something like that.


I would just like to add a few comments on this in general..

First, you can make it less painfull to use this by implementing implicit casts to and from 'T'. Can cause confusion though.

Second, remember that you would need to store the RefValue<int> whereever you want an int that you can pass by reference and store.

Third, this requires more memory (as in 3-5 times the normal memory in case of an int) which is not really a problem unless you have tons of them, but it fragments memory and most importantly allocates objects on the heap which puts more stress on the GC.


Another solution to problems like this is to use arrays (pools) and indices. With some generic structs magic, you can create a solution similar to that suggested by promit, but that requires only a single heap allocation and can be used (almost) like a normal variable.

This topic is closed to new replies.

Advertisement