Archived

This topic is now archived and is closed to further replies.

Andrew Nguyen

Dynamic Variables

Recommended Posts

I''m thinking, is there a Dynamic Varible available? No.was the answer. Now, I have to say, they are useful. What is a dynamic variable you may ask, well, if i said: dynamic int a and a = (c/b), then if c and b bothe equaled 5, a would be 0 now, the computer only processess that once, but what if b or c changed? Well, with a dynamic variable, a would be constantly recalculated if b or c were constantly recalculated too.

Share this post


Link to post
Share on other sites
quote:
Original post by barazor
sounds alot like references
maybe
int &a = (c/b);
would work...not sure though


Blahh.. Maybe! I've got to test this out!



Edited by - MindFlayer on December 31, 2001 4:47:27 PM

Share this post


Link to post
Share on other sites
Maybe the only viable solution so far is Martee''s #define?

Your class would need to create a thread and periodically poll the values of variables it was assigned to monitor - useless overhead. If the variable was a symbolic alias (meaning that it was replaced by the actual values at compile time), then it would work but would introduce some new problems.

I think it''s easier to simply notify any "watcher" variables whenever code modifies a variable''s value.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also, if we''re talking about game programming, doesn''t the
game loop iterate for every frame displayed? If you looked
at these variables at the start of each loop, it should be
plenty.

Share this post


Link to post
Share on other sites
that would suck because a b c would be public.

maybe you could do this:

inline float DyncamicA(){
static float* a;
static float* b;

return (a / b );
}

i dont know i dont do inline often.
you would also need to initialize ur pointers somehow. maybe you could use a template or something too.

Share this post


Link to post
Share on other sites
Build an expression tree. When you want the value of a, evaluate the tree and it will give you the right answer, even if you change the other values.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yeah use an expression tree. An expression tree will have various operators stored in the non leaf nodes. The leaf nodes would hold pointers to things that you want to check. It will take a fair amount of coding and won''t be as easy to use as you are hoping for but it will do the job.

Share this post


Link to post
Share on other sites
You can do it in C++, today with most compilers, by defining a property get method and having it calculate the result as needed.

Wiht MSVC you'd do this:
    
class MyClass
{
__declspec(property(get=a)) float fakeProperty;
float a()
{
return b/c;
}

};

And use set property accessors to set a boolean flag indicating that you need to recalculate.


Edited by - Magmai Kai Holmlor on January 1, 2002 5:47:46 AM

Share this post


Link to post
Share on other sites
Off the top of my head.

  
#include <iostream>
using namespace std;

class DynamicQuotient<typename T>
{
public:
// notice, I take references to the numbers, so that I can ''see'' any changes made to them

// notice also this requires me to use initialization rather than assignment

DynamicQuotient(T& numerator, T& denominator) : n(numerator), d(denominator) {}
T operator()(void) { return n / d; }
// is this the syntax for user-defined casts? I can never remember

T operator T { return n / d; }
private:
// notice, they''re references

T& n;
T& d;
};

int main(int, char**)
{
double a, b;
DynamicQuotient<double> c(a, b);
a = 3; b = 4;
cout << "the value of c is " << c() << endl;
a = 5;
cout << "the value of c is " << (double)c << endl;
// etc

}

You would have to be really careful that the scope of the dynamic variable matched (or was less than) the scope of the variables it refers to. If they go out of scope but it doesn''t, its references are now broken and your program ought to crash.

It could be fairly simply altered to instead of being a division to take a functor object, describing a binary function that could operate on both values. You could probably also chain them.

Share this post


Link to post
Share on other sites
You are all boneheads! j/k! Really, here''s how you do it.

You use a DAG. It''s a Directed Acyclic Graph based on dependencies. Each variable is a node in the graph. Any variable which changes value then propogates downstream in the graph. Updates only occur as needed and always occur when needed.

Share this post


Link to post
Share on other sites
To be more precise regarding the DAG, when a variable changes value, it propogates downstream that it changed value, but not what the rippling value change is. This is necessary for arrays, as we don't want to recalculate a whole array downstream, we just want to inform descendants in the DAG that they are no longer valid. One flag indicates a whole array is no longer valid.

When a value is required, we check to see if it has been flagged as out of date. If it is, we propogate upstream, recalulating until an ancestor is encountered that is still up to date, making it unnecessary to recalculate up to date ancestors.

If only one element of a whole array needs to be recalculated, this method serves that purpose. Arrays could also be flagged as 'calculate all at once', or only 'on a case by case' basis. This would be the programmer's decision, depending on the purpose of the array.

Edited by - bishop_pass on January 1, 2002 1:20:01 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Andrew Nguyen
Please mr. 45 year old, show us the code to do so!


I fear I am nowhere near that old. Build a directed graph. I don''t have any interest in doing so at this time. The best way to do it would be to write an expression parser which builds the graph for you.

Share this post


Link to post
Share on other sites
a = b / c + x;
b = g + f;

  
g + f
|
b / c
|
[] + x
|
a


Acyclic means we can't cycle back around to the top. We can only go downstream. A descendant can never be the ancestor of the descendant or its ancestors.

If a variable changes, it propogates down the graph to descendants.

Edited by - bishop_pass on January 1, 2002 1:36:52 PM

Share this post


Link to post
Share on other sites