Sign in to follow this  
Ultra The Vampire

RunTime Question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 definition
class my_super
{
protected:
static std::string color;

public:
void changeColor(std::string);
std::string getColor();
};

// my_super implementation
std::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 Definition
class 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 : white
blue's color : white
green's color : white
redders's color : white
bluer's color : white
greener's color : white

red takes over.
red's color : red
blue's color : red
green's color : red
redders's color : red
bluer's color : red
greener's color : red

blue takes over.
red's color : blue
blue's color : blue
green's color : blue
redders's color : blue
bluer's color : blue
greener's color : blue

redder takes over.
red's color : redder
blue's color : redder
green's color : redder
redders's color : redder
bluer's color : redder
greener's color : redder

and 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

Share this post


Link to post
Share on other sites

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

Sign in to follow this