Hi everyone! I need help with my current game. It is may first game, so I have a lot of new ideas. But I have a problem dealing with event listeners. The listeners need to know a lot of variables that exists inside main. But they are a different function, of course. My question is: How can I acess the main variables using another method, without having to use they like parameters.

I could use globals, but when I talk variables I mean a lot of them.
The only thing that "looks like"(damn!! sorry for the bad english) is the friend key word. I´m so sad...
One interesting thing: everyone knows that when inline function are used, the code is, actually, coppied to the position of the call, but I think that this is useless.

An example of the horrible thing I´m trying to do;

int main(){
int an_int;

//do something with an_int

f();

return 0;

}

void f(){

//do more things with an_int

}

Thanks for any help!!! :)

04-14-2003

DirX

If it is just a large group of varibles that you need to pass into functions you could just make a struct with all the variables in it, that way you would only have to pass the struct in every time. If you pass it in by reference you will not allocate any new memory but just simply use the memory that was allocated inside main.

for example:

Code:

struct vars {
int an_int;
}

void f(vars &);

int main()
{
vars game;
game.an_int = 0;

// do something with game.an_int;

f(game);

return 0;
}

void f(vars & game)
{
// do more with game.an_int;
}

you can have as many variables as you want inside the vars struct and all you have to pass around is game. Also no memory will be lost as it will pass around the address of game instead of copying all the values into new memory when you pass game around. The only bad thing is that you have to put "game." infront of every variable.

04-14-2003

axon

hello and welcome, firstly you should read the top two posts on this board, and use code tags.

As to your question, try using a class...you can use the class object in main as well as a function referenced in the class.

hope this helps,

axon

04-14-2003

PorkyChop

I'm not really understanding your objection to passing an_int as an argument to f()...seems to me like that's the only way to get it done other than making an_int global(urg). If there are other game data related to an_int...maybe you could make an object where an_int is a data member, and then make the listener a member function.

Quote:

One interesting thing: everyone knows that when inline function are used, the code is, actually, coppied to the position of the call, but I think that this is useless.

Normal functions take time to call. Inline functions take very little, if any, time to call. So, if you have a function that is small and is called often, you would make your program faster by making that function inline to reduce the call time.

04-15-2003

gustavosserra

THank YoU AlL!

Thank you every one! :)
That was the idea that I had this morning.
About saying that inline is useless, it is just for my problem...