开发者

Best way to get object data?

I need to display some stats, numbers, and graphs about various game objects on the screen.

(examples: camera position, field of view, frames per second, fill rate, number of objects culled, etc... )

Currently any object which wants to be graphed or displayed implements an interface along these lines:

public interface IGraphable
{
    float GraphableValue { get; set; }
}

Then that object can be sent to a graph component to be displayed. This has some obvious drawbacks like not being able to graph 2 different pieces of 开发者_JAVA技巧data which belong to the same class.

What I want is a way to pass a pointer to where the data is located or a pointer to a function which knows how to return the data instead of passing the object to the display component.

I believe that this is what delegates are for but I don't understand how to use them in this context (Actually I don't understand them very well at all). Also, is there another (smarter/better) way to do this?

Thanks!


Why not invert the control like this:

public interface IGraphable
{
    void BuildGraphable( IGraph g );
}

interface IGraph {
    void AddValue( double value );
}

this is a preferred option in OO anyway as it hides details of the IGraphable implementation. Additionally you can now extend IGraph for added functionality without breaking compatibility.


Depending on how you're doing things, you could possibly use Reflection (attributes on accessors), although that can be relatively confusing at first too. But it's a very useful tool in your arsenal, so it's well worth spending the time on. Here is a great tutorial on how to use them:

http://www.brainbell.com/tutors/C_Sharp/Attributes.htm

But then, learning delegates is also very useful, and that does sound like a good solution. I haven't looked deeply into it, but this tutorial on the same site might be useful:

http://www.brainbell.com/tutors/C_Sharp/Delegates_and_Event_Handlers.htm


I have decided to do the following:

public class GraphComponent
{
   private Func<flaot> _function;

   public GraphComponent(Func<flaot> function, ...)
   { ... }
}

This allows me to specify how the data is retrieved by writing something like this:

FPSComponent fpsc = new FPSComponent();    
GraphComponent fpsg = new GraphComponent(delegate() { return fpsc.ApproximateFPS; }, ...);


What I want is a way to pass a pointer to where the data is located or a pointer to a function which knows how to return the data instead of passing the object to the display component.

If you don't want to add your objects to your graph component BECAUSE

This has some obvious drawbacks like not being able to graph 2 different pieces of data which belong to the same class.

Maybe a list will solve your problem ?

public interface IGraphable
{
    List<float> GraphableValues { get; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜