Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C# alternative to pointers for integral types?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Damocles   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2012 - 11:24 AM

I'm trying to move from C++ to C# because Unity3D uses C# fairly extensively. So far so horrific 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?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9674

Like
0Likes
Like

Posted 11 February 2012 - 11:29 AM

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.

#3 Damocles   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2012 - 12:01 PM

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]

#4 DvDmanDT   GDNet+   -  Reputation: 1002

Like
0Likes
Like

Posted 11 February 2012 - 12:51 PM

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);
}

#5 Promit   Moderators   -  Reputation: 7667

Like
0Likes
Like

Posted 11 February 2012 - 12:56 PM

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

#6 Damocles   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2012 - 01:42 PM

Thanks DvDmanDT, 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]

#7 DvDmanDT   GDNet+   -  Reputation: 1002

Like
0Likes
Like

Posted 11 February 2012 - 10:52 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS