• entries
    707
  • comments
    1173
  • views
    434104

Untitled

Sign in to follow this  

107 views

I really shouldn't still be working on the stat system, but it's proving useful and I've run into a situation that will be common that I'm trying to come up with a solution:

class SomeStruct
{
type SomeVariable;

public:
type GetSomeVariable() const { return this->SomeVariable; }
};





I ran into this (and it would be very common occurrence seeing as how a lot of data is hidden) and I've been trying to come up with a solution.

The first idea was quite simple using BOOST and the StatVariableBase class:

template
class FunctionResultStat : public StatVariableBase
{
boost::function Functor;

public:
FunctionResultStat(boost::function Functor) : Functor(Functor) { }

// Error handling would be needed below.
string ToString() const { return boost::lexical_cast(Functor()); }
};





However, that's quite limiting and kind of hackish.

The "best" solution I can come up with is just creating a new class for each function (like my TimeSinceStartStat from yesterday), but that's not really a "usable" solution.

The only other idea I've come up with so far is using the FunctionResultStat and overloading it, allowing for up to 10 different parameters. Won't be the prettiest solution, but it'll be a lot better than using macros.

No matter how I go about it, there is also the fact that it's limited to types that can be cast via boost::lexical_cast.

[edit]
I think I've decided that to add this is just too much hacking. On to more important things (suggestions are still welcome if anyone has any.)

Any suggestions?
Sign in to follow this  


4 Comments


Recommended Comments

Here's nice chat about Get/Set.

Of course, if you need to store different types of stats, I would do something silly like this:

class stats
{
public:
bool GetBool( std::string key ){ return mBool[ key ]; }
int GetInt( std::string key ){ return mInt[ key ]; }
float GetFloat( std::string key ){ return mFloat[ key ]; }
const std::string& GetString( std::string key ){ return mString[ key ]; }

void SetBool( std::string key, bool b ){ mBool[ key ] = b; }
void SetInt( std::string key, int b ){ mInt[ key ] = b; }
void SetFloat( std::string key, float b ){ mFloat[ key ] = b; }
void SetString( std::string key, const std::string& b ){ mString[ key ] = b; }

private:
std::map< std::string, bool > mBool;
std::map< std::string, int > mInt;
std::map< std::string, float > mFloat;
std::map< std::string, std::string > mString;
};

class SomeClass : public stats //, ...
{
};



But that's just me.

Share this comment


Link to comment
Quote:
Original post by Slather
Here's nice chat about Get/Set.

Of course, if you need to store different types of stats, I would do something silly like this:
*** Source Snippet Removed ***

But that's just me.


Thanks for the input! That is a nice thread on Get/Set.

I actually have a system similar to the one you posted (I call it PropertyBag), the problem is that, for the engine side of stats anyway, I'd prefer to store pointers so that there isn't any overhead of setting the value every frame. So, I can add a stat using the address of my AverageFrameRate member and every frame the system renders the value without me having set it.

Either way, regular variables are working just fine, it's the fact that there times when you want to use a function's return value as a stat. I've made hackish fix for that, but some of these functions need parameters (as in my TimeSinceStart stat. It returns a string, but requires that a formatting flag be supplied.)

I guess the big problem is that I'm trying to add way too much functionality and should just stick with what I've got. If there's a function that I want to use a stat, I can just use the the method that I did for TimeSinceStart.

Or maybe the big problem is that I'm still using C++ instead of C# lol.

Again, thanks for the input! I really appreciate it.

Share this comment


Link to comment
public class Vars

{
private Dictionary<string,object> data=new Dictionary<string,object>();

public object this[string index]
{
get
{
try{return data[index];}
catch(KeyNotFoundException exception){return null;}
}
set{data[index]=value;}
}
}


Is this what you're wanting, just more C++ish?

Share this comment


Link to comment
Quote:
Original post by nerd_boy
*** Source Snippet Removed ***

Is this what you're wanting, just more C++ish?


Pretty much, yea. I'm not sure if objects are stored by reference or whatnot in C# though.

Pretty much:

// in my class
real32 SomeObject = 0.0f;
MyStats["SomeObject"] = &SomeObject;

// MyStats["SomeObject"] == 0.0f

SomeObject = 10.0f;

// MyStats["SomeObject"] == 10.0f



The link that the post by fallenang3l in the thread Slather suggested might be of some help. I'm going to check that out.

Share this comment


Link to comment

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