Sign in to follow this  
barakus

[.net] Storing reference of a value type

Recommended Posts

You mean, for example, having a reference to a float?

If that becomes necessary, I would store the value in some kind of class, and reference that class. That way, I can control exactly how the value is accessed. Anything that must access an instance of one of these objects then has a pointer to it.

(take, for example, my characters. They each have a position and direction vector, which the AI uses.)

Share this post


Link to post
Share on other sites
Yeah, that's pretty much what I want to do. I thought of making a specific class with the variable to do that, but ideally it would be generic enough that you could reference a float or Vector from anywhere to do it.

Share this post


Link to post
Share on other sites
Unless there is a specific relationship between the class being referenced, and the referencer(for want of a better generic word) it can get messy very quickly. e.g, you delete the class containing the reference, and the referencer is now reading empty pieces of memory. It looks like its working for a while, then it all goes boobies up.

Or, you have 10 objects referencing the same class. the first moves the character forwards. The second moves him back. The third spins him around. The fourth does something else, etc. By the time you render him, he is buried head first, up to the waist, in the ground with his pants down because a physics constraint failed.

This has never happened to me (whistle whistle whistle)

Share this post


Link to post
Share on other sites
Yeah, I may just end up doing that in the end. I had already done it with pointers, but I am finding some bugs in it and a few random crashes which I think might have to do with the garbage collector. I think I'll leave the pointers to my C++ code.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
The easiest way to have a reference to a value type is to use boxing.

float a = 12;
object float_object = (object)a;

Now you have reference to a.

theTroll


Modifying float_object will not affect a, but passing float_object to other functions which modify it will affect any other reference to float_object.

Share this post


Link to post
Share on other sites


class boxed<T> where T : struct
{
public T value;
public boxed(T v) { value = t; }
}


The nature of value types is that they are always temporary in nature when you actually do things with them. Auto boxing won't help either, and dealing with objects as your data type is a very very bad idea.

The thing is,
if a method takes in an object, which is actually a boxed float, then you can't actually change it. You will be making a reference change, creating a new boxed float, not making a value change (as a ref float would).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this