Jump to content
  • Advertisement
Sign in to follow this  
Damocles

Unity C# alternative to pointers for integral types?

This topic is 2494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
You could also just jury rig a boxed reference container.
class RefValue<T>
{
public T value;
}

Something like that.

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!