Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic Variables

Started by Andrew Nguyen Dec 31, 2001 at 3:11 PM 29 replies 900+ views
Original Post
Andrew Nguyen
Andrew Nguyen
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.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Martee
Martee
#define a (c / b)

ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
VolkerG
VolkerG
I don''t know of any programming language that supports "Dynamic Varaibles".
But how can 5/5 be 0?
Andrew Nguyen
Andrew Nguyen
Minor thing, forget it.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
MindFlayer
MindFlayer
Hmm... Interesting idea though. One could write a C++ class or something
Andrew Nguyen
Andrew Nguyen
It actually would be VERY useful in game programming (THe truth)
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
barazor
barazor
sounds alot like references
maybe
int &a = (c/b);
would work...not sure though
MindFlayer
MindFlayer
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
Andrew Nguyen
Andrew Nguyen
Maybe that class would accept a string operator and will recalculate again and again?
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Oluseyi
Oluseyi
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!
EvilCrap
EvilCrap
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.
strtok
strtok
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.
Andrew Nguyen
Andrew Nguyen
Now if we could only do:

dynamic((type)*variable,char*expression)... that''d be the day
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Andrew Nguyen
Andrew Nguyen
Slight chance it might come in Python 2.3
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Shannon Barber
Shannon Barber
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
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
DrPizza
DrPizza
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Andrew Nguyen
Andrew Nguyen
But, what I need is that I need is so that any variable could be used by that class.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
bishop_pass
bishop_pass
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.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
bishop_pass
bishop_pass
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
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
Andrew Nguyen
Andrew Nguyen
Please mr. 45 year old, show us the code to do so!
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.