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

RunTime Question

Started by Ultra The Vampire Aug 30, 2005 at 1:44 PM 4 replies 750+ views
Original Post
Ultra The Vampire
Ultra The Vampire
how do i change variables in a program during runtime? do i assign a pointer to default value and reference in function that will be called (lets say for example sake) in a input function? like if i have enemy that comes up by default with a blue color key, but i want to have move a slider that changes the and update the color key during runtime.
frob
frob
What exactly are you trying to accomplish?

Your first question about changing variabls at runtime is something you probably already know. In many languages it looks like x = y+1; You have now set the variable x to the value y+1.

Your second question about pointers, default variables, references, and input functions, show that you don't understand pointers very well. If you want to learn about pointers, try a tutorial on them like this one.

The next comment about a color key is much more complex. It sounds like you want to read the value from a slider control, and set the value to your color key (whatever that's supposed to be).

frob.
Ultra The Vampire
Ultra The Vampire
no you have my question all wrong, it was'nt seperate questions. It was one whole question. allow me to clarify.

I want to know if data for something once i it has been stored, how would i go about changing it and it's change having impact. like the color key example above, when the application starts it sets the colorkey, would i go and edit the variable in memory by reference or something and that would cause the color to change? or would i have to change the variable and run some function to call it again?

basically i want to know, if a function holds onto the variables in memory or not.
frob
frob
Quote:
Original post by Ultra The Vampire
basically i want to know, if a function holds onto the variables in memory or not.


In C and C++, a static variable will keep it's value between calls. Other variable types (such as register or auto which is the default) will not.
For example:
 void ShowCount() {   static int a = 0;   int b = 0;   printf("a=%d, b=%d\n", a++, b++ );   return; } int main() {   int i=0;   for(; i<100; i++)     ShowCount();      return 0; }

In this example, the variable a will increase, the variable b will remain constant. As you put it, the function holds on to the static variable.

Quote:
would i go and edit the variable in memory by reference or something and that would cause the color to change? or would i have to change the variable and run some function to call it again?


This is a design thing. You normally don't want to hold on to addresses and references unless you are its creator/owner. In an object-oriented world, you want to tell the object to set it's value by using an accessor or member function. In this case you might say currentSprite->KeyColor( newcolor ) to set the new color.
kSquared
kSquared
Quote:
Original post by Ultra The Vampire
basically i want to know, if a function holds onto the variables in memory or not.

Once a scope block (e.g., method, for-loop, if statement, etc.) ends, all local variables in that function have gone out of scope and can no longer be referenced.

It sounds like you want the color key to have a default value in your application, but you want this value to later be changed by users. In this case, one possible design improvement is store your application-specific data in some kind of GuiConfiguration object (in which the color key would be included). GUI components can then ask this object for the current value of the key, or set it themselves.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
random_thinker
random_thinker
Quote:
Original post by kSquared
Quote:
Original post by Ultra The Vampire
basically i want to know, if a function holds onto the variables in memory or not.

Once a scope block (e.g., method, for-loop, if statement, etc.) ends, all local variables in that function have gone out of scope and can no longer be referenced.

It sounds like you want the color key to have a default value in your application, but you want this value to later be changed by users. In this case, one possible design improvement is store your application-specific data in some kind of GuiConfiguration object (in which the color key would be included). GUI components can then ask this object for the current value of the key, or set it themselves.


Is this basically a struct?

Another possibility (that may or may not be safer, depending upon your requirements) is to make a superclass with static data members (or a static property array). Then all superclass instances or subclass instances must use these same class-based data members, and if any instance changes this value, all instances must now access this changed value, super or sub. Something like:

#include <iostream>// Super definitionclass my_super{   protected:   static std::string color;   public:   void changeColor(std::string);   std::string getColor();};// my_super implementationstd::string my_super::color = "white";void my_super::changeColor(std::string value){  my_super::color = value;}std::string my_super::getColor(){  return my_super::color;}// sub Definitionclass my_sub : public my_super{   public:   my_sub();};my_sub::my_sub()   :my_super(){ }int main (int argc, const char * argv[]){   my_super red, blue, green;   my_sub redder, bluer, greener;   std::cout << "\ndefaults." << std::endl;   std::cout << "red's color     : " << red.getColor()  << std::endl;   std::cout << "blue's color    : " << blue.getColor() << std::endl;   std::cout << "green's color   : " << green.getColor()  << std::endl;   std::cout << "redders's color : " << redder.getColor()  << std::endl;   std::cout << "bluer's color   : " << bluer.getColor() << std::endl;   std::cout << "greener's color : " << greener.getColor()  << std::endl;    red.changeColor("red");   std::cout << "\nred takes over." << std::endl;   std::cout << "red's color     : " << red.getColor()  << std::endl;   std::cout << "blue's color    : " << blue.getColor() << std::endl;   std::cout << "green's color   : " << green.getColor()  << std::endl;   std::cout << "redders's color : " << redder.getColor()  << std::endl;   std::cout << "bluer's color   : " << bluer.getColor() << std::endl;   std::cout << "greener's color : " << greener.getColor()  << std::endl;    blue.changeColor("blue");   std::cout << "\nblue takes over." << std::endl;   std::cout << "red's color     : " << red.getColor()  << std::endl;   std::cout << "blue's color    : " << blue.getColor() << std::endl;   std::cout << "green's color   : " << green.getColor()  << std::endl;   std::cout << "redders's color : " << redder.getColor()  << std::endl;   std::cout << "bluer's color   : " << bluer.getColor() << std::endl;   std::cout << "greener's color : " << greener.getColor()  << std::endl;    redder.changeColor("redder");   std::cout << "\nredder takes over." << std::endl;   std::cout << "red's color     : " << red.getColor()  << std::endl;   std::cout << "blue's color    : " << blue.getColor() << std::endl;   std::cout << "green's color   : " << green.getColor()  << std::endl;   std::cout << "redders's color : " << redder.getColor()  << std::endl;   std::cout << "bluer's color   : " << bluer.getColor() << std::endl;   std::cout << "greener's color : " << greener.getColor()  << std::endl;   std::cout << "\nand so on." << std::endl;      return EXIT_SUCCESS;}


this code outputs:

defaults.red's color     : whiteblue's color    : whitegreen's color   : whiteredders's color : whitebluer's color   : whitegreener's color : whitered takes over.red's color     : redblue's color    : redgreen's color   : redredders's color : redbluer's color   : redgreener's color : redblue takes over.red's color     : blueblue's color    : bluegreen's color   : blueredders's color : bluebluer's color   : bluegreener's color : blueredder takes over.red's color     : redderblue's color    : reddergreen's color   : redderredders's color : redderbluer's color   : reddergreener's color : redderand so on.


You can now introduce pointers and intialize super or sub instances by using super for all, some examples are:

my_super * orange = new my_super();my_super * violet = new my_sub();my_super * pColor;void init_color_pointer(int choice){   // A switch statement or function-pointer.   pColor = new my_sub();}


--random_thinker
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.

Topic Locked

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

Sign in to reply to this topic.